Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 | /* * Copyright (c) 1999-2009 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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. * * hfs_vfsops.c * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95 * * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved. * * hfs_vfsops.c -- VFS layer for loadable HFS file system. * */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kauth.h> #include <sys/ubc.h> #include <sys/ubc_internal.h> #include <sys/vnode_internal.h> #include <sys/mount_internal.h> #include <sys/sysctl.h> #include <sys/malloc.h> #include <sys/stat.h> #include <sys/quota.h> #include <sys/disk.h> #include <sys/paths.h> #include <sys/utfconv.h> #include <sys/kdebug.h> #include <sys/fslog.h> #include <kern/locks.h> #include <vfs/vfs_journal.h> #include <miscfs/specfs/specdev.h> #include <hfs/hfs_mount.h> #include <libkern/crypto/md5.h> #include <uuid/uuid.h> #include "hfs.h" #include "hfs_catalog.h" #include "hfs_cnode.h" #include "hfs_dbg.h" #include "hfs_endian.h" #include "hfs_hotfiles.h" #include "hfs_quota.h" #include "hfscommon/headers/FileMgrInternal.h" #include "hfscommon/headers/BTreesInternal.h" #if HFS_DIAGNOSTIC int hfs_dbg_all = 0; int hfs_dbg_err = 0; #endif lck_grp_attr_t * hfs_group_attr; lck_attr_t * hfs_lock_attr; lck_grp_t * hfs_mutex_group; lck_grp_t * hfs_rwlock_group; extern struct vnodeopv_desc hfs_vnodeop_opv_desc; extern struct vnodeopv_desc hfs_std_vnodeop_opv_desc; /* not static so we can re-use in hfs_readwrite.c for build_path calls */ int hfs_vfs_vget(struct mount *mp, ino64_t ino, struct vnode **vpp, vfs_context_t context); static int hfs_changefs(struct mount *mp, struct hfs_mount_args *args); static int hfs_fhtovp(struct mount *mp, int fhlen, unsigned char *fhp, struct vnode **vpp, vfs_context_t context); static int hfs_flushfiles(struct mount *, int, struct proc *); static int hfs_flushMDB(struct hfsmount *hfsmp, int waitfor, int altflush); static int hfs_getmountpoint(struct vnode *vp, struct hfsmount **hfsmpp); static int hfs_init(struct vfsconf *vfsp); static int hfs_mount(struct mount *mp, vnode_t devvp, user_addr_t data, vfs_context_t context); static int hfs_mountfs(struct vnode *devvp, struct mount *mp, struct hfs_mount_args *args, int journal_replay_only, vfs_context_t context); static int hfs_reload(struct mount *mp); static int hfs_vfs_root(struct mount *mp, struct vnode **vpp, vfs_context_t context); static int hfs_quotactl(struct mount *, int, uid_t, caddr_t, vfs_context_t context); static int hfs_start(struct mount *mp, int flags, vfs_context_t context); static int hfs_statfs(struct mount *mp, register struct vfsstatfs *sbp, vfs_context_t context); static int hfs_sync(struct mount *mp, int waitfor, vfs_context_t context); static int hfs_sysctl(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp, user_addr_t newp, size_t newlen, vfs_context_t context); static int hfs_unmount(struct mount *mp, int mntflags, vfs_context_t context); static int hfs_vptofh(struct vnode *vp, int *fhlenp, unsigned char *fhp, vfs_context_t context); static int hfs_reclaimspace(struct hfsmount *hfsmp, u_int32_t startblk, u_int32_t reclaimblks, vfs_context_t context); static int hfs_overlapped_overflow_extents(struct hfsmount *hfsmp, u_int32_t startblk, u_int32_t catblks, u_int32_t fileID, int rsrcfork); static int hfs_journal_replay(vnode_t devvp, vfs_context_t context); /* * Called by vfs_mountroot when mounting HFS Plus as root. */ __private_extern__ int hfs_mountroot(mount_t mp, vnode_t rvp, vfs_context_t context) { struct hfsmount *hfsmp; ExtendedVCB *vcb; struct vfsstatfs *vfsp; int error; if ((error = hfs_mountfs(rvp, mp, NULL, 0, context))) return (error); /* Init hfsmp */ hfsmp = VFSTOHFS(mp); hfsmp->hfs_uid = UNKNOWNUID; hfsmp->hfs_gid = UNKNOWNGID; hfsmp->hfs_dir_mask = (S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH); /* 0755 */ hfsmp->hfs_file_mask = (S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH); /* 0755 */ /* Establish the free block reserve. */ vcb = HFSTOVCB(hfsmp); vcb->reserveBlocks = ((u_int64_t)vcb->totalBlocks * HFS_MINFREE) / 100; vcb->reserveBlocks = MIN(vcb->reserveBlocks, HFS_MAXRESERVE / vcb->blockSize); vfsp = vfs_statfs(mp); (void)hfs_statfs(mp, vfsp, NULL); return (0); } /* * VFS Operations. * * mount system call */ static int hfs_mount(struct mount *mp, vnode_t devvp, user_addr_t data, vfs_context_t context) { struct proc *p = vfs_context_proc(context); struct hfsmount *hfsmp = NULL; struct hfs_mount_args args; int retval = E_NONE; u_int32_t cmdflags; if ((retval = copyin(data, (caddr_t)&args, sizeof(args)))) { return (retval); } cmdflags = (u_int32_t)vfs_flags(mp) & MNT_CMDFLAGS; if (cmdflags & MNT_UPDATE) { hfsmp = VFSTOHFS(mp); /* Reload incore data after an fsck. */ if (cmdflags & MNT_RELOAD) { if (vfs_isrdonly(mp)) return hfs_reload(mp); else return (EINVAL); } /* Change to a read-only file system. */ if (((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) && vfs_isrdonly(mp)) { int flags; /* Set flag to indicate that a downgrade to read-only * is in progress and therefore block any further * modifications to the file system. */ hfs_global_exclusive_lock_acquire(hfsmp); hfsmp->hfs_flags |= HFS_RDONLY_DOWNGRADE; hfsmp->hfs_downgrading_proc = current_thread(); hfs_global_exclusive_lock_release(hfsmp); /* use VFS_SYNC to push out System (btree) files */ retval = VFS_SYNC(mp, MNT_WAIT, context); if (retval && ((cmdflags & MNT_FORCE) == 0)) { hfsmp->hfs_flags &= ~HFS_RDONLY_DOWNGRADE; hfsmp->hfs_downgrading_proc = NULL; goto out; } flags = WRITECLOSE; if (cmdflags & MNT_FORCE) flags |= FORCECLOSE; if ((retval = hfs_flushfiles(mp, flags, p))) { hfsmp->hfs_flags &= ~HFS_RDONLY_DOWNGRADE; hfsmp->hfs_downgrading_proc = NULL; goto out; } /* mark the volume cleanly unmounted */ hfsmp->vcbAtrb |= kHFSVolumeUnmountedMask; retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); hfsmp->hfs_flags |= HFS_READ_ONLY; /* also get the volume bitmap blocks */ if (!retval) { if (vnode_mount(hfsmp->hfs_devvp) == mp) { retval = hfs_fsync(hfsmp->hfs_devvp, MNT_WAIT, 0, p); } else { vnode_get(hfsmp->hfs_devvp); retval = VNOP_FSYNC(hfsmp->hfs_devvp, MNT_WAIT, context); vnode_put(hfsmp->hfs_devvp); } } if (retval) { hfsmp->hfs_flags &= ~HFS_RDONLY_DOWNGRADE; hfsmp->hfs_downgrading_proc = NULL; hfsmp->hfs_flags &= ~HFS_READ_ONLY; goto out; } if (hfsmp->jnl) { hfs_global_exclusive_lock_acquire(hfsmp); journal_close(hfsmp->jnl); hfsmp->jnl = NULL; // Note: we explicitly don't want to shutdown // access to the jvp because we may need // it later if we go back to being read-write. hfs_global_exclusive_lock_release(hfsmp); } hfsmp->hfs_downgrading_proc = NULL; } /* Change to a writable file system. */ if (vfs_iswriteupgrade(mp)) { /* * On inconsistent disks, do not allow read-write mount * unless it is the boot volume being mounted. */ if (!(vfs_flags(mp) & MNT_ROOTFS) && (hfsmp->vcbAtrb & kHFSVolumeInconsistentMask)) { retval = EINVAL; goto out; } // If the journal was shut-down previously because we were // asked to be read-only, let's start it back up again now if ( (HFSTOVCB(hfsmp)->vcbAtrb & kHFSVolumeJournaledMask) && hfsmp->jnl == NULL && hfsmp->jvp != NULL) { int jflags; if (hfsmp->hfs_flags & HFS_NEED_JNL_RESET) { jflags = JOURNAL_RESET; } else { jflags = 0; } hfs_global_exclusive_lock_acquire(hfsmp); hfsmp->jnl = journal_open(hfsmp->jvp, (hfsmp->jnl_start * HFSTOVCB(hfsmp)->blockSize) + (off_t)HFSTOVCB(hfsmp)->hfsPlusIOPosOffset, hfsmp->jnl_size, hfsmp->hfs_devvp, hfsmp->hfs_logical_block_size, jflags, 0, hfs_sync_metadata, hfsmp->hfs_mp); hfs_global_exclusive_lock_release(hfsmp); if (hfsmp->jnl == NULL) { retval = EINVAL; goto out; } else { hfsmp->hfs_flags &= ~HFS_NEED_JNL_RESET; } } /* See if we need to erase unused Catalog nodes due to <rdar://problem/6947811>. */ retval = hfs_erase_unused_nodes(hfsmp); if (retval != E_NONE) goto out; /* Only clear HFS_READ_ONLY after a successful write */ hfsmp->hfs_flags &= ~HFS_READ_ONLY; /* If this mount point was downgraded from read-write * to read-only, clear that information as we are now * moving back to read-write. */ hfsmp->hfs_flags &= ~HFS_RDONLY_DOWNGRADE; hfsmp->hfs_downgrading_proc = NULL; /* mark the volume dirty (clear clean unmount bit) */ hfsmp->vcbAtrb &= ~kHFSVolumeUnmountedMask; retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); if (retval != E_NONE) goto out; if (!(hfsmp->hfs_flags & (HFS_READ_ONLY | HFS_STANDARD))) { /* Setup private/hidden directories for hardlinks. */ hfs_privatedir_init(hfsmp, FILE_HARDLINKS); hfs_privatedir_init(hfsmp, DIR_HARDLINKS); hfs_remove_orphans(hfsmp); /* * Allow hot file clustering if conditions allow. */ if (hfsmp->hfs_flags & HFS_METADATA_ZONE) { (void) hfs_recording_init(hfsmp); } /* Force ACLs on HFS+ file systems. */ if (vfs_extendedsecurity(HFSTOVFS(hfsmp)) == 0) { vfs_setextendedsecurity(HFSTOVFS(hfsmp)); } } } /* Update file system parameters. */ retval = hfs_changefs(mp, &args); } else /* not an update request */ { /* Set the mount flag to indicate that we support volfs */ vfs_setflags(mp, (u_int64_t)((unsigned int)MNT_DOVOLFS)); retval = hfs_mountfs(devvp, mp, &args, 0, context); } out: if (retval == 0) { (void)hfs_statfs(mp, vfs_statfs(mp), context); } return (retval); } struct hfs_changefs_cargs { struct hfsmount *hfsmp; int namefix; int permfix; int permswitch; }; static int hfs_changefs_callback(struct vnode *vp, void *cargs) { ExtendedVCB *vcb; struct cnode *cp; struct cat_desc cndesc; struct cat_attr cnattr; struct hfs_changefs_cargs *args; int lockflags; int error; args = (struct hfs_changefs_cargs *)cargs; cp = VTOC(vp); vcb = HFSTOVCB(args->hfsmp); lockflags = hfs_systemfile_lock(args->hfsmp, SFL_CATALOG, HFS_SHARED_LOCK); error = cat_lookup(args->hfsmp, &cp->c_desc, 0, &cndesc, &cnattr, NULL, NULL); hfs_systemfile_unlock(args->hfsmp, lockflags); if (error) { /* * If we couldn't find this guy skip to the next one */ if (args->namefix) cache_purge(vp); return (VNODE_RETURNED); } /* * Get the real uid/gid and perm mask from disk. */ if (args->permswitch || args->permfix) { cp->c_uid = cnattr.ca_uid; cp->c_gid = cnattr.ca_gid; cp->c_mode = cnattr.ca_mode; } /* * If we're switching name converters then... * Remove the existing entry from the namei cache. * Update name to one based on new encoder. */ if (args->namefix) { cache_purge(vp); replace_desc(cp, &cndesc); if (cndesc.cd_cnid == kHFSRootFolderID) { strlcpy((char *)vcb->vcbVN, (const char *)cp->c_desc.cd_nameptr, NAME_MAX+1); cp->c_desc.cd_encoding = args->hfsmp->hfs_encoding; } } else { cat_releasedesc(&cndesc); } return (VNODE_RETURNED); } /* Change fs mount parameters */ static int hfs_changefs(struct mount *mp, struct hfs_mount_args *args) { int retval = 0; int namefix, permfix, permswitch; struct hfsmount *hfsmp; ExtendedVCB *vcb; hfs_to_unicode_func_t get_unicode_func; unicode_to_hfs_func_t get_hfsname_func; u_int32_t old_encoding = 0; struct hfs_changefs_cargs cargs; u_int32_t mount_flags; hfsmp = VFSTOHFS(mp); vcb = HFSTOVCB(hfsmp); mount_flags = (unsigned int)vfs_flags(mp); hfsmp->hfs_flags |= HFS_IN_CHANGEFS; permswitch = (((hfsmp->hfs_flags & HFS_UNKNOWN_PERMS) && ((mount_flags & MNT_UNKNOWNPERMISSIONS) == 0)) || (((hfsmp->hfs_flags & HFS_UNKNOWN_PERMS) == 0) && (mount_flags & MNT_UNKNOWNPERMISSIONS))); /* The root filesystem must operate with actual permissions: */ if (permswitch && (mount_flags & MNT_ROOTFS) && (mount_flags & MNT_UNKNOWNPERMISSIONS)) { vfs_clearflags(mp, (u_int64_t)((unsigned int)MNT_UNKNOWNPERMISSIONS)); /* Just say "No". */ retval = EINVAL; goto exit; } if (mount_flags & MNT_UNKNOWNPERMISSIONS) hfsmp->hfs_flags |= HFS_UNKNOWN_PERMS; else hfsmp->hfs_flags &= ~HFS_UNKNOWN_PERMS; namefix = permfix = 0; /* * Tracking of hot files requires up-to-date access times. So if * access time updates are disabled, we must also disable hot files. */ if (mount_flags & MNT_NOATIME) { (void) hfs_recording_suspend(hfsmp); } /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */ if (args->hfs_timezone.tz_minuteswest != VNOVAL) { gTimeZone = args->hfs_timezone; } /* Change the default uid, gid and/or mask */ if ((args->hfs_uid != (uid_t)VNOVAL) && (hfsmp->hfs_uid != args->hfs_uid)) { hfsmp->hfs_uid = args->hfs_uid; if (vcb->vcbSigWord == kHFSPlusSigWord) ++permfix; } if ((args->hfs_gid != (gid_t)VNOVAL) && (hfsmp->hfs_gid != args->hfs_gid)) { hfsmp->hfs_gid = args->hfs_gid; if (vcb->vcbSigWord == kHFSPlusSigWord) ++permfix; } if (args->hfs_mask != (mode_t)VNOVAL) { if (hfsmp->hfs_dir_mask != (args->hfs_mask & ALLPERMS)) { hfsmp->hfs_dir_mask = args->hfs_mask & ALLPERMS; hfsmp->hfs_file_mask = args->hfs_mask & ALLPERMS; if ((args->flags != VNOVAL) && (args->flags & HFSFSMNT_NOXONFILES)) hfsmp->hfs_file_mask = (args->hfs_mask & DEFFILEMODE); if (vcb->vcbSigWord == kHFSPlusSigWord) ++permfix; } } /* Change the hfs encoding value (hfs only) */ if ((vcb->vcbSigWord == kHFSSigWord) && (args->hfs_encoding != (u_int32_t)VNOVAL) && (hfsmp->hfs_encoding != args->hfs_encoding)) { retval = hfs_getconverter(args->hfs_encoding, &get_unicode_func, &get_hfsname_func); if (retval) goto exit; /* * Connect the new hfs_get_unicode converter but leave * the old hfs_get_hfsname converter in place so that * we can lookup existing vnodes to get their correctly * encoded names. * * When we're all finished, we can then connect the new * hfs_get_hfsname converter and release our interest * in the old converters. */ hfsmp->hfs_get_unicode = get_unicode_func; old_encoding = hfsmp->hfs_encoding; hfsmp->hfs_encoding = args->hfs_encoding; ++namefix; } if (!(namefix || permfix || permswitch)) goto exit; /* XXX 3762912 hack to support HFS filesystem 'owner' */ if (permfix) vfs_setowner(mp, hfsmp->hfs_uid == UNKNOWNUID ? KAUTH_UID_NONE : hfsmp->hfs_uid, hfsmp->hfs_gid == UNKNOWNGID ? KAUTH_GID_NONE : hfsmp->hfs_gid); /* * For each active vnode fix things that changed * * Note that we can visit a vnode more than once * and we can race with fsync. * * hfs_changefs_callback will be called for each vnode * hung off of this mount point * * The vnode will be properly referenced and unreferenced * around the callback */ cargs.hfsmp = hfsmp; cargs.namefix = namefix; cargs.permfix = permfix; cargs.permswitch = permswitch; vnode_iterate(mp, 0, hfs_changefs_callback, (void *)&cargs); /* * If we're switching name converters we can now * connect the new hfs_get_hfsname converter and * release our interest in the old converters. */ if (namefix) { hfsmp->hfs_get_hfsname = get_hfsname_func; vcb->volumeNameEncodingHint = args->hfs_encoding; (void) hfs_relconverter(old_encoding); } exit: hfsmp->hfs_flags &= ~HFS_IN_CHANGEFS; return (retval); } struct hfs_reload_cargs { struct hfsmount *hfsmp; int error; }; static int hfs_reload_callback(struct vnode *vp, void *cargs) { struct cnode *cp; struct hfs_reload_cargs *args; int lockflags; args = (struct hfs_reload_cargs *)cargs; /* * flush all the buffers associated with this node */ (void) buf_invalidateblks(vp, 0, 0, 0); cp = VTOC(vp); /* * Remove any directory hints */ if (vnode_isdir(vp)) hfs_reldirhints(cp, 0); /* * Re-read cnode data for all active vnodes (non-metadata files). */ if (!vnode_issystem(vp) && !VNODE_IS_RSRC(vp)) { struct cat_fork *datafork; struct cat_desc desc; datafork = cp->c_datafork ? &cp->c_datafork->ff_data : NULL; /* lookup by fileID since name could have changed */ lockflags = hfs_systemfile_lock(args->hfsmp, SFL_CATALOG, HFS_SHARED_LOCK); args->error = cat_idlookup(args->hfsmp, cp->c_fileid, 0, &desc, &cp->c_attr, datafork); hfs_systemfile_unlock(args->hfsmp, lockflags); if (args->error) { return (VNODE_RETURNED_DONE); } /* update cnode's catalog descriptor */ (void) replace_desc(cp, &desc); } return (VNODE_RETURNED); } /* * Reload all incore data for a filesystem (used after running fsck on * the root filesystem and finding things to fix). The filesystem must * be mounted read-only. * * Things to do to update the mount: * invalidate all cached meta-data. * invalidate all inactive vnodes. * invalidate all cached file data. * re-read volume header from disk. * re-load meta-file info (extents, file size). * re-load B-tree header data. * re-read cnode data for all active vnodes. */ static int hfs_reload(struct mount *mountp) { register struct vnode *devvp; struct buf *bp; int error, i; struct hfsmount *hfsmp; struct HFSPlusVolumeHeader *vhp; ExtendedVCB *vcb; struct filefork *forkp; struct cat_desc cndesc; struct hfs_reload_cargs args; daddr64_t priIDSector; hfsmp = VFSTOHFS(mountp); vcb = HFSTOVCB(hfsmp); if (vcb->vcbSigWord == kHFSSigWord) return (EINVAL); /* rooting from HFS is not supported! */ /* * Invalidate all cached meta-data. */ devvp = hfsmp->hfs_devvp; if (buf_invalidateblks(devvp, 0, 0, 0)) panic("hfs_reload: dirty1"); args.hfsmp = hfsmp; args.error = 0; /* * hfs_reload_callback will be called for each vnode * hung off of this mount point that can't be recycled... * vnode_iterate will recycle those that it can (the VNODE_RELOAD option) * the vnode will be in an 'unbusy' state (VNODE_WAIT) and * properly referenced and unreferenced around the callback */ vnode_iterate(mountp, VNODE_RELOAD | VNODE_WAIT, hfs_reload_callback, (void *)&args); if (args.error) return (args.error); /* * Re-read VolumeHeader from disk. */ priIDSector = (daddr64_t)((vcb->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size) + HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size)); error = (int)buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(priIDSector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &bp); if (error) { if (bp != NULL) buf_brelse(bp); return (error); } vhp = (HFSPlusVolumeHeader *) (buf_dataptr(bp) + HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size)); /* Do a quick sanity check */ if ((SWAP_BE16(vhp->signature) != kHFSPlusSigWord && SWAP_BE16(vhp->signature) != kHFSXSigWord) || (SWAP_BE16(vhp->version) != kHFSPlusVersion && SWAP_BE16(vhp->version) != kHFSXVersion) || SWAP_BE32(vhp->blockSize) != vcb->blockSize) { buf_brelse(bp); return (EIO); } vcb->vcbLsMod = to_bsd_time(SWAP_BE32(vhp->modifyDate)); vcb->vcbAtrb = SWAP_BE32 (vhp->attributes); vcb->vcbJinfoBlock = SWAP_BE32(vhp->journalInfoBlock); vcb->vcbClpSiz = SWAP_BE32 (vhp->rsrcClumpSize); vcb->vcbNxtCNID = SWAP_BE32 (vhp->nextCatalogID); vcb->vcbVolBkUp = to_bsd_time(SWAP_BE32(vhp->backupDate)); vcb->vcbWrCnt = SWAP_BE32 (vhp->writeCount); vcb->vcbFilCnt = SWAP_BE32 (vhp->fileCount); vcb->vcbDirCnt = SWAP_BE32 (vhp->folderCount); HFS_UPDATE_NEXT_ALLOCATION(vcb, SWAP_BE32 (vhp->nextAllocation)); vcb->totalBlocks = SWAP_BE32 (vhp->totalBlocks); vcb->freeBlocks = SWAP_BE32 (vhp->freeBlocks); vcb->encodingsBitmap = SWAP_BE64 (vhp->encodingsBitmap); bcopy(vhp->finderInfo, vcb->vcbFndrInfo, sizeof(vhp->finderInfo)); vcb->localCreateDate = SWAP_BE32 (vhp->createDate); /* hfs+ create date is in local time */ /* * Re-load meta-file vnode data (extent info, file size, etc). */ forkp = VTOF((struct vnode *)vcb->extentsRefNum); for (i = 0; i < kHFSPlusExtentDensity; i++) { forkp->ff_extents[i].startBlock = SWAP_BE32 (vhp->extentsFile.extents[i].startBlock); forkp->ff_extents[i].blockCount = SWAP_BE32 (vhp->extentsFile.extents[i].blockCount); } forkp->ff_size = SWAP_BE64 (vhp->extentsFile.logicalSize); forkp->ff_blocks = SWAP_BE32 (vhp->extentsFile.totalBlocks); forkp->ff_clumpsize = SWAP_BE32 (vhp->extentsFile.clumpSize); forkp = VTOF((struct vnode *)vcb->catalogRefNum); for (i = 0; i < kHFSPlusExtentDensity; i++) { forkp->ff_extents[i].startBlock = SWAP_BE32 (vhp->catalogFile.extents[i].startBlock); forkp->ff_extents[i].blockCount = SWAP_BE32 (vhp->catalogFile.extents[i].blockCount); } forkp->ff_size = SWAP_BE64 (vhp->catalogFile.logicalSize); forkp->ff_blocks = SWAP_BE32 (vhp->catalogFile.totalBlocks); forkp->ff_clumpsize = SWAP_BE32 (vhp->catalogFile.clumpSize); if (hfsmp->hfs_attribute_vp) { forkp = VTOF(hfsmp->hfs_attribute_vp); for (i = 0; i < kHFSPlusExtentDensity; i++) { forkp->ff_extents[i].startBlock = SWAP_BE32 (vhp->attributesFile.extents[i].startBlock); forkp->ff_extents[i].blockCount = SWAP_BE32 (vhp->attributesFile.extents[i].blockCount); } forkp->ff_size = SWAP_BE64 (vhp->attributesFile.logicalSize); forkp->ff_blocks = SWAP_BE32 (vhp->attributesFile.totalBlocks); forkp->ff_clumpsize = SWAP_BE32 (vhp->attributesFile.clumpSize); } forkp = VTOF((struct vnode *)vcb->allocationsRefNum); for (i = 0; i < kHFSPlusExtentDensity; i++) { forkp->ff_extents[i].startBlock = SWAP_BE32 (vhp->allocationFile.extents[i].startBlock); forkp->ff_extents[i].blockCount = SWAP_BE32 (vhp->allocationFile.extents[i].blockCount); } forkp->ff_size = SWAP_BE64 (vhp->allocationFile.logicalSize); forkp->ff_blocks = SWAP_BE32 (vhp->allocationFile.totalBlocks); forkp->ff_clumpsize = SWAP_BE32 (vhp->allocationFile.clumpSize); buf_brelse(bp); vhp = NULL; /* * Re-load B-tree header data */ forkp = VTOF((struct vnode *)vcb->extentsRefNum); if ( (error = MacToVFSError( BTReloadData((FCB*)forkp) )) ) return (error); forkp = VTOF((struct vnode *)vcb->catalogRefNum); if ( (error = MacToVFSError( BTReloadData((FCB*)forkp) )) ) return (error); if (hfsmp->hfs_attribute_vp) { forkp = VTOF(hfsmp->hfs_attribute_vp); if ( (error = MacToVFSError( BTReloadData((FCB*)forkp) )) ) return (error); } /* Reload the volume name */ if ((error = cat_idlookup(hfsmp, kHFSRootFolderID, 0, &cndesc, NULL, NULL))) return (error); vcb->volumeNameEncodingHint = cndesc.cd_encoding; bcopy(cndesc.cd_nameptr, vcb->vcbVN, min(255, cndesc.cd_namelen)); cat_releasedesc(&cndesc); /* Re-establish private/hidden directories. */ hfs_privatedir_init(hfsmp, FILE_HARDLINKS); hfs_privatedir_init(hfsmp, DIR_HARDLINKS); /* In case any volume information changed to trigger a notification */ hfs_generate_volume_notifications(hfsmp); return (0); } static void hfs_syncer(void *arg0, void *unused) { #pragma unused(unused) struct hfsmount *hfsmp = arg0; clock_sec_t secs; clock_usec_t usecs; uint32_t delay = HFS_META_DELAY; uint64_t now; static int no_max=1; clock_get_calendar_microtime(&secs, &usecs); now = ((uint64_t)secs * 1000000ULL) + (uint64_t)usecs; // // If the amount of pending writes is more than our limit, wait // for 2/3 of it to drain and then flush the journal. // if (hfsmp->hfs_mp->mnt_pending_write_size > hfsmp->hfs_max_pending_io) { int counter=0; uint64_t pending_io, start, rate; no_max = 0; hfs_start_transaction(hfsmp); // so we hold off any new i/o's pending_io = hfsmp->hfs_mp->mnt_pending_write_size; clock_get_calendar_microtime(&secs, &usecs); start = ((uint64_t)secs * 1000000ULL) + (uint64_t)usecs; while(hfsmp->hfs_mp->mnt_pending_write_size > (pending_io/3) && counter++ < 500) { tsleep((caddr_t)hfsmp, PRIBIO, "hfs-wait-for-io-to-drain", 10); } if (counter >= 500) { printf("hfs: timed out waiting for io to drain (%lld)\n", (int64_t)hfsmp->hfs_mp->mnt_pending_write_size); } if (hfsmp->jnl) { journal_flush(hfsmp->jnl); } else { hfs_sync(hfsmp->hfs_mp, MNT_WAIT, vfs_context_kernel()); } clock_get_calendar_microtime(&secs, &usecs); now = ((uint64_t)secs * 1000000ULL) + (uint64_t)usecs; hfsmp->hfs_last_sync_time = now; rate = ((pending_io * 1000000ULL) / (now - start)); // yields bytes per second hfs_end_transaction(hfsmp); // // If a reasonable amount of time elapsed then check the // i/o rate. If it's taking less than 1 second or more // than 2 seconds, adjust hfs_max_pending_io so that we // will allow about 1.5 seconds of i/o to queue up. // if ((now - start) >= 300000) { uint64_t scale = (pending_io * 100) / rate; if (scale < 100 || scale > 200) { // set it so that it should take about 1.5 seconds to drain hfsmp->hfs_max_pending_io = (rate * 150ULL) / 100ULL; } } } else if ( ((now - hfsmp->hfs_last_sync_time) >= 5000000ULL) || (((now - hfsmp->hfs_last_sync_time) >= 100000LL) && ((now - hfsmp->hfs_last_sync_request_time) >= 100000LL) && (hfsmp->hfs_active_threads == 0) && (hfsmp->hfs_global_lock_nesting == 0))) { // // Flush the journal if more than 5 seconds elapsed since // the last sync OR we have not sync'ed recently and the // last sync request time was more than 100 milliseconds // ago and no one is in the middle of a transaction right // now. Else we defer the sync and reschedule it. // if (hfsmp->jnl) { lck_rw_lock_shared(&hfsmp->hfs_global_lock); journal_flush(hfsmp->jnl); lck_rw_unlock_shared(&hfsmp->hfs_global_lock); } else { hfs_sync(hfsmp->hfs_mp, MNT_WAIT, vfs_context_kernel()); } clock_get_calendar_microtime(&secs, &usecs); now = ((uint64_t)secs * 1000000ULL) + (uint64_t)usecs; hfsmp->hfs_last_sync_time = now; } else if (hfsmp->hfs_active_threads == 0) { uint64_t deadline; clock_interval_to_deadline(delay, HFS_MILLISEC_SCALE, &deadline); thread_call_enter_delayed(hfsmp->hfs_syncer, deadline); // note: we intentionally return early here and do not // decrement the sync_scheduled and sync_incomplete // variables because we rescheduled the timer. return; } // // NOTE: we decrement these *after* we're done the journal_flush() since // it can take a significant amount of time and so we don't want more // callbacks scheduled until we're done this one. // OSDecrementAtomic((volatile SInt32 *)&hfsmp->hfs_sync_scheduled); OSDecrementAtomic((volatile SInt32 *)&hfsmp->hfs_sync_incomplete); wakeup((caddr_t)&hfsmp->hfs_sync_incomplete); } extern int IOBSDIsMediaEjectable( const char *cdev_name ); /* * Common code for mount and mountroot */ static int hfs_mountfs(struct vnode *devvp, struct mount *mp, struct hfs_mount_args *args, int journal_replay_only, vfs_context_t context) { struct proc *p = vfs_context_proc(context); int retval = E_NONE; struct hfsmount *hfsmp = NULL; struct buf *bp; dev_t dev; HFSMasterDirectoryBlock *mdbp = NULL; int ronly; #if QUOTA int i; #endif int mntwrapper; kauth_cred_t cred; u_int64_t disksize; daddr64_t log_blkcnt; u_int32_t log_blksize; u_int32_t phys_blksize; u_int32_t minblksize; u_int32_t iswritable; daddr64_t mdb_offset; int isvirtual = 0; int isroot = 0; if (args == NULL) { /* only hfs_mountroot passes us NULL as the 'args' argument */ isroot = 1; } ronly = vfs_isrdonly(mp); dev = vnode_specrdev(devvp); cred = p ? vfs_context_ucred(context) : NOCRED; mntwrapper = 0; bp = NULL; hfsmp = NULL; mdbp = NULL; minblksize = kHFSBlockSize; /* Advisory locking should be handled at the VFS layer */ vfs_setlocklocal(mp); /* Get the logical block size (treated as physical block size everywhere) */ if (VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&log_blksize, 0, context)) { retval = ENXIO; goto error_exit; } if (log_blksize == 0 || log_blksize > 1024*1024*1024) { printf("hfs: logical block size 0x%x looks bad. Not mounting.\n", log_blksize); retval = ENXIO; goto error_exit; } /* Get the physical block size. */ retval = VNOP_IOCTL(devvp, DKIOCGETPHYSICALBLOCKSIZE, (caddr_t)&phys_blksize, 0, context); if (retval) { if ((retval != ENOTSUP) && (retval != ENOTTY)) { retval = ENXIO; goto error_exit; } /* If device does not support this ioctl, assume that physical * block size is same as logical block size */ phys_blksize = log_blksize; } if (phys_blksize == 0 || phys_blksize > 1024*1024*1024) { printf("hfs: physical block size 0x%x looks bad. Not mounting.\n", phys_blksize); retval = ENXIO; goto error_exit; } /* Switch to 512 byte sectors (temporarily) */ if (log_blksize > 512) { u_int32_t size512 = 512; if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, context)) { retval = ENXIO; goto error_exit; } } /* Get the number of 512 byte physical blocks. */ if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) { /* resetting block size may fail if getting block count did */ (void)VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context); retval = ENXIO; goto error_exit; } /* Compute an accurate disk size (i.e. within 512 bytes) */ disksize = (u_int64_t)log_blkcnt * (u_int64_t)512; /* * On Tiger it is not necessary to switch the device * block size to be 4k if there are more than 31-bits * worth of blocks but to insure compatibility with * pre-Tiger systems we have to do it. * * If the device size is not a multiple of 4K (8 * 512), then * switching the logical block size isn't going to help because * we will be unable to write the alternate volume header. * In this case, just leave the logical block size unchanged. */ if (log_blkcnt > 0x000000007fffffff && (log_blkcnt & 7) == 0) { minblksize = log_blksize = 4096; if (phys_blksize < log_blksize) phys_blksize = log_blksize; } /* * The cluster layer is not currently prepared to deal with a logical * block size larger than the system's page size. (It can handle * blocks per page, but not multiple pages per block.) So limit the * logical block size to the page size. */ if (log_blksize > PAGE_SIZE) log_blksize = PAGE_SIZE; /* Now switch to our preferred physical block size. */ if (log_blksize > 512) { if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context)) { retval = ENXIO; goto error_exit; } /* Get the count of physical blocks. */ if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) { retval = ENXIO; goto error_exit; } } /* * At this point: * minblksize is the minimum physical block size * log_blksize has our preferred physical block size * log_blkcnt has the total number of physical blocks */ mdb_offset = (daddr64_t)HFS_PRI_SECTOR(log_blksize); if ((retval = (int)buf_meta_bread(devvp, HFS_PHYSBLK_ROUNDDOWN(mdb_offset, (phys_blksize/log_blksize)), phys_blksize, cred, &bp))) { goto error_exit; } MALLOC(mdbp, HFSMasterDirectoryBlock *, kMDBSize, M_TEMP, M_WAITOK); if (mdbp == NULL) { retval = ENOMEM; goto error_exit; } bcopy((char *)buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize), mdbp, kMDBSize); buf_brelse(bp); bp = NULL; MALLOC(hfsmp, struct hfsmount *, sizeof(struct hfsmount), M_HFSMNT, M_WAITOK); if (hfsmp == NULL) { retval = ENOMEM; goto error_exit; } bzero(hfsmp, sizeof(struct hfsmount)); hfs_chashinit_finish(hfsmp); /* * Init the volume information structure */ lck_mtx_init(&hfsmp->hfs_mutex, hfs_mutex_group, hfs_lock_attr); lck_mtx_init(&hfsmp->hfc_mutex, hfs_mutex_group, hfs_lock_attr); lck_rw_init(&hfsmp->hfs_global_lock, hfs_rwlock_group, hfs_lock_attr); lck_rw_init(&hfsmp->hfs_insync, hfs_rwlock_group, hfs_lock_attr); vfs_setfsprivate(mp, hfsmp); hfsmp->hfs_mp = mp; /* Make VFSTOHFS work */ hfsmp->hfs_raw_dev = vnode_specrdev(devvp); hfsmp->hfs_devvp = devvp; vnode_ref(devvp); /* Hold a ref on the device, dropped when hfsmp is freed. */ hfsmp->hfs_logical_block_size = log_blksize; hfsmp->hfs_logical_block_count = log_blkcnt; hfsmp->hfs_physical_block_size = phys_blksize; hfsmp->hfs_log_per_phys = (phys_blksize / log_blksize); hfsmp->hfs_flags |= HFS_WRITEABLE_MEDIA; if (ronly) hfsmp->hfs_flags |= HFS_READ_ONLY; if (((unsigned int)vfs_flags(mp)) & MNT_UNKNOWNPERMISSIONS) hfsmp->hfs_flags |= HFS_UNKNOWN_PERMS; #if QUOTA for (i = 0; i < MAXQUOTAS; i++) dqfileinit(&hfsmp->hfs_qfiles[i]); #endif if (args) { hfsmp->hfs_uid = (args->hfs_uid == (uid_t)VNOVAL) ? UNKNOWNUID : args->hfs_uid; if (hfsmp->hfs_uid == 0xfffffffd) hfsmp->hfs_uid = UNKNOWNUID; hfsmp->hfs_gid = (args->hfs_gid == (gid_t)VNOVAL) ? UNKNOWNGID : args->hfs_gid; if (hfsmp->hfs_gid == 0xfffffffd) hfsmp->hfs_gid = UNKNOWNGID; vfs_setowner(mp, hfsmp->hfs_uid, hfsmp->hfs_gid); /* tell the VFS */ if (args->hfs_mask != (mode_t)VNOVAL) { hfsmp->hfs_dir_mask = args->hfs_mask & ALLPERMS; if (args->flags & HFSFSMNT_NOXONFILES) { hfsmp->hfs_file_mask = (args->hfs_mask & DEFFILEMODE); } else { hfsmp->hfs_file_mask = args->hfs_mask & ALLPERMS; } } else { hfsmp->hfs_dir_mask = UNKNOWNPERMISSIONS & ALLPERMS; /* 0777: rwx---rwx */ hfsmp->hfs_file_mask = UNKNOWNPERMISSIONS & DEFFILEMODE; /* 0666: no --x by default? */ } if ((args->flags != (int)VNOVAL) && (args->flags & HFSFSMNT_WRAPPER)) mntwrapper = 1; } else { /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */ if (((unsigned int)vfs_flags(mp)) & MNT_UNKNOWNPERMISSIONS) { hfsmp->hfs_uid = UNKNOWNUID; hfsmp->hfs_gid = UNKNOWNGID; vfs_setowner(mp, hfsmp->hfs_uid, hfsmp->hfs_gid); /* tell the VFS */ hfsmp->hfs_dir_mask = UNKNOWNPERMISSIONS & ALLPERMS; /* 0777: rwx---rwx */ hfsmp->hfs_file_mask = UNKNOWNPERMISSIONS & DEFFILEMODE; /* 0666: no --x by default? */ } } /* Find out if disk media is writable. */ if (VNOP_IOCTL(devvp, DKIOCISWRITABLE, (caddr_t)&iswritable, 0, context) == 0) { if (iswritable) hfsmp->hfs_flags |= HFS_WRITEABLE_MEDIA; else hfsmp->hfs_flags &= ~HFS_WRITEABLE_MEDIA; } // record the current time at which we're mounting this volume struct timeval tv; microtime(&tv); hfsmp->hfs_mount_time = tv.tv_sec; /* Mount a standard HFS disk */ if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && (mntwrapper || (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord))) { /* On 10.6 and beyond, non read-only mounts for HFS standard vols get rejected */ if (vfs_isrdwr(mp)) { retval = EROFS; goto error_exit; } /* Treat it as if it's read-only and not writeable */ hfsmp->hfs_flags |= HFS_READ_ONLY; hfsmp->hfs_flags &= ~HFS_WRITEABLE_MEDIA; /* If only journal replay is requested, exit immediately */ if (journal_replay_only) { retval = 0; goto error_exit; } if ((vfs_flags(mp) & MNT_ROOTFS)) { retval = EINVAL; /* Cannot root from HFS standard disks */ goto error_exit; } /* HFS disks can only use 512 byte physical blocks */ if (log_blksize > kHFSBlockSize) { log_blksize = kHFSBlockSize; if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context)) { retval = ENXIO; goto error_exit; } if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) { retval = ENXIO; goto error_exit; } hfsmp->hfs_logical_block_size = log_blksize; hfsmp->hfs_logical_block_count = log_blkcnt; hfsmp->hfs_physical_block_size = log_blksize; hfsmp->hfs_log_per_phys = 1; } if (args) { hfsmp->hfs_encoding = args->hfs_encoding; HFSTOVCB(hfsmp)->volumeNameEncodingHint = args->hfs_encoding; /* establish the timezone */ gTimeZone = args->hfs_timezone; } retval = hfs_getconverter(hfsmp->hfs_encoding, &hfsmp->hfs_get_unicode, &hfsmp->hfs_get_hfsname); if (retval) goto error_exit; retval = hfs_MountHFSVolume(hfsmp, mdbp, p); if (retval) (void) hfs_relconverter(hfsmp->hfs_encoding); } else /* Mount an HFS Plus disk */ { HFSPlusVolumeHeader *vhp; off_t embeddedOffset; int jnl_disable = 0; /* Get the embedded Volume Header */ if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) { embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * kHFSBlockSize; embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz); /* * If the embedded volume doesn't start on a block * boundary, then switch the device to a 512-byte * block size so everything will line up on a block * boundary. */ if ((embeddedOffset % log_blksize) != 0) { printf("hfs_mountfs: embedded volume offset not" " a multiple of physical block size (%d);" " switching to 512\n", log_blksize); log_blksize = 512; if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context)) { retval = ENXIO; goto error_exit; } if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) { retval = ENXIO; goto error_exit; } /* Note: relative block count adjustment */ hfsmp->hfs_logical_block_count *= hfsmp->hfs_logical_block_size / log_blksize; /* Update logical /physical block size */ hfsmp->hfs_logical_block_size = log_blksize; hfsmp->hfs_physical_block_size = log_blksize; phys_blksize = log_blksize; hfsmp->hfs_log_per_phys = 1; } disksize = (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.blockCount) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz); hfsmp->hfs_logical_block_count = disksize / log_blksize; mdb_offset = (daddr64_t)((embeddedOffset / log_blksize) + HFS_PRI_SECTOR(log_blksize)); retval = (int)buf_meta_bread(devvp, HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys), phys_blksize, cred, &bp); if (retval) goto error_exit; bcopy((char *)buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize), mdbp, 512); buf_brelse(bp); bp = NULL; vhp = (HFSPlusVolumeHeader*) mdbp; } else /* pure HFS+ */ { embeddedOffset = 0; vhp = (HFSPlusVolumeHeader*) mdbp; } /* * On inconsistent disks, do not allow read-write mount * unless it is the boot volume being mounted. We also * always want to replay the journal if the journal_replay_only * flag is set because that will (most likely) get the * disk into a consistent state before fsck_hfs starts * looking at it. */ if ( !(vfs_flags(mp) & MNT_ROOTFS) && (SWAP_BE32(vhp->attributes) & kHFSVolumeInconsistentMask) && !journal_replay_only && !(hfsmp->hfs_flags & HFS_READ_ONLY)) { retval = EINVAL; goto error_exit; } // XXXdbg // hfsmp->jnl = NULL; hfsmp->jvp = NULL; if (args != NULL && (args->flags & HFSFSMNT_EXTENDED_ARGS) && args->journal_disable) { jnl_disable = 1; } // // We only initialize the journal here if the last person // to mount this volume was journaling aware. Otherwise // we delay journal initialization until later at the end // of hfs_MountHFSPlusVolume() because the last person who // mounted it could have messed things up behind our back // (so we need to go find the .journal file, make sure it's // the right size, re-sync up if it was moved, etc). // if ( (SWAP_BE32(vhp->lastMountedVersion) == kHFSJMountVersion) && (SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask) && !jnl_disable) { // if we're able to init the journal, mark the mount // point as journaled. // if ((retval = hfs_early_journal_init(hfsmp, vhp, args, embeddedOffset, mdb_offset, mdbp, cred)) == 0) { vfs_setflags(mp, (u_int64_t)((unsigned int)MNT_JOURNALED)); } else { if (retval == EROFS) { // EROFS is a special error code that means the volume has an external // journal which we couldn't find. in that case we do not want to // rewrite the volume header - we'll just refuse to mount the volume. retval = EINVAL; goto error_exit; } // if the journal failed to open, then set the lastMountedVersion // to be "FSK!" which fsck_hfs will see and force the fsck instead // of just bailing out because the volume is journaled. if (!ronly) { HFSPlusVolumeHeader *jvhp; hfsmp->hfs_flags |= HFS_NEED_JNL_RESET; if (mdb_offset == 0) { mdb_offset = (daddr64_t)((embeddedOffset / log_blksize) + HFS_PRI_SECTOR(log_blksize)); } bp = NULL; retval = (int)buf_meta_bread(devvp, HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys), phys_blksize, cred, &bp); if (retval == 0) { jvhp = (HFSPlusVolumeHeader *)(buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize)); if (SWAP_BE16(jvhp->signature) == kHFSPlusSigWord || SWAP_BE16(jvhp->signature) == kHFSXSigWord) { printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n"); jvhp->lastMountedVersion = SWAP_BE32(kFSKMountVersion); buf_bwrite(bp); } else { buf_brelse(bp); } bp = NULL; } else if (bp) { buf_brelse(bp); // clear this so the error exit path won't try to use it bp = NULL; } } // if this isn't the root device just bail out. // If it is the root device we just continue on // in the hopes that fsck_hfs will be able to // fix any damage that exists on the volume. if ( !(vfs_flags(mp) & MNT_ROOTFS)) { retval = EINVAL; goto error_exit; } } } // XXXdbg /* Either the journal is replayed successfully, or there * was nothing to replay, or no journal exists. In any case, * return success. */ if (journal_replay_only) { retval = 0; goto error_exit; } (void) hfs_getconverter(0, &hfsmp->hfs_get_unicode, &hfsmp->hfs_get_hfsname); retval = hfs_MountHFSPlusVolume(hfsmp, vhp, embeddedOffset, disksize, p, args, cred); /* * If the backend didn't like our physical blocksize * then retry with physical blocksize of 512. */ if ((retval == ENXIO) && (log_blksize > 512) && (log_blksize != minblksize)) { printf("hfs_mountfs: could not use physical block size " "(%d) switching to 512\n", log_blksize); log_blksize = 512; if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context)) { retval = ENXIO; goto error_exit; } if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) { retval = ENXIO; goto error_exit; } devvp->v_specsize = log_blksize; /* Note: relative block count adjustment (in case this is an embedded volume). */ hfsmp->hfs_logical_block_count *= hfsmp->hfs_logical_block_size / log_blksize; hfsmp->hfs_logical_block_size = log_blksize; hfsmp->hfs_log_per_phys = hfsmp->hfs_physical_block_size / log_blksize; if (hfsmp->jnl && hfsmp->jvp == devvp) { // close and re-open this with the new block size journal_close(hfsmp->jnl); hfsmp->jnl = NULL; if (hfs_early_journal_init(hfsmp, vhp, args, embeddedOffset, mdb_offset, mdbp, cred) == 0) { vfs_setflags(mp, (u_int64_t)((unsigned int)MNT_JOURNALED)); } else { // if the journal failed to open, then set the lastMountedVersion // to be "FSK!" which fsck_hfs will see and force the fsck instead // of just bailing out because the volume is journaled. if (!ronly) { HFSPlusVolumeHeader *jvhp; hfsmp->hfs_flags |= HFS_NEED_JNL_RESET; if (mdb_offset == 0) { mdb_offset = (daddr64_t)((embeddedOffset / log_blksize) + HFS_PRI_SECTOR(log_blksize)); } bp = NULL; retval = (int)buf_meta_bread(devvp, HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys), phys_blksize, cred, &bp); if (retval == 0) { jvhp = (HFSPlusVolumeHeader *)(buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize)); if (SWAP_BE16(jvhp->signature) == kHFSPlusSigWord || SWAP_BE16(jvhp->signature) == kHFSXSigWord) { printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n"); jvhp->lastMountedVersion = SWAP_BE32(kFSKMountVersion); buf_bwrite(bp); } else { buf_brelse(bp); } bp = NULL; } else if (bp) { buf_brelse(bp); // clear this so the error exit path won't try to use it bp = NULL; } } // if this isn't the root device just bail out. // If it is the root device we just continue on // in the hopes that fsck_hfs will be able to // fix any damage that exists on the volume. if ( !(vfs_flags(mp) & MNT_ROOTFS)) { retval = EINVAL; goto error_exit; } } } /* Try again with a smaller block size... */ retval = hfs_MountHFSPlusVolume(hfsmp, vhp, embeddedOffset, disksize, p, args, cred); } if (retval) (void) hfs_relconverter(0); } // save off a snapshot of the mtime from the previous mount // (for matador). hfsmp->hfs_last_mounted_mtime = hfsmp->hfs_mtime; if ( retval ) { goto error_exit; } mp->mnt_vfsstat.f_fsid.val[0] = (long)dev; mp->mnt_vfsstat.f_fsid.val[1] = vfs_typenum(mp); vfs_setmaxsymlen(mp, 0); mp->mnt_vtable->vfc_vfsflags |= VFC_VFSNATIVEXATTR; #if NAMEDSTREAMS mp->mnt_kern_flag |= MNTK_NAMED_STREAMS; #endif if (!(hfsmp->hfs_flags & HFS_STANDARD)) { /* Tell VFS that we support directory hard links. */ mp->mnt_vtable->vfc_vfsflags |= VFC_VFSDIRLINKS; } else { /* HFS standard doesn't support extended readdir! */ mp->mnt_vtable->vfc_vfsflags &= ~VFC_VFSREADDIR_EXTENDED; } if (args) { /* * Set the free space warning levels for a non-root volume: * * Set the "danger" limit to 1% of the volume size or 100MB, whichever * is less. Set the "warning" limit to 2% of the volume size or 150MB, * whichever is less. And last, set the "desired" freespace level to * to 3% of the volume size or 200MB, whichever is less. */ hfsmp->hfs_freespace_notify_dangerlimit = MIN(HFS_VERYLOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize, (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_VERYLOWDISKTRIGGERFRACTION); hfsmp->hfs_freespace_notify_warninglimit = MIN(HFS_LOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize, (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_LOWDISKTRIGGERFRACTION); hfsmp->hfs_freespace_notify_desiredlevel = MIN(HFS_LOWDISKSHUTOFFLEVEL / HFSTOVCB(hfsmp)->blockSize, (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_LOWDISKSHUTOFFFRACTION); } else { /* * Set the free space warning levels for the root volume: * * Set the "danger" limit to 5% of the volume size or 125MB, whichever * is less. Set the "warning" limit to 10% of the volume size or 250MB, * whichever is less. And last, set the "desired" freespace level to * to 11% of the volume size or 375MB, whichever is less. */ hfsmp->hfs_freespace_notify_dangerlimit = MIN(HFS_ROOTVERYLOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize, (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_ROOTVERYLOWDISKTRIGGERFRACTION); hfsmp->hfs_freespace_notify_warninglimit = MIN(HFS_ROOTLOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize, (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_ROOTLOWDISKTRIGGERFRACTION); hfsmp->hfs_freespace_notify_desiredlevel = MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL / HFSTOVCB(hfsmp)->blockSize, (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION); }; /* Check if the file system exists on virtual device, like disk image */ if (VNOP_IOCTL(devvp, DKIOCISVIRTUAL, (caddr_t)&isvirtual, 0, context) == 0) { if (isvirtual) { hfsmp->hfs_flags |= HFS_VIRTUAL_DEVICE; } } /* do not allow ejectability checks on the root device */ if (isroot == 0) { if ((hfsmp->hfs_flags & HFS_VIRTUAL_DEVICE) == 0 && IOBSDIsMediaEjectable(mp->mnt_vfsstat.f_mntfromname)) { hfsmp->hfs_max_pending_io = 4096*1024; // a reasonable value to start with. hfsmp->hfs_syncer = thread_call_allocate(hfs_syncer, hfsmp); if (hfsmp->hfs_syncer == NULL) { printf("hfs: failed to allocate syncer thread callback for %s (%s)\n", mp->mnt_vfsstat.f_mntfromname, mp->mnt_vfsstat.f_mntonname); } } } /* * Start looking for free space to drop below this level and generate a * warning immediately if needed: */ hfsmp->hfs_notification_conditions = 0; hfs_generate_volume_notifications(hfsmp); if (ronly == 0) { (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); } FREE(mdbp, M_TEMP); return (0); error_exit: if (bp) buf_brelse(bp); if (mdbp) FREE(mdbp, M_TEMP); if (hfsmp && hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) { vnode_clearmountedon(hfsmp->jvp); (void)VNOP_CLOSE(hfsmp->jvp, ronly ? FREAD : FREAD|FWRITE, vfs_context_kernel()); hfsmp->jvp = NULL; } if (hfsmp) { if (hfsmp->hfs_devvp) { vnode_rele(hfsmp->hfs_devvp); } hfs_delete_chash(hfsmp); FREE(hfsmp, M_HFSMNT); vfs_setfsprivate(mp, NULL); } return (retval); } /* * Make a filesystem operational. * Nothing to do at the moment. */ /* ARGSUSED */ static int hfs_start(__unused struct mount *mp, __unused int flags, __unused vfs_context_t context) { return (0); } /* * unmount system call */ static int hfs_unmount(struct mount *mp, int mntflags, vfs_context_t context) { struct proc *p = vfs_context_proc(context); struct hfsmount *hfsmp = VFSTOHFS(mp); int retval = E_NONE; int flags; int force; int started_tr = 0; flags = 0; force = 0; if (mntflags & MNT_FORCE) { flags |= FORCECLOSE; force = 1; } if ((retval = hfs_flushfiles(mp, flags, p)) && !force) return (retval); if (hfsmp->hfs_flags & HFS_METADATA_ZONE) (void) hfs_recording_suspend(hfsmp); /* * Cancel any pending timers for this volume. Then wait for any timers * which have fired, but whose callbacks have not yet completed. */ if (hfsmp->hfs_syncer) { struct timespec ts = {0, 100000000}; /* 0.1 seconds */ /* * Cancel any timers that have been scheduled, but have not * fired yet. NOTE: The kernel considers a timer complete as * soon as it starts your callback, so the kernel does not * keep track of the number of callbacks in progress. */ if (thread_call_cancel(hfsmp->hfs_syncer)) OSDecrementAtomic((volatile SInt32 *)&hfsmp->hfs_sync_incomplete); thread_call_free(hfsmp->hfs_syncer); hfsmp->hfs_syncer = NULL; /* * This waits for all of the callbacks that were entered before * we did thread_call_cancel above, but have not completed yet. */ while(hfsmp->hfs_sync_incomplete > 0) { msleep((caddr_t)&hfsmp->hfs_sync_incomplete, NULL, PWAIT, "hfs_unmount", &ts); } if (hfsmp->hfs_sync_incomplete < 0) panic("hfs_unmount: pm_sync_incomplete underflow!\n"); } /* * Flush out the b-trees, volume bitmap and Volume Header */ if ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) { retval = hfs_start_transaction(hfsmp); if (retval == 0) { started_tr = 1; } else if (!force) { goto err_exit; } if (hfsmp->hfs_startup_vp) { (void) hfs_lock(VTOC(hfsmp->hfs_startup_vp), HFS_EXCLUSIVE_LOCK); retval = hfs_fsync(hfsmp->hfs_startup_vp, MNT_WAIT, 0, p); hfs_unlock(VTOC(hfsmp->hfs_startup_vp)); if (retval && !force) goto err_exit; } if (hfsmp->hfs_attribute_vp) { (void) hfs_lock(VTOC(hfsmp->hfs_attribute_vp), HFS_EXCLUSIVE_LOCK); retval = hfs_fsync(hfsmp->hfs_attribute_vp, MNT_WAIT, 0, p); hfs_unlock(VTOC(hfsmp->hfs_attribute_vp)); if (retval && !force) goto err_exit; } (void) hfs_lock(VTOC(hfsmp->hfs_catalog_vp), HFS_EXCLUSIVE_LOCK); retval = hfs_fsync(hfsmp->hfs_catalog_vp, MNT_WAIT, 0, p); hfs_unlock(VTOC(hfsmp->hfs_catalog_vp)); if (retval && !force) goto err_exit; (void) hfs_lock(VTOC(hfsmp->hfs_extents_vp), HFS_EXCLUSIVE_LOCK); retval = hfs_fsync(hfsmp->hfs_extents_vp, MNT_WAIT, 0, p); hfs_unlock(VTOC(hfsmp->hfs_extents_vp)); if (retval && !force) goto err_exit; if (hfsmp->hfs_allocation_vp) { (void) hfs_lock(VTOC(hfsmp->hfs_allocation_vp), HFS_EXCLUSIVE_LOCK); retval = hfs_fsync(hfsmp->hfs_allocation_vp, MNT_WAIT, 0, p); hfs_unlock(VTOC(hfsmp->hfs_allocation_vp)); if (retval && !force) goto err_exit; } if (hfsmp->hfc_filevp && vnode_issystem(hfsmp->hfc_filevp)) { retval = hfs_fsync(hfsmp->hfc_filevp, MNT_WAIT, 0, p); if (retval && !force) goto err_exit; } /* If runtime corruption was detected, indicate that the volume * was not unmounted cleanly. */ if (hfsmp->vcbAtrb & kHFSVolumeInconsistentMask) { HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeUnmountedMask; } else { HFSTOVCB(hfsmp)->vcbAtrb |= kHFSVolumeUnmountedMask; } if (hfsmp->hfs_flags & HFS_HAS_SPARSE_DEVICE) { int i; u_int32_t min_start = hfsmp->totalBlocks; // set the nextAllocation pointer to the smallest free block number // we've seen so on the next mount we won't rescan unnecessarily for(i=0; i < (int)hfsmp->vcbFreeExtCnt; i++) { if (hfsmp->vcbFreeExt[i].startBlock < min_start) { min_start = hfsmp->vcbFreeExt[i].startBlock; } } if (min_start < hfsmp->nextAllocation) { hfsmp->nextAllocation = min_start; } } retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); if (retval) { HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeUnmountedMask; if (!force) goto err_exit; /* could not flush everything */ } if (started_tr) { hfs_end_transaction(hfsmp); started_tr = 0; } } if (hfsmp->jnl) { hfs_journal_flush(hfsmp); } /* * Invalidate our caches and release metadata vnodes */ (void) hfsUnmount(hfsmp, p); /* * Last chance to dump unreferenced system files. */ (void) vflush(mp, NULLVP, FORCECLOSE); if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) (void) hfs_relconverter(hfsmp->hfs_encoding); // XXXdbg if (hfsmp->jnl) { journal_close(hfsmp->jnl); hfsmp->jnl = NULL; } VNOP_FSYNC(hfsmp->hfs_devvp, MNT_WAIT, context); if (hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) { vnode_clearmountedon(hfsmp->jvp); retval = VNOP_CLOSE(hfsmp->jvp, hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, vfs_context_kernel()); vnode_put(hfsmp->jvp); hfsmp->jvp = NULL; } // XXXdbg #ifdef HFS_SPARSE_DEV /* Drop our reference on the backing fs (if any). */ if ((hfsmp->hfs_flags & HFS_HAS_SPARSE_DEVICE) && hfsmp->hfs_backingfs_rootvp) { struct vnode * tmpvp; hfsmp->hfs_flags &= ~HFS_HAS_SPARSE_DEVICE; tmpvp = hfsmp->hfs_backingfs_rootvp; hfsmp->hfs_backingfs_rootvp = NULLVP; vnode_rele(tmpvp); } #endif /* HFS_SPARSE_DEV */ lck_mtx_destroy(&hfsmp->hfc_mutex, hfs_mutex_group); vnode_rele(hfsmp->hfs_devvp); hfs_delete_chash(hfsmp); FREE(hfsmp, M_HFSMNT); return (0); err_exit: if (started_tr) { hfs_end_transaction(hfsmp); } return retval; } /* * Return the root of a filesystem. */ static int hfs_vfs_root(struct mount *mp, struct vnode **vpp, __unused vfs_context_t context) { return hfs_vget(VFSTOHFS(mp), (cnid_t)kHFSRootFolderID, vpp, 1); } /* * Do operations associated with quotas */ #if !QUOTA static int hfs_quotactl(__unused struct mount *mp, __unused int cmds, __unused uid_t uid, __unused caddr_t datap, __unused vfs_context_t context) { return (ENOTSUP); } #else static int hfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t datap, vfs_context_t context) { struct proc *p = vfs_context_proc(context); int cmd, type, error; if (uid == ~0U) uid = vfs_context_ucred(context)->cr_ruid; cmd = cmds >> SUBCMDSHIFT; switch (cmd) { case Q_SYNC: case Q_QUOTASTAT: break; case Q_GETQUOTA: if (uid == vfs_context_ucred(context)->cr_ruid) break; /* fall through */ default: if ( (error = vfs_context_suser(context)) ) return (error); } type = cmds & SUBCMDMASK; if ((u_int)type >= MAXQUOTAS) return (EINVAL); if (vfs_busy(mp, LK_NOWAIT)) return (0); switch (cmd) { case Q_QUOTAON: error = hfs_quotaon(p, mp, type, datap); break; case Q_QUOTAOFF: error = hfs_quotaoff(p, mp, type); break; case Q_SETQUOTA: error = hfs_setquota(mp, uid, type, datap); break; case Q_SETUSE: error = hfs_setuse(mp, uid, type, datap); break; case Q_GETQUOTA: error = hfs_getquota(mp, uid, type, datap); break; case Q_SYNC: error = hfs_qsync(mp); break; case Q_QUOTASTAT: error = hfs_quotastat(mp, type, datap); break; default: error = EINVAL; break; } vfs_unbusy(mp); return (error); } #endif /* QUOTA */ /* Subtype is composite of bits */ #define HFS_SUBTYPE_JOURNALED 0x01 #define HFS_SUBTYPE_CASESENSITIVE 0x02 /* bits 2 - 6 reserved */ #define HFS_SUBTYPE_STANDARDHFS 0x80 /* * Get file system statistics. */ static int hfs_statfs(struct mount *mp, register struct vfsstatfs *sbp, __unused vfs_context_t context) { ExtendedVCB *vcb = VFSTOVCB(mp); struct hfsmount *hfsmp = VFSTOHFS(mp); u_int32_t freeCNIDs; u_int16_t subtype = 0; freeCNIDs = (u_int32_t)0xFFFFFFFF - (u_int32_t)vcb->vcbNxtCNID; sbp->f_bsize = (u_int32_t)vcb->blockSize; sbp->f_iosize = (size_t)cluster_max_io_size(mp, 0); sbp->f_blocks = (u_int64_t)((u_int32_t)vcb->totalBlocks); sbp->f_bfree = (u_int64_t)((u_int32_t )hfs_freeblks(hfsmp, 0)); sbp->f_bavail = (u_int64_t)((u_int32_t )hfs_freeblks(hfsmp, 1)); sbp->f_files = (u_int64_t)((u_int32_t )(vcb->totalBlocks - 2)); /* max files is constrained by total blocks */ sbp->f_ffree = (u_int64_t)((u_int32_t )(MIN(freeCNIDs, sbp->f_bavail))); /* * Subtypes (flavors) for HFS * 0: Mac OS Extended * 1: Mac OS Extended (Journaled) * 2: Mac OS Extended (Case Sensitive) * 3: Mac OS Extended (Case Sensitive, Journaled) * 4 - 127: Reserved * 128: Mac OS Standard * */ if (hfsmp->hfs_flags & HFS_STANDARD) { subtype = HFS_SUBTYPE_STANDARDHFS; } else /* HFS Plus */ { if (hfsmp->jnl) subtype |= HFS_SUBTYPE_JOURNALED; if (hfsmp->hfs_flags & HFS_CASE_SENSITIVE) subtype |= HFS_SUBTYPE_CASESENSITIVE; } sbp->f_fssubtype = subtype; return (0); } // // XXXdbg -- this is a callback to be used by the journal to // get meta data blocks flushed out to disk. // // XXXdbg -- be smarter and don't flush *every* block on each // call. try to only flush some so we don't wind up // being too synchronous. // __private_extern__ void hfs_sync_metadata(void *arg) { struct mount *mp = (struct mount *)arg; struct hfsmount *hfsmp; ExtendedVCB *vcb; buf_t bp; int retval; daddr64_t priIDSector; hfsmp = VFSTOHFS(mp); vcb = HFSTOVCB(hfsmp); // now make sure the super block is flushed priIDSector = (daddr64_t)((vcb->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size) + HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size)); retval = (int)buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(priIDSector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &bp); if ((retval != 0 ) && (retval != ENXIO)) { printf("hfs_sync_metadata: can't read volume header at %d! (retval 0x%x)\n", (int)priIDSector, retval); } if (retval == 0 && ((buf_flags(bp) & (B_DELWRI | B_LOCKED)) == B_DELWRI)) { buf_bwrite(bp); } else if (bp) { buf_brelse(bp); } // the alternate super block... // XXXdbg - we probably don't need to do this each and every time. // hfs_btreeio.c:FlushAlternate() should flag when it was // written... if (hfsmp->hfs_alt_id_sector) { retval = (int)buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(hfsmp->hfs_alt_id_sector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &bp); if (retval == 0 && ((buf_flags(bp) & (B_DELWRI | B_LOCKED)) == B_DELWRI)) { buf_bwrite(bp); } else if (bp) { buf_brelse(bp); } } } struct hfs_sync_cargs { kauth_cred_t cred; struct proc *p; int waitfor; int error; }; static int hfs_sync_callback(struct vnode *vp, void *cargs) { struct cnode *cp; struct hfs_sync_cargs *args; int error; args = (struct hfs_sync_cargs *)cargs; if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) != 0) { return (VNODE_RETURNED); } cp = VTOC(vp); if ((cp->c_flag & C_MODIFIED) || (cp->c_touch_acctime | cp->c_touch_chgtime | cp->c_touch_modtime) || vnode_hasdirtyblks(vp)) { error = hfs_fsync(vp, args->waitfor, 0, args->p); if (error) args->error = error; } hfs_unlock(cp); return (VNODE_RETURNED); } /* * Go through the disk queues to initiate sandbagged IO; * go through the inodes to write those that have been modified; * initiate the writing of the super block if it has been modified. * * Note: we are always called with the filesystem marked `MPBUSY'. */ static int hfs_sync(struct mount *mp, int waitfor, vfs_context_t context) { struct proc *p = vfs_context_proc(context); struct cnode *cp; struct hfsmount *hfsmp; ExtendedVCB *vcb; struct vnode *meta_vp[4]; int i; int error, allerror = 0; struct hfs_sync_cargs args; hfsmp = VFSTOHFS(mp); /* * hfs_changefs might be manipulating vnodes so back off */ if (hfsmp->hfs_flags & HFS_IN_CHANGEFS) return (0); if (hfsmp->hfs_flags & HFS_READ_ONLY) return (EROFS); /* skip over frozen volumes */ if (!lck_rw_try_lock_shared(&hfsmp->hfs_insync)) return 0; args.cred = kauth_cred_get(); args.waitfor = waitfor; args.p = p; args.error = 0; /* * hfs_sync_callback will be called for each vnode * hung off of this mount point... the vnode will be * properly referenced and unreferenced around the callback */ vnode_iterate(mp, 0, hfs_sync_callback, (void *)&args); if (args.error) allerror = args.error; vcb = HFSTOVCB(hfsmp); meta_vp[0] = vcb->extentsRefNum; meta_vp[1] = vcb->catalogRefNum; meta_vp[2] = vcb->allocationsRefNum; /* This is NULL for standard HFS */ meta_vp[3] = hfsmp->hfs_attribute_vp; /* Optional file */ /* Now sync our three metadata files */ for (i = 0; i < 4; ++i) { struct vnode *btvp; btvp = meta_vp[i];; if ((btvp==0) || (vnode_mount(btvp) != mp)) continue; /* XXX use hfs_systemfile_lock instead ? */ (void) hfs_lock(VTOC(btvp), HFS_EXCLUSIVE_LOCK); cp = VTOC(btvp); if (((cp->c_flag & C_MODIFIED) == 0) && (cp->c_touch_acctime == 0) && (cp->c_touch_chgtime == 0) && (cp->c_touch_modtime == 0) && vnode_hasdirtyblks(btvp) == 0) { hfs_unlock(VTOC(btvp)); continue; } error = vnode_get(btvp); if (error) { hfs_unlock(VTOC(btvp)); continue; } if ((error = hfs_fsync(btvp, waitfor, 0, p))) allerror = error; hfs_unlock(cp); vnode_put(btvp); }; /* * Force stale file system control information to be flushed. */ if (vcb->vcbSigWord == kHFSSigWord) { if ((error = VNOP_FSYNC(hfsmp->hfs_devvp, waitfor, context))) { allerror = error; } } #if QUOTA hfs_qsync(mp); #endif /* QUOTA */ hfs_hotfilesync(hfsmp, vfs_context_kernel()); /* * Write back modified superblock. */ if (IsVCBDirty(vcb)) { error = hfs_flushvolumeheader(hfsmp, waitfor, 0); if (error) allerror = error; } if (hfsmp->jnl) { hfs_journal_flush(hfsmp); } { clock_sec_t secs; clock_usec_t usecs; uint64_t now; clock_get_calendar_microtime(&secs, &usecs); now = ((uint64_t)secs * 1000000ULL) + (uint64_t)usecs; hfsmp->hfs_last_sync_time = now; } lck_rw_unlock_shared(&hfsmp->hfs_insync); return (allerror); } /* * File handle to vnode * * Have to be really careful about stale file handles: * - check that the cnode id is valid * - call hfs_vget() to get the locked cnode * - check for an unallocated cnode (i_mode == 0) * - check that the given client host has export rights and return * those rights via. exflagsp and credanonp */ static int hfs_fhtovp(struct mount *mp, int fhlen, unsigned char *fhp, struct vnode **vpp, __unused vfs_context_t context) { struct hfsfid *hfsfhp; struct vnode *nvp; int result; *vpp = NULL; hfsfhp = (struct hfsfid *)fhp; if (fhlen < (int)sizeof(struct hfsfid)) return (EINVAL); result = hfs_vget(VFSTOHFS(mp), ntohl(hfsfhp->hfsfid_cnid), &nvp, 0); if (result) { if (result == ENOENT) result = ESTALE; return result; } /* * We used to use the create time as the gen id of the file handle, * but it is not static enough because it can change at any point * via system calls. We still don't have another volume ID or other * unique identifier to use for a generation ID across reboots that * persists until the file is removed. Using only the CNID exposes * us to the potential wrap-around case, but as of 2/2008, it would take * over 2 months to wrap around if the machine did nothing but allocate * CNIDs. Using some kind of wrap counter would only be effective if * each file had the wrap counter associated with it. For now, * we use only the CNID to identify the file as it's good enough. */ *vpp = nvp; hfs_unlock(VTOC(nvp)); return (0); } /* * Vnode pointer to File handle */ /* ARGSUSED */ static int hfs_vptofh(struct vnode *vp, int *fhlenp, unsigned char *fhp, __unused vfs_context_t context) { struct cnode *cp; struct hfsfid *hfsfhp; if (ISHFS(VTOVCB(vp))) return (ENOTSUP); /* hfs standard is not exportable */ if (*fhlenp < (int)sizeof(struct hfsfid)) return (EOVERFLOW); cp = VTOC(vp); hfsfhp = (struct hfsfid *)fhp; /* only the CNID is used to identify the file now */ hfsfhp->hfsfid_cnid = htonl(cp->c_fileid); hfsfhp->hfsfid_gen = htonl(cp->c_fileid); *fhlenp = sizeof(struct hfsfid); return (0); } /* * Initial HFS filesystems, done only once. */ static int hfs_init(__unused struct vfsconf *vfsp) { static int done = 0; if (done) return (0); done = 1; hfs_chashinit(); hfs_converterinit(); BTReserveSetup(); hfs_lock_attr = lck_attr_alloc_init(); hfs_group_attr = lck_grp_attr_alloc_init(); hfs_mutex_group = lck_grp_alloc_init("hfs-mutex", hfs_group_attr); hfs_rwlock_group = lck_grp_alloc_init("hfs-rwlock", hfs_group_attr); #if HFS_COMPRESSION decmpfs_init(); #endif return (0); } static int hfs_getmountpoint(struct vnode *vp, struct hfsmount **hfsmpp) { struct hfsmount * hfsmp; char fstypename[MFSNAMELEN]; if (vp == NULL) return (EINVAL); if (!vnode_isvroot(vp)) return (EINVAL); vnode_vfsname(vp, fstypename); if (strncmp(fstypename, "hfs", sizeof(fstypename)) != 0) return (EINVAL); hfsmp = VTOHFS(vp); if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) return (EINVAL); *hfsmpp = hfsmp; return (0); } // XXXdbg #include <sys/filedesc.h> /* * HFS filesystem related variables. */ static int hfs_sysctl(int *name, __unused u_int namelen, user_addr_t oldp, size_t *oldlenp, user_addr_t newp, size_t newlen, vfs_context_t context) { struct proc *p = vfs_context_proc(context); int error; struct hfsmount *hfsmp; /* all sysctl names at this level are terminal */ if (name[0] == HFS_ENCODINGBIAS) { int bias; bias = hfs_getencodingbias(); error = sysctl_int(oldp, oldlenp, newp, newlen, &bias); if (error == 0 && newp) hfs_setencodingbias(bias); return (error); } else if (name[0] == HFS_EXTEND_FS) { u_int64_t newsize; vnode_t vp = vfs_context_cwd(context); if (newp == USER_ADDR_NULL || vp == NULLVP) return (EINVAL); if ((error = hfs_getmountpoint(vp, &hfsmp))) return (error); error = sysctl_quad(oldp, oldlenp, newp, newlen, (quad_t *)&newsize); if (error) return (error); error = hfs_extendfs(hfsmp, newsize, context); return (error); } else if (name[0] == HFS_ENCODINGHINT) { size_t bufsize; size_t bytes; u_int32_t hint; u_int16_t *unicode_name = NULL; char *filename = NULL; if ((newlen <= 0) || (newlen > MAXPATHLEN)) return (EINVAL); bufsize = MAX(newlen * 3, MAXPATHLEN); MALLOC(filename, char *, newlen, M_TEMP, M_WAITOK); if (filename == NULL) { error = ENOMEM; goto encodinghint_exit; } MALLOC(unicode_name, u_int16_t *, bufsize, M_TEMP, M_WAITOK); if (filename == NULL) { error = ENOMEM; goto encodinghint_exit; } error = copyin(newp, (caddr_t)filename, newlen); if (error == 0) { error = utf8_decodestr((u_int8_t *)filename, newlen - 1, unicode_name, &bytes, bufsize, 0, UTF_DECOMPOSED); if (error == 0) { hint = hfs_pickencoding(unicode_name, bytes / 2); error = sysctl_int(oldp, oldlenp, USER_ADDR_NULL, 0, (int32_t *)&hint); } } encodinghint_exit: if (unicode_name) FREE(unicode_name, M_TEMP); if (filename) FREE(filename, M_TEMP); return (error); } else if (name[0] == HFS_ENABLE_JOURNALING) { // make the file system journaled... vnode_t vp = vfs_context_cwd(context); vnode_t jvp; ExtendedVCB *vcb; struct cat_attr jnl_attr, jinfo_attr; struct cat_fork jnl_fork, jinfo_fork; void *jnl = NULL; int lockflags; /* Only root can enable journaling */ if (!is_suser()) { return (EPERM); } if (vp == NULLVP) return EINVAL; hfsmp = VTOHFS(vp); if (hfsmp->hfs_flags & HFS_READ_ONLY) { return EROFS; } if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) { printf("hfs: can't make a plain hfs volume journaled.\n"); return EINVAL; } if (hfsmp->jnl) { printf("hfs: volume @ mp %p is already journaled!\n", vnode_mount(vp)); return EAGAIN; } vcb = HFSTOVCB(hfsmp); lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_EXTENTS, HFS_EXCLUSIVE_LOCK); if (BTHasContiguousNodes(VTOF(vcb->catalogRefNum)) == 0 || BTHasContiguousNodes(VTOF(vcb->extentsRefNum)) == 0) { printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n"); hfs_systemfile_unlock(hfsmp, lockflags); return EINVAL; } hfs_systemfile_unlock(hfsmp, lockflags); // make sure these both exist! if ( GetFileInfo(vcb, kHFSRootFolderID, ".journal_info_block", &jinfo_attr, &jinfo_fork) == 0 || GetFileInfo(vcb, kHFSRootFolderID, ".journal", &jnl_attr, &jnl_fork) == 0) { return EINVAL; } hfs_sync(hfsmp->hfs_mp, MNT_WAIT, context); printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n", (off_t)name[2], (off_t)name[3]); // // XXXdbg - note that currently (Sept, 08) hfs_util does not support // enabling the journal on a separate device so it is safe // to just copy hfs_devvp here. If hfs_util gets the ability // to dynamically enable the journal on a separate device then // we will have to do the same thing as hfs_early_journal_init() // to locate and open the journal device. // jvp = hfsmp->hfs_devvp; jnl = journal_create(jvp, (off_t)name[2] * (off_t)HFSTOVCB(hfsmp)->blockSize + HFSTOVCB(hfsmp)->hfsPlusIOPosOffset, (off_t)((unsigned)name[3]), hfsmp->hfs_devvp, hfsmp->hfs_logical_block_size, 0, 0, hfs_sync_metadata, hfsmp->hfs_mp); if (jnl == NULL) { printf("hfs: FAILED to create the journal!\n"); if (jvp && jvp != hfsmp->hfs_devvp) { vnode_clearmountedon(jvp); VNOP_CLOSE(jvp, hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, vfs_context_kernel()); } jvp = NULL; return EINVAL; } hfs_global_exclusive_lock_acquire(hfsmp); /* * Flush all dirty metadata buffers. */ buf_flushdirtyblks(hfsmp->hfs_devvp, MNT_WAIT, 0, "hfs_sysctl"); buf_flushdirtyblks(hfsmp->hfs_extents_vp, MNT_WAIT, 0, "hfs_sysctl"); buf_flushdirtyblks(hfsmp->hfs_catalog_vp, MNT_WAIT, 0, "hfs_sysctl"); buf_flushdirtyblks(hfsmp->hfs_allocation_vp, MNT_WAIT, 0, "hfs_sysctl"); if (hfsmp->hfs_attribute_vp) buf_flushdirtyblks(hfsmp->hfs_attribute_vp, MNT_WAIT, 0, "hfs_sysctl"); HFSTOVCB(hfsmp)->vcbJinfoBlock = name[1]; HFSTOVCB(hfsmp)->vcbAtrb |= kHFSVolumeJournaledMask; hfsmp->jvp = jvp; hfsmp->jnl = jnl; // save this off for the hack-y check in hfs_remove() hfsmp->jnl_start = (u_int32_t)name[2]; hfsmp->jnl_size = (off_t)((unsigned)name[3]); hfsmp->hfs_jnlinfoblkid = jinfo_attr.ca_fileid; hfsmp->hfs_jnlfileid = jnl_attr.ca_fileid; vfs_setflags(hfsmp->hfs_mp, (u_int64_t)((unsigned int)MNT_JOURNALED)); hfs_global_exclusive_lock_release(hfsmp); hfs_flushvolumeheader(hfsmp, MNT_WAIT, 1); { fsid_t fsid; fsid.val[0] = (int32_t)hfsmp->hfs_raw_dev; fsid.val[1] = (int32_t)vfs_typenum(HFSTOVFS(hfsmp)); vfs_event_signal(&fsid, VQ_UPDATE, (intptr_t)NULL); } return 0; } else if (name[0] == HFS_DISABLE_JOURNALING) { // clear the journaling bit vnode_t vp = vfs_context_cwd(context); /* Only root can disable journaling */ if (!is_suser()) { return (EPERM); } if (vp == NULLVP) return EINVAL; hfsmp = VTOHFS(vp); /* * Disabling journaling is disallowed on volumes with directory hard links * because we have not tested the relevant code path. */ if (hfsmp->hfs_private_attr[DIR_HARDLINKS].ca_entries != 0){ printf("hfs: cannot disable journaling on volumes with directory hardlinks\n"); return EPERM; } printf("hfs: disabling journaling for mount @ %p\n", vnode_mount(vp)); hfs_global_exclusive_lock_acquire(hfsmp); // Lights out for you buddy! journal_close(hfsmp->jnl); hfsmp->jnl = NULL; if (hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) { vnode_clearmountedon(hfsmp->jvp); VNOP_CLOSE(hfsmp->jvp, hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, vfs_context_kernel()); vnode_put(hfsmp->jvp); } hfsmp->jvp = NULL; vfs_clearflags(hfsmp->hfs_mp, (u_int64_t)((unsigned int)MNT_JOURNALED)); hfsmp->jnl_start = 0; hfsmp->hfs_jnlinfoblkid = 0; hfsmp->hfs_jnlfileid = 0; HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeJournaledMask; hfs_global_exclusive_lock_release(hfsmp); hfs_flushvolumeheader(hfsmp, MNT_WAIT, 1); { fsid_t fsid; fsid.val[0] = (int32_t)hfsmp->hfs_raw_dev; fsid.val[1] = (int32_t)vfs_typenum(HFSTOVFS(hfsmp)); vfs_event_signal(&fsid, VQ_UPDATE, (intptr_t)NULL); } return 0; } else if (name[0] == HFS_GET_JOURNAL_INFO) { vnode_t vp = vfs_context_cwd(context); off_t jnl_start, jnl_size; if (vp == NULLVP) return EINVAL; /* 64-bit processes won't work with this sysctl -- can't fit a pointer into an int! */ if (proc_is64bit(current_proc())) return EINVAL; hfsmp = VTOHFS(vp); if (hfsmp->jnl == NULL) { jnl_start = 0; jnl_size = 0; } else { jnl_start = (off_t)(hfsmp->jnl_start * HFSTOVCB(hfsmp)->blockSize) + (off_t)HFSTOVCB(hfsmp)->hfsPlusIOPosOffset; jnl_size = (off_t)hfsmp->jnl_size; } if ((error = copyout((caddr_t)&jnl_start, CAST_USER_ADDR_T(name[1]), sizeof(off_t))) != 0) { return error; } if ((error = copyout((caddr_t)&jnl_size, CAST_USER_ADDR_T(name[2]), sizeof(off_t))) != 0) { return error; } return 0; } else if (name[0] == HFS_SET_PKG_EXTENSIONS) { return set_package_extensions_table((user_addr_t)((unsigned)name[1]), name[2], name[3]); } else if (name[0] == VFS_CTL_QUERY) { struct sysctl_req *req; union union_vfsidctl vc; struct mount *mp; struct vfsquery vq; req = CAST_DOWN(struct sysctl_req *, oldp); /* we're new style vfs sysctl. */ error = SYSCTL_IN(req, &vc, proc_is64bit(p)? sizeof(vc.vc64):sizeof(vc.vc32)); if (error) return (error); mp = vfs_getvfs(&vc.vc32.vc_fsid); /* works for 32 and 64 */ if (mp == NULL) return (ENOENT); hfsmp = VFSTOHFS(mp); bzero(&vq, sizeof(vq)); vq.vq_flags = hfsmp->hfs_notification_conditions; return SYSCTL_OUT(req, &vq, sizeof(vq));; } else if (name[0] == HFS_REPLAY_JOURNAL) { vnode_t devvp = NULL; int device_fd; if (namelen != 2) { return (EINVAL); } device_fd = name[1]; error = file_vnode(device_fd, &devvp); if (error) { return error; } error = vnode_getwithref(devvp); if (error) { file_drop(device_fd); return error; } error = hfs_journal_replay(devvp, context); file_drop(device_fd); vnode_put(devvp); return error; } return (ENOTSUP); } /* * hfs_vfs_vget is not static since it is used in hfs_readwrite.c to support * the build_path ioctl. We use it to leverage the code below that updates * the origin list cache if necessary */ int hfs_vfs_vget(struct mount *mp, ino64_t ino, struct vnode **vpp, __unused vfs_context_t context) { int error; int lockflags; struct hfsmount *hfsmp; hfsmp = VFSTOHFS(mp); error = hfs_vget(hfsmp, (cnid_t)ino, vpp, 1); if (error) return (error); /* * ADLs may need to have their origin state updated * since build_path needs a valid parent. The same is true * for hardlinked files as well. There isn't a race window here * in re-acquiring the cnode lock since we aren't pulling any data * out of the cnode; instead, we're going to the catalog. */ if ((VTOC(*vpp)->c_flag & C_HARDLINK) && (hfs_lock(VTOC(*vpp), HFS_EXCLUSIVE_LOCK) == 0)) { cnode_t *cp = VTOC(*vpp); struct cat_desc cdesc; if (!hfs_haslinkorigin(cp)) { lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK); error = cat_findname(hfsmp, (cnid_t)ino, &cdesc); hfs_systemfile_unlock(hfsmp, lockflags); if (error == 0) { if ((cdesc.cd_parentcnid != hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) && (cdesc.cd_parentcnid != hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid)) { hfs_savelinkorigin(cp, cdesc.cd_parentcnid); } cat_releasedesc(&cdesc); } } hfs_unlock(cp); } return (0); } /* * Look up an HFS object by ID. * * The object is returned with an iocount reference and the cnode locked. * * If the object is a file then it will represent the data fork. */ __private_extern__ int hfs_vget(struct hfsmount *hfsmp, cnid_t cnid, struct vnode **vpp, int skiplock) { struct vnode *vp = NULLVP; struct cat_desc cndesc; struct cat_attr cnattr; struct cat_fork cnfork; u_int32_t linkref = 0; int error; /* Check for cnids that should't be exported. */ if ((cnid < kHFSFirstUserCatalogNodeID) && (cnid != kHFSRootFolderID && cnid != kHFSRootParentID)) { return (ENOENT); } /* Don't export our private directories. */ if (cnid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid || cnid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) { return (ENOENT); } /* * Check the hash first */ vp = hfs_chash_getvnode(hfsmp, cnid, 0, skiplock); if (vp) { *vpp = vp; return(0); } bzero(&cndesc, sizeof(cndesc)); bzero(&cnattr, sizeof(cnattr)); bzero(&cnfork, sizeof(cnfork)); /* * Not in hash, lookup in catalog */ if (cnid == kHFSRootParentID) { static char hfs_rootname[] = "/"; cndesc.cd_nameptr = (const u_int8_t *)&hfs_rootname[0]; cndesc.cd_namelen = 1; cndesc.cd_parentcnid = kHFSRootParentID; cndesc.cd_cnid = kHFSRootFolderID; cndesc.cd_flags = CD_ISDIR; cnattr.ca_fileid = kHFSRootFolderID; cnattr.ca_linkcount = 1; cnattr.ca_entries = 1; cnattr.ca_dircount = 1; cnattr.ca_mode = (S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO); } else { int lockflags; cnid_t pid; const char *nameptr; lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK); error = cat_idlookup(hfsmp, cnid, 0, &cndesc, &cnattr, &cnfork); hfs_systemfile_unlock(hfsmp, lockflags); if (error) { *vpp = NULL; return (error); } /* * Check for a raw hardlink inode and save its linkref. */ pid = cndesc.cd_parentcnid; nameptr = (const char *)cndesc.cd_nameptr; if ((pid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid) && (bcmp(nameptr, HFS_INODE_PREFIX, HFS_INODE_PREFIX_LEN) == 0)) { linkref = strtoul(&nameptr[HFS_INODE_PREFIX_LEN], NULL, 10); } else if ((pid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) && (bcmp(nameptr, HFS_DIRINODE_PREFIX, HFS_DIRINODE_PREFIX_LEN) == 0)) { linkref = strtoul(&nameptr[HFS_DIRINODE_PREFIX_LEN], NULL, 10); } else if ((pid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid) && (bcmp(nameptr, HFS_DELETE_PREFIX, HFS_DELETE_PREFIX_LEN) == 0)) { *vpp = NULL; cat_releasedesc(&cndesc); return (ENOENT); /* open unlinked file */ } } /* * Finish initializing cnode descriptor for hardlinks. * * We need a valid name and parent for reverse lookups. */ if (linkref) { cnid_t nextlinkid; cnid_t prevlinkid; struct cat_desc linkdesc; int lockflags; cnattr.ca_linkref = linkref; /* * Pick up the first link in the chain and get a descriptor for it. * This allows blind volfs paths to work for hardlinks. */ if ((hfs_lookuplink(hfsmp, linkref, &prevlinkid, &nextlinkid) == 0) && (nextlinkid != 0)) { lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK); error = cat_findname(hfsmp, nextlinkid, &linkdesc); hfs_systemfile_unlock(hfsmp, lockflags); if (error == 0) { cat_releasedesc(&cndesc); bcopy(&linkdesc, &cndesc, sizeof(linkdesc)); } } } if (linkref) { error = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cnfork, &vp); if (error == 0) { VTOC(vp)->c_flag |= C_HARDLINK; vnode_setmultipath(vp); } } else { struct componentname cn; /* Supply hfs_getnewvnode with a component name. */ MALLOC_ZONE(cn.cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); cn.cn_nameiop = LOOKUP; cn.cn_flags = ISLASTCN | HASBUF; cn.cn_context = NULL; cn.cn_pnlen = MAXPATHLEN; cn.cn_nameptr = cn.cn_pnbuf; cn.cn_namelen = cndesc.cd_namelen; cn.cn_hash = 0; cn.cn_consume = 0; bcopy(cndesc.cd_nameptr, cn.cn_nameptr, cndesc.cd_namelen + 1); error = hfs_getnewvnode(hfsmp, NULLVP, &cn, &cndesc, 0, &cnattr, &cnfork, &vp); if (error == 0 && (VTOC(vp)->c_flag & C_HARDLINK)) { hfs_savelinkorigin(VTOC(vp), cndesc.cd_parentcnid); } FREE_ZONE(cn.cn_pnbuf, cn.cn_pnlen, M_NAMEI); } cat_releasedesc(&cndesc); *vpp = vp; if (vp && skiplock) { hfs_unlock(VTOC(vp)); } return (error); } /* * Flush out all the files in a filesystem. */ static int #if QUOTA hfs_flushfiles(struct mount *mp, int flags, struct proc *p) #else hfs_flushfiles(struct mount *mp, int flags, __unused struct proc *p) #endif /* QUOTA */ { struct hfsmount *hfsmp; struct vnode *skipvp = NULLVP; int error; #if QUOTA int quotafilecnt; int i; #endif hfsmp = VFSTOHFS(mp); #if QUOTA /* * The open quota files have an indirect reference on * the root directory vnode. We must account for this * extra reference when doing the intial vflush. */ quotafilecnt = 0; if (((unsigned int)vfs_flags(mp)) & MNT_QUOTA) { /* Find out how many quota files we have open. */ for (i = 0; i < MAXQUOTAS; i++) { if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP) ++quotafilecnt; } /* Obtain the root vnode so we can skip over it. */ skipvp = hfs_chash_getvnode(hfsmp, kHFSRootFolderID, 0, 0); } #endif /* QUOTA */ error = vflush(mp, skipvp, SKIPSYSTEM | SKIPSWAP | flags); if (error != 0) return(error); error = vflush(mp, skipvp, SKIPSYSTEM | flags); #if QUOTA if (((unsigned int)vfs_flags(mp)) & MNT_QUOTA) { if (skipvp) { /* * See if there are additional references on the * root vp besides the ones obtained from the open * quota files and the hfs_chash_getvnode call above. */ if ((error == 0) && (vnode_isinuse(skipvp, quotafilecnt))) { error = EBUSY; /* root directory is still open */ } hfs_unlock(VTOC(skipvp)); vnode_put(skipvp); } if (error && (flags & FORCECLOSE) == 0) return (error); for (i = 0; i < MAXQUOTAS; i++) { if (hfsmp->hfs_qfiles[i].qf_vp == NULLVP) continue; hfs_quotaoff(p, mp, i); } error = vflush(mp, NULLVP, SKIPSYSTEM | flags); } #endif /* QUOTA */ return (error); } /* * Update volume encoding bitmap (HFS Plus only) */ __private_extern__ void hfs_setencodingbits(struct hfsmount *hfsmp, u_int32_t encoding) { #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */ #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */ u_int32_t index; switch (encoding) { case kTextEncodingMacUkrainian: index = kIndexMacUkrainian; break; case kTextEncodingMacFarsi: index = kIndexMacFarsi; break; default: index = encoding; break; } if (index < 64 && (hfsmp->encodingsBitmap & (u_int64_t)(1ULL << index)) == 0) { HFS_MOUNT_LOCK(hfsmp, TRUE) hfsmp->encodingsBitmap |= (u_int64_t)(1ULL << index); MarkVCBDirty(hfsmp); HFS_MOUNT_UNLOCK(hfsmp, TRUE); } } /* * Update volume stats * * On journal volumes this will cause a volume header flush */ __private_extern__ int hfs_volupdate(struct hfsmount *hfsmp, enum volop op, int inroot) { struct timeval tv; microtime(&tv); lck_mtx_lock(&hfsmp->hfs_mutex); MarkVCBDirty(hfsmp); hfsmp->hfs_mtime = tv.tv_sec; switch (op) { case VOL_UPDATE: break; case VOL_MKDIR: if (hfsmp->hfs_dircount != 0xFFFFFFFF) ++hfsmp->hfs_dircount; if (inroot && hfsmp->vcbNmRtDirs != 0xFFFF) ++hfsmp->vcbNmRtDirs; break; case VOL_RMDIR: if (hfsmp->hfs_dircount != 0) --hfsmp->hfs_dircount; if (inroot && hfsmp->vcbNmRtDirs != 0xFFFF) --hfsmp->vcbNmRtDirs; break; case VOL_MKFILE: if (hfsmp->hfs_filecount != 0xFFFFFFFF) ++hfsmp->hfs_filecount; if (inroot && hfsmp->vcbNmFls != 0xFFFF) ++hfsmp->vcbNmFls; break; case VOL_RMFILE: if (hfsmp->hfs_filecount != 0) --hfsmp->hfs_filecount; if (inroot && hfsmp->vcbNmFls != 0xFFFF) --hfsmp->vcbNmFls; break; } lck_mtx_unlock(&hfsmp->hfs_mutex); if (hfsmp->jnl) { hfs_flushvolumeheader(hfsmp, 0, 0); } return (0); } static int hfs_flushMDB(struct hfsmount *hfsmp, int waitfor, int altflush) { ExtendedVCB *vcb = HFSTOVCB(hfsmp); struct filefork *fp; HFSMasterDirectoryBlock *mdb; struct buf *bp = NULL; int retval; int sectorsize; ByteCount namelen; sectorsize = hfsmp->hfs_logical_block_size; retval = (int)buf_bread(hfsmp->hfs_devvp, (daddr64_t)HFS_PRI_SECTOR(sectorsize), sectorsize, NOCRED, &bp); if (retval) { if (bp) buf_brelse(bp); return retval; } lck_mtx_lock(&hfsmp->hfs_mutex); mdb = (HFSMasterDirectoryBlock *)(buf_dataptr(bp) + HFS_PRI_OFFSET(sectorsize)); mdb->drCrDate = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbCrDate))); mdb->drLsMod = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbLsMod))); mdb->drAtrb = SWAP_BE16 (vcb->vcbAtrb); mdb->drNmFls = SWAP_BE16 (vcb->vcbNmFls); mdb->drAllocPtr = SWAP_BE16 (vcb->nextAllocation); mdb->drClpSiz = SWAP_BE32 (vcb->vcbClpSiz); mdb->drNxtCNID = SWAP_BE32 (vcb->vcbNxtCNID); mdb->drFreeBks = SWAP_BE16 (vcb->freeBlocks); namelen = strlen((char *)vcb->vcbVN); retval = utf8_to_hfs(vcb, namelen, vcb->vcbVN, mdb->drVN); /* Retry with MacRoman in case that's how it was exported. */ if (retval) retval = utf8_to_mac_roman(namelen, vcb->vcbVN, mdb->drVN); mdb->drVolBkUp = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbVolBkUp))); mdb->drWrCnt = SWAP_BE32 (vcb->vcbWrCnt); mdb->drNmRtDirs = SWAP_BE16 (vcb->vcbNmRtDirs); mdb->drFilCnt = SWAP_BE32 (vcb->vcbFilCnt); mdb->drDirCnt = SWAP_BE32 (vcb->vcbDirCnt); bcopy(vcb->vcbFndrInfo, mdb->drFndrInfo, sizeof(mdb->drFndrInfo)); fp = VTOF(vcb->extentsRefNum); mdb->drXTExtRec[0].startBlock = SWAP_BE16 (fp->ff_extents[0].startBlock); mdb->drXTExtRec[0].blockCount = SWAP_BE16 (fp->ff_extents[0].blockCount); mdb->drXTExtRec[1].startBlock = SWAP_BE16 (fp->ff_extents[1].startBlock); mdb->drXTExtRec[1].blockCount = SWAP_BE16 (fp->ff_extents[1].blockCount); mdb->drXTExtRec[2].startBlock = SWAP_BE16 (fp->ff_extents[2].startBlock); mdb->drXTExtRec[2].blockCount = SWAP_BE16 (fp->ff_extents[2].blockCount); mdb->drXTFlSize = SWAP_BE32 (fp->ff_blocks * vcb->blockSize); mdb->drXTClpSiz = SWAP_BE32 (fp->ff_clumpsize); FTOC(fp)->c_flag &= ~C_MODIFIED; fp = VTOF(vcb->catalogRefNum); mdb->drCTExtRec[0].startBlock = SWAP_BE16 (fp->ff_extents[0].startBlock); mdb->drCTExtRec[0].blockCount = SWAP_BE16 (fp->ff_extents[0].blockCount); mdb->drCTExtRec[1].startBlock = SWAP_BE16 (fp->ff_extents[1].startBlock); mdb->drCTExtRec[1].blockCount = SWAP_BE16 (fp->ff_extents[1].blockCount); mdb->drCTExtRec[2].startBlock = SWAP_BE16 (fp->ff_extents[2].startBlock); mdb->drCTExtRec[2].blockCount = SWAP_BE16 (fp->ff_extents[2].blockCount); mdb->drCTFlSize = SWAP_BE32 (fp->ff_blocks * vcb->blockSize); mdb->drCTClpSiz = SWAP_BE32 (fp->ff_clumpsize); FTOC(fp)->c_flag &= ~C_MODIFIED; MarkVCBClean( vcb ); lck_mtx_unlock(&hfsmp->hfs_mutex); /* If requested, flush out the alternate MDB */ if (altflush) { struct buf *alt_bp = NULL; if (buf_meta_bread(hfsmp->hfs_devvp, hfsmp->hfs_alt_id_sector, sectorsize, NOCRED, &alt_bp) == 0) { bcopy(mdb, (char *)buf_dataptr(alt_bp) + HFS_ALT_OFFSET(sectorsize), kMDBSize); (void) VNOP_BWRITE(alt_bp); } else if (alt_bp) buf_brelse(alt_bp); } if (waitfor != MNT_WAIT) buf_bawrite(bp); else retval = VNOP_BWRITE(bp); return (retval); } /* * Flush any dirty in-memory mount data to the on-disk * volume header. * * Note: the on-disk volume signature is intentionally * not flushed since the on-disk "H+" and "HX" signatures * are always stored in-memory as "H+". */ __private_extern__ int hfs_flushvolumeheader(struct hfsmount *hfsmp, int waitfor, int altflush) { ExtendedVCB *vcb = HFSTOVCB(hfsmp); struct filefork *fp; HFSPlusVolumeHeader *volumeHeader, *altVH; int retval; struct buf *bp, *alt_bp; int i; daddr64_t priIDSector; int critical; u_int16_t signature; u_int16_t hfsversion; if (hfsmp->hfs_flags & HFS_READ_ONLY) { return(0); } if (hfsmp->hfs_flags & HFS_STANDARD) { return hfs_flushMDB(hfsmp, waitfor, altflush); } critical = altflush; priIDSector = (daddr64_t)((vcb->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size) + HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size)); if (hfs_start_transaction(hfsmp) != 0) { return EINVAL; } bp = NULL; alt_bp = NULL; retval = (int)buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(priIDSector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &bp); if (retval) { printf("hfs: err %d reading VH blk (%s)\n", retval, vcb->vcbVN); goto err_exit; } volumeHeader = (HFSPlusVolumeHeader *)((char *)buf_dataptr(bp) + HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size)); /* * Sanity check what we just read. If it's bad, try the alternate * instead. */ signature = SWAP_BE16 (volumeHeader->signature); hfsversion = SWAP_BE16 (volumeHeader->version); if ((signature != kHFSPlusSigWord && signature != kHFSXSigWord) || (hfsversion < kHFSPlusVersion) || (hfsversion > 100) || (SWAP_BE32 (volumeHeader->blockSize) != vcb->blockSize)) { printf("hfs: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d%s\n", vcb->vcbVN, signature, hfsversion, SWAP_BE32 (volumeHeader->blockSize), hfsmp->hfs_alt_id_sector ? "; trying alternate" : ""); hfs_mark_volume_inconsistent(hfsmp); if (hfsmp->hfs_alt_id_sector) { retval = buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(hfsmp->hfs_alt_id_sector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &alt_bp); if (retval) { printf("hfs: err %d reading alternate VH (%s)\n", retval, vcb->vcbVN); goto err_exit; } altVH = (HFSPlusVolumeHeader *)((char *)buf_dataptr(alt_bp) + HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size)); signature = SWAP_BE16(altVH->signature); hfsversion = SWAP_BE16(altVH->version); if ((signature != kHFSPlusSigWord && signature != kHFSXSigWord) || (hfsversion < kHFSPlusVersion) || (kHFSPlusVersion > 100) || (SWAP_BE32(altVH->blockSize) != vcb->blockSize)) { printf("hfs: corrupt alternate VH on %s, sig 0x%04x, ver %d, blksize %d\n", vcb->vcbVN, signature, hfsversion, SWAP_BE32(altVH->blockSize)); retval = EIO; goto err_exit; } /* The alternate is plausible, so use it. */ bcopy(altVH, volumeHeader, kMDBSize); buf_brelse(alt_bp); alt_bp = NULL; } else { /* No alternate VH, nothing more we can do. */ retval = EIO; goto err_exit; } } if (hfsmp->jnl) { journal_modify_block_start(hfsmp->jnl, bp); } /* * For embedded HFS+ volumes, update create date if it changed * (ie from a setattrlist call) */ if ((vcb->hfsPlusIOPosOffset != 0) && (SWAP_BE32 (volumeHeader->createDate) != vcb->localCreateDate)) { struct buf *bp2; HFSMasterDirectoryBlock *mdb; retval = (int)buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size), hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &bp2); if (retval) { if (bp2) buf_brelse(bp2); retval = 0; } else { mdb = (HFSMasterDirectoryBlock *)(buf_dataptr(bp2) + HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size)); if ( SWAP_BE32 (mdb->drCrDate) != vcb->localCreateDate ) { if (hfsmp->jnl) { journal_modify_block_start(hfsmp->jnl, bp2); } mdb->drCrDate = SWAP_BE32 (vcb->localCreateDate); /* pick up the new create date */ if (hfsmp->jnl) { journal_modify_block_end(hfsmp->jnl, bp2, NULL, NULL); } else { (void) VNOP_BWRITE(bp2); /* write out the changes */ } } else { buf_brelse(bp2); /* just release it */ } } } lck_mtx_lock(&hfsmp->hfs_mutex); /* Note: only update the lower 16 bits worth of attributes */ volumeHeader->attributes = SWAP_BE32 (vcb->vcbAtrb); volumeHeader->journalInfoBlock = SWAP_BE32 (vcb->vcbJinfoBlock); if (hfsmp->jnl) { volumeHeader->lastMountedVersion = SWAP_BE32 (kHFSJMountVersion); } else { volumeHeader->lastMountedVersion = SWAP_BE32 (kHFSPlusMountVersion); } volumeHeader->createDate = SWAP_BE32 (vcb->localCreateDate); /* volume create date is in local time */ volumeHeader->modifyDate = SWAP_BE32 (to_hfs_time(vcb->vcbLsMod)); volumeHeader->backupDate = SWAP_BE32 (to_hfs_time(vcb->vcbVolBkUp)); volumeHeader->fileCount = SWAP_BE32 (vcb->vcbFilCnt); volumeHeader->folderCount = SWAP_BE32 (vcb->vcbDirCnt); volumeHeader->totalBlocks = SWAP_BE32 (vcb->totalBlocks); volumeHeader->freeBlocks = SWAP_BE32 (vcb->freeBlocks); volumeHeader->nextAllocation = SWAP_BE32 (vcb->nextAllocation); volumeHeader->rsrcClumpSize = SWAP_BE32 (vcb->vcbClpSiz); volumeHeader->dataClumpSize = SWAP_BE32 (vcb->vcbClpSiz); volumeHeader->nextCatalogID = SWAP_BE32 (vcb->vcbNxtCNID); volumeHeader->writeCount = SWAP_BE32 (vcb->vcbWrCnt); volumeHeader->encodingsBitmap = SWAP_BE64 (vcb->encodingsBitmap); if (bcmp(vcb->vcbFndrInfo, volumeHeader->finderInfo, sizeof(volumeHeader->finderInfo)) != 0) { bcopy(vcb->vcbFndrInfo, volumeHeader->finderInfo, sizeof(volumeHeader->finderInfo)); critical = 1; } /* * System files are only dirty when altflush is set. */ if (altflush == 0) { goto done; } /* Sync Extents over-flow file meta data */ fp = VTOF(vcb->extentsRefNum); if (FTOC(fp)->c_flag & C_MODIFIED) { for (i = 0; i < kHFSPlusExtentDensity; i++) { volumeHeader->extentsFile.extents[i].startBlock = SWAP_BE32 (fp->ff_extents[i].startBlock); volumeHeader->extentsFile.extents[i].blockCount = SWAP_BE32 (fp->ff_extents[i].blockCount); } volumeHeader->extentsFile.logicalSize = SWAP_BE64 (fp->ff_size); volumeHeader->extentsFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); volumeHeader->extentsFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); FTOC(fp)->c_flag &= ~C_MODIFIED; } /* Sync Catalog file meta data */ fp = VTOF(vcb->catalogRefNum); if (FTOC(fp)->c_flag & C_MODIFIED) { for (i = 0; i < kHFSPlusExtentDensity; i++) { volumeHeader->catalogFile.extents[i].startBlock = SWAP_BE32 (fp->ff_extents[i].startBlock); volumeHeader->catalogFile.extents[i].blockCount = SWAP_BE32 (fp->ff_extents[i].blockCount); } volumeHeader->catalogFile.logicalSize = SWAP_BE64 (fp->ff_size); volumeHeader->catalogFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); volumeHeader->catalogFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); FTOC(fp)->c_flag &= ~C_MODIFIED; } /* Sync Allocation file meta data */ fp = VTOF(vcb->allocationsRefNum); if (FTOC(fp)->c_flag & C_MODIFIED) { for (i = 0; i < kHFSPlusExtentDensity; i++) { volumeHeader->allocationFile.extents[i].startBlock = SWAP_BE32 (fp->ff_extents[i].startBlock); volumeHeader->allocationFile.extents[i].blockCount = SWAP_BE32 (fp->ff_extents[i].blockCount); } volumeHeader->allocationFile.logicalSize = SWAP_BE64 (fp->ff_size); volumeHeader->allocationFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); volumeHeader->allocationFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); FTOC(fp)->c_flag &= ~C_MODIFIED; } /* Sync Attribute file meta data */ if (hfsmp->hfs_attribute_vp) { fp = VTOF(hfsmp->hfs_attribute_vp); for (i = 0; i < kHFSPlusExtentDensity; i++) { volumeHeader->attributesFile.extents[i].startBlock = SWAP_BE32 (fp->ff_extents[i].startBlock); volumeHeader->attributesFile.extents[i].blockCount = SWAP_BE32 (fp->ff_extents[i].blockCount); } FTOC(fp)->c_flag &= ~C_MODIFIED; volumeHeader->attributesFile.logicalSize = SWAP_BE64 (fp->ff_size); volumeHeader->attributesFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); volumeHeader->attributesFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); } /* Sync Startup file meta data */ if (hfsmp->hfs_startup_vp) { fp = VTOF(hfsmp->hfs_startup_vp); if (FTOC(fp)->c_flag & C_MODIFIED) { for (i = 0; i < kHFSPlusExtentDensity; i++) { volumeHeader->startupFile.extents[i].startBlock = SWAP_BE32 (fp->ff_extents[i].startBlock); volumeHeader->startupFile.extents[i].blockCount = SWAP_BE32 (fp->ff_extents[i].blockCount); } volumeHeader->startupFile.logicalSize = SWAP_BE64 (fp->ff_size); volumeHeader->startupFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); volumeHeader->startupFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); FTOC(fp)->c_flag &= ~C_MODIFIED; } } done: MarkVCBClean(hfsmp); lck_mtx_unlock(&hfsmp->hfs_mutex); /* If requested, flush out the alternate volume header */ if (altflush && hfsmp->hfs_alt_id_sector) { if (buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(hfsmp->hfs_alt_id_sector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &alt_bp) == 0) { if (hfsmp->jnl) { journal_modify_block_start(hfsmp->jnl, alt_bp); } bcopy(volumeHeader, (char *)buf_dataptr(alt_bp) + HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size), kMDBSize); if (hfsmp->jnl) { journal_modify_block_end(hfsmp->jnl, alt_bp, NULL, NULL); } else { (void) VNOP_BWRITE(alt_bp); } } else if (alt_bp) buf_brelse(alt_bp); } if (hfsmp->jnl) { journal_modify_block_end(hfsmp->jnl, bp, NULL, NULL); } else { if (waitfor != MNT_WAIT) buf_bawrite(bp); else { retval = VNOP_BWRITE(bp); /* When critical data changes, flush the device cache */ if (critical && (retval == 0)) { (void) VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, NULL); } } } hfs_end_transaction(hfsmp); return (retval); err_exit: if (alt_bp) buf_brelse(alt_bp); if (bp) buf_brelse(bp); hfs_end_transaction(hfsmp); return retval; } /* * Extend a file system. */ __private_extern__ int hfs_extendfs(struct hfsmount *hfsmp, u_int64_t newsize, vfs_context_t context) { struct proc *p = vfs_context_proc(context); kauth_cred_t cred = vfs_context_ucred(context); struct vnode *vp; struct vnode *devvp; struct buf *bp; struct filefork *fp = NULL; ExtendedVCB *vcb; struct cat_fork forkdata; u_int64_t oldsize; u_int64_t newblkcnt; u_int64_t prev_phys_block_count; u_int32_t addblks; u_int64_t sectorcnt; u_int32_t sectorsize; u_int32_t phys_sectorsize; daddr64_t prev_alt_sector; daddr_t bitmapblks; int lockflags; int error; int64_t oldBitmapSize; Boolean usedExtendFileC = false; devvp = hfsmp->hfs_devvp; vcb = HFSTOVCB(hfsmp); /* * - HFS Plus file systems only. * - Journaling must be enabled. * - No embedded volumes. */ if ((vcb->vcbSigWord == kHFSSigWord) || (hfsmp->jnl == NULL) || (vcb->hfsPlusIOPosOffset != 0)) { return (EPERM); } /* * If extending file system by non-root, then verify * ownership and check permissions. */ if (suser(cred, NULL)) { error = hfs_vget(hfsmp, kHFSRootFolderID, &vp, 0); if (error) return (error); error = hfs_owner_rights(hfsmp, VTOC(vp)->c_uid, cred, p, 0); if (error == 0) { error = hfs_write_access(vp, cred, p, false); } hfs_unlock(VTOC(vp)); vnode_put(vp); if (error) return (error); error = vnode_authorize(devvp, NULL, KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA, context); if (error) return (error); } if (VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)§orsize, 0, context)) { return (ENXIO); } if (sectorsize != hfsmp->hfs_logical_block_size) { return (ENXIO); } if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)§orcnt, 0, context)) { return (ENXIO); } if ((sectorsize * sectorcnt) < newsize) { printf("hfs_extendfs: not enough space on device\n"); return (ENOSPC); } error = VNOP_IOCTL(devvp, DKIOCGETPHYSICALBLOCKSIZE, (caddr_t)&phys_sectorsize, 0, context); if (error) { if ((error != ENOTSUP) && (error != ENOTTY)) { return (ENXIO); } /* If ioctl is not supported, force physical and logical sector size to be same */ phys_sectorsize = sectorsize; } oldsize = (u_int64_t)hfsmp->totalBlocks * (u_int64_t)hfsmp->blockSize; /* * Validate new size. */ if ((newsize <= oldsize) || (newsize % sectorsize) || (newsize % phys_sectorsize)) { printf("hfs_extendfs: invalid size\n"); return (EINVAL); } newblkcnt = newsize / vcb->blockSize; if (newblkcnt > (u_int64_t)0xFFFFFFFF) return (EOVERFLOW); addblks = newblkcnt - vcb->totalBlocks; printf("hfs_extendfs: growing %s by %d blocks\n", vcb->vcbVN, addblks); /* * Enclose changes inside a transaction. */ if (hfs_start_transaction(hfsmp) != 0) { return (EINVAL); } /* * Note: we take the attributes lock in case we have an attribute data vnode * which needs to change size. */ lockflags = hfs_systemfile_lock(hfsmp, SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK); vp = vcb->allocationsRefNum; fp = VTOF(vp); bcopy(&fp->ff_data, &forkdata, sizeof(forkdata)); /* * Calculate additional space required (if any) by allocation bitmap. */ oldBitmapSize = fp->ff_size; bitmapblks = roundup((newblkcnt+7) / 8, vcb->vcbVBMIOSize) / vcb->blockSize; if (bitmapblks > (daddr_t)fp->ff_blocks) bitmapblks -= fp->ff_blocks; else bitmapblks = 0; if (bitmapblks > 0) { daddr64_t blkno; daddr_t blkcnt; off_t bytesAdded; /* * Get the bitmap's current size (in allocation blocks) so we know * where to start zero filling once the new space is added. We've * got to do this before the bitmap is grown. */ blkno = (daddr64_t)fp->ff_blocks; /* * Try to grow the allocation file in the normal way, using allocation * blocks already existing in the file system. This way, we might be * able to grow the bitmap contiguously, or at least in the metadata * zone. */ error = ExtendFileC(vcb, fp, bitmapblks * vcb->blockSize, 0, kEFAllMask | kEFNoClumpMask | kEFReserveMask | kEFMetadataMask, &bytesAdded); if (error == 0) { usedExtendFileC = true; } else { /* * If the above allocation failed, fall back to allocating the new * extent of the bitmap from the space we're going to add. Since those * blocks don't yet belong to the file system, we have to update the * extent list directly, and manually adjust the file size. */ bytesAdded = 0; error = AddFileExtent(vcb, fp, vcb->totalBlocks, bitmapblks); if (error) { printf("hfs_extendfs: error %d adding extents\n", error); goto out; } fp->ff_blocks += bitmapblks; VTOC(vp)->c_blocks = fp->ff_blocks; VTOC(vp)->c_flag |= C_MODIFIED; } /* * Update the allocation file's size to include the newly allocated * blocks. Note that ExtendFileC doesn't do this, which is why this * statement is outside the above "if" statement. */ fp->ff_size += (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize; /* * Zero out the new bitmap blocks. */ { bp = NULL; blkcnt = bitmapblks; while (blkcnt > 0) { error = (int)buf_meta_bread(vp, blkno, vcb->blockSize, NOCRED, &bp); if (error) { if (bp) { buf_brelse(bp); } break; } bzero((char *)buf_dataptr(bp), vcb->blockSize); buf_markaged(bp); error = (int)buf_bwrite(bp); if (error) break; --blkcnt; ++blkno; } } if (error) { printf("hfs_extendfs: error %d clearing blocks\n", error); goto out; } /* * Mark the new bitmap space as allocated. * * Note that ExtendFileC will have marked any blocks it allocated, so * this is only needed if we used AddFileExtent. Also note that this * has to come *after* the zero filling of new blocks in the case where * we used AddFileExtent (since the part of the bitmap we're touching * is in those newly allocated blocks). */ if (!usedExtendFileC) { error = BlockMarkAllocated(vcb, vcb->totalBlocks, bitmapblks); if (error) { printf("hfs_extendfs: error %d setting bitmap\n", error); goto out; } vcb->freeBlocks -= bitmapblks; } } /* * Mark the new alternate VH as allocated. */ if (vcb->blockSize == 512) error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 2, 2); else error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 1, 1); if (error) { printf("hfs_extendfs: error %d setting bitmap (VH)\n", error); goto out; } /* * Mark the old alternate VH as free. */ if (vcb->blockSize == 512) (void) BlockMarkFree(vcb, vcb->totalBlocks - 2, 2); else (void) BlockMarkFree(vcb, vcb->totalBlocks - 1, 1); /* * Adjust file system variables for new space. */ prev_phys_block_count = hfsmp->hfs_logical_block_count; prev_alt_sector = hfsmp->hfs_alt_id_sector; vcb->totalBlocks += addblks; vcb->freeBlocks += addblks; hfsmp->hfs_logical_block_count = newsize / sectorsize; hfsmp->hfs_alt_id_sector = (hfsmp->hfsPlusIOPosOffset / sectorsize) + HFS_ALT_SECTOR(sectorsize, hfsmp->hfs_logical_block_count); MarkVCBDirty(vcb); error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH); if (error) { printf("hfs_extendfs: couldn't flush volume headers (%d)", error); /* * Restore to old state. */ if (usedExtendFileC) { (void) TruncateFileC(vcb, fp, oldBitmapSize, false); } else { fp->ff_blocks -= bitmapblks; fp->ff_size -= (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize; /* * No need to mark the excess blocks free since those bitmap blocks * are no longer part of the bitmap. But we do need to undo the * effect of the "vcb->freeBlocks -= bitmapblks" above. */ vcb->freeBlocks += bitmapblks; } vcb->totalBlocks -= addblks; vcb->freeBlocks -= addblks; hfsmp->hfs_logical_block_count = prev_phys_block_count; hfsmp->hfs_alt_id_sector = prev_alt_sector; MarkVCBDirty(vcb); if (vcb->blockSize == 512) (void) BlockMarkAllocated(vcb, vcb->totalBlocks - 2, 2); else (void) BlockMarkAllocated(vcb, vcb->totalBlocks - 1, 1); goto out; } /* * Invalidate the old alternate volume header. */ bp = NULL; if (prev_alt_sector) { if (buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(prev_alt_sector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &bp) == 0) { journal_modify_block_start(hfsmp->jnl, bp); bzero((char *)buf_dataptr(bp) + HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size), kMDBSize); journal_modify_block_end(hfsmp->jnl, bp, NULL, NULL); } else if (bp) { buf_brelse(bp); } } /* * TODO: Adjust the size of the metadata zone based on new volume size? */ /* * Adjust the size of hfsmp->hfs_attrdata_vp */ if (hfsmp->hfs_attrdata_vp) { struct cnode *attr_cp; struct filefork *attr_fp; if (vnode_get(hfsmp->hfs_attrdata_vp) == 0) { attr_cp = VTOC(hfsmp->hfs_attrdata_vp); attr_fp = VTOF(hfsmp->hfs_attrdata_vp); attr_cp->c_blocks = newblkcnt; attr_fp->ff_blocks = newblkcnt; attr_fp->ff_extents[0].blockCount = newblkcnt; attr_fp->ff_size = (off_t) newblkcnt * hfsmp->blockSize; ubc_setsize(hfsmp->hfs_attrdata_vp, attr_fp->ff_size); vnode_put(hfsmp->hfs_attrdata_vp); } } out: if (error && fp) { /* Restore allocation fork. */ bcopy(&forkdata, &fp->ff_data, sizeof(forkdata)); VTOC(vp)->c_blocks = fp->ff_blocks; } /* Regardless of whether or not the totalblocks actually increased, we should reset the allocLimit field. If it changed, it will get updated; if not, it will remain the same. */ hfsmp->allocLimit = vcb->totalBlocks; hfs_systemfile_unlock(hfsmp, lockflags); hfs_end_transaction(hfsmp); return (error); } #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL) /* * Truncate a file system (while still mounted). */ __private_extern__ int hfs_truncatefs(struct hfsmount *hfsmp, u_int64_t newsize, vfs_context_t context) { struct buf *bp = NULL; u_int64_t oldsize; u_int32_t newblkcnt; u_int32_t reclaimblks = 0; int lockflags = 0; int transaction_begun = 0; int error; lck_mtx_lock(&hfsmp->hfs_mutex); if (hfsmp->hfs_flags & HFS_RESIZE_IN_PROGRESS) { lck_mtx_unlock(&hfsmp->hfs_mutex); return (EALREADY); } hfsmp->hfs_flags |= HFS_RESIZE_IN_PROGRESS; hfsmp->hfs_resize_filesmoved = 0; hfsmp->hfs_resize_totalfiles = 0; lck_mtx_unlock(&hfsmp->hfs_mutex); /* * - Journaled HFS Plus volumes only. * - No embedded volumes. */ if ((hfsmp->jnl == NULL) || (hfsmp->hfsPlusIOPosOffset != 0)) { error = EPERM; goto out; } oldsize = (u_int64_t)hfsmp->totalBlocks * (u_int64_t)hfsmp->blockSize; newblkcnt = newsize / hfsmp->blockSize; reclaimblks = hfsmp->totalBlocks - newblkcnt; /* Make sure new size is valid. */ if ((newsize < HFS_MIN_SIZE) || (newsize >= oldsize) || (newsize % hfsmp->hfs_logical_block_size) || (newsize % hfsmp->hfs_physical_block_size)) { printf ("hfs_truncatefs: invalid size\n"); error = EINVAL; goto out; } /* Make sure there's enough space to work with. */ if (reclaimblks >= hfs_freeblks(hfsmp, 1)) { printf("hfs_truncatefs: insufficient space (need %u blocks; have %u blocks)\n", reclaimblks, hfs_freeblks(hfsmp, 1)); error = ENOSPC; goto out; } /* Start with a clean journal. */ hfs_journal_flush(hfsmp); if (hfs_start_transaction(hfsmp) != 0) { error = EINVAL; goto out; } transaction_begun = 1; /* * Prevent new allocations from using the part we're trying to truncate. * * NOTE: allocLimit is set to the allocation block number where the new * alternate volume header will be. That way there will be no files to * interfere with allocating the new alternate volume header, and no files * in the allocation blocks beyond (i.e. the blocks we're trying to * truncate away. */ lck_mtx_lock(&hfsmp->hfs_mutex); if (hfsmp->blockSize == 512) hfsmp->allocLimit = newblkcnt - 2; else hfsmp->allocLimit = newblkcnt - 1; hfsmp->freeBlocks -= reclaimblks; lck_mtx_unlock(&hfsmp->hfs_mutex); /* * Look for files that have blocks at or beyond the location of the * new alternate volume header. */ if (hfs_isallocated(hfsmp, hfsmp->allocLimit, reclaimblks)) { /* * hfs_reclaimspace will use separate transactions when * relocating files (so we don't overwhelm the journal). */ hfs_end_transaction(hfsmp); transaction_begun = 0; /* Attempt to reclaim some space. */ if (hfs_reclaimspace(hfsmp, hfsmp->allocLimit, reclaimblks, context) != 0) { printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp->vcbVN); error = ENOSPC; goto out; } if (hfs_start_transaction(hfsmp) != 0) { error = EINVAL; goto out; } transaction_begun = 1; /* Check if we're clear now. */ if (hfs_isallocated(hfsmp, hfsmp->allocLimit, reclaimblks)) { printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp->vcbVN); error = EAGAIN; /* tell client to try again */ goto out; } } /* * Note: we take the attributes lock in case we have an attribute data vnode * which needs to change size. */ lockflags = hfs_systemfile_lock(hfsmp, SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK); /* * Mark the old alternate volume header as free. * We don't bother shrinking allocation bitmap file. */ if (hfsmp->blockSize == 512) (void) BlockMarkFree(hfsmp, hfsmp->totalBlocks - 2, 2); else (void) BlockMarkFree(hfsmp, hfsmp->totalBlocks - 1, 1); /* * Allocate last 1KB for alternate volume header. */ error = BlockMarkAllocated(hfsmp, hfsmp->allocLimit, (hfsmp->blockSize == 512) ? 2 : 1); if (error) { printf("hfs_truncatefs: Error %d allocating new alternate volume header\n", error); goto out; } /* * Invalidate the existing alternate volume header. * * Don't include this in a transaction (don't call journal_modify_block) * since this block will be outside of the truncated file system! */ if (hfsmp->hfs_alt_id_sector) { if (buf_meta_bread(hfsmp->hfs_devvp, HFS_PHYSBLK_ROUNDDOWN(hfsmp->hfs_alt_id_sector, hfsmp->hfs_log_per_phys), hfsmp->hfs_physical_block_size, NOCRED, &bp) == 0) { bzero((void*)((char *)buf_dataptr(bp) + HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size)), kMDBSize); (void) VNOP_BWRITE(bp); } else if (bp) { buf_brelse(bp); } bp = NULL; } /* Log successful shrinking. */ printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n", hfsmp->vcbVN, newblkcnt, hfsmp->totalBlocks); /* * Adjust file system variables and flush them to disk. */ hfsmp->totalBlocks = newblkcnt; hfsmp->hfs_logical_block_count = newsize / hfsmp->hfs_logical_block_size; hfsmp->hfs_alt_id_sector = HFS_ALT_SECTOR(hfsmp->hfs_logical_block_size, hfsmp->hfs_logical_block_count); MarkVCBDirty(hfsmp); error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH); if (error) panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error); /* * TODO: Adjust the size of the metadata zone based on new volume size? */ /* * Adjust the size of hfsmp->hfs_attrdata_vp */ if (hfsmp->hfs_attrdata_vp) { struct cnode *cp; struct filefork *fp; if (vnode_get(hfsmp->hfs_attrdata_vp) == 0) { cp = VTOC(hfsmp->hfs_attrdata_vp); fp = VTOF(hfsmp->hfs_attrdata_vp); cp->c_blocks = newblkcnt; fp->ff_blocks = newblkcnt; fp->ff_extents[0].blockCount = newblkcnt; fp->ff_size = (off_t) newblkcnt * hfsmp->blockSize; ubc_setsize(hfsmp->hfs_attrdata_vp, fp->ff_size); vnode_put(hfsmp->hfs_attrdata_vp); } } out: if (error) hfsmp->freeBlocks += reclaimblks; lck_mtx_lock(&hfsmp->hfs_mutex); hfsmp->allocLimit = hfsmp->totalBlocks; if (hfsmp->nextAllocation >= hfsmp->allocLimit) hfsmp->nextAllocation = hfsmp->hfs_metazone_end + 1; hfsmp->hfs_flags &= ~HFS_RESIZE_IN_PROGRESS; lck_mtx_unlock(&hfsmp->hfs_mutex); if (lockflags) { hfs_systemfile_unlock(hfsmp, lockflags); } if (transaction_begun) { hfs_end_transaction(hfsmp); hfs_journal_flush(hfsmp); } return (error); } /* * Invalidate the physical block numbers associated with buffer cache blocks * in the given extent of the given vnode. */ struct hfs_inval_blk_no { daddr64_t sectorStart; daddr64_t sectorCount; }; static int hfs_invalidate_block_numbers_callback(buf_t bp, void *args_in) { daddr64_t blkno; struct hfs_inval_blk_no *args; blkno = buf_blkno(bp); args = args_in; if (blkno >= args->sectorStart && blkno < args->sectorStart+args->sectorCount) buf_setblkno(bp, buf_lblkno(bp)); return BUF_RETURNED; } static void hfs_invalidate_sectors(struct vnode *vp, daddr64_t sectorStart, daddr64_t sectorCount) { struct hfs_inval_blk_no args; args.sectorStart = sectorStart; args.sectorCount = sectorCount; buf_iterate(vp, hfs_invalidate_block_numbers_callback, BUF_SCAN_DIRTY|BUF_SCAN_CLEAN, &args); } /* * Copy the contents of an extent to a new location. Also invalidates the * physical block number of any buffer cache block in the copied extent * (so that if the block is written, it will go through VNOP_BLOCKMAP to * determine the new physical block number). */ static int hfs_copy_extent( struct hfsmount *hfsmp, struct vnode *vp, /* The file whose extent is being copied. */ u_int32_t oldStart, /* The start of the source extent. */ u_int32_t newStart, /* The start of the destination extent. */ u_int32_t blockCount, /* The number of allocation blocks to copy. */ vfs_context_t context) { int err = 0; size_t bufferSize; void *buffer = NULL; struct vfsioattr ioattr; buf_t bp = NULL; off_t resid; size_t ioSize; u_int32_t ioSizeSectors; /* Device sectors in this I/O */ daddr64_t srcSector, destSector; u_int32_t sectorsPerBlock = hfsmp->blockSize / hfsmp->hfs_logical_block_size; /* * Sanity check that we have locked the vnode of the file we're copying. * * But since hfs_systemfile_lock() doesn't actually take the lock on * the allocation file if a journal is active, ignore the check if the * file being copied is the allocation file. */ struct cnode *cp = VTOC(vp); if (cp != hfsmp->hfs_allocation_cp && cp->c_lockowner != current_thread()) panic("hfs_copy_extent: vp=%p (cp=%p) not owned?\n", vp, cp); /* * Wait for any in-progress writes to this vnode to complete, so that we'll * be copying consistent bits. (Otherwise, it's possible that an async * write will complete to the old extent after we read from it. That * could lead to corruption.) */ err = vnode_waitforwrites(vp, 0, 0, 0, "hfs_copy_extent"); if (err) { printf("hfs_copy_extent: Error %d from vnode_waitforwrites\n", err); return err; } /* * Determine the I/O size to use * * NOTE: Many external drives will result in an ioSize of 128KB. * TODO: Should we use a larger buffer, doing several consecutive * reads, then several consecutive writes? */ vfs_ioattr(hfsmp->hfs_mp, &ioattr); bufferSize = MIN(ioattr.io_maxreadcnt, ioattr.io_maxwritecnt); if (kmem_alloc(kernel_map, (vm_offset_t*) &buffer, bufferSize)) return ENOMEM; /* Get a buffer for doing the I/O */ bp = buf_alloc(hfsmp->hfs_devvp); buf_setdataptr(bp, (uintptr_t)buffer); resid = (off_t) blockCount * (off_t) hfsmp->blockSize; srcSector = (daddr64_t) oldStart * hfsmp->blockSize / hfsmp->hfs_logical_block_size; destSector = (daddr64_t) newStart * hfsmp->blockSize / hfsmp->hfs_logical_block_size; while (resid > 0) { ioSize = MIN(bufferSize, (size_t) resid); ioSizeSectors = ioSize / hfsmp->hfs_logical_block_size; /* Prepare the buffer for reading */ buf_reset(bp, B_READ); buf_setsize(bp, ioSize); buf_setcount(bp, ioSize); buf_setblkno(bp, srcSector); buf_setlblkno(bp, srcSector); /* Do the read */ err = VNOP_STRATEGY(bp); if (!err) err = buf_biowait(bp); if (err) { printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (read)\n", err); break; } /* Prepare the buffer for writing */ buf_reset(bp, B_WRITE); buf_setsize(bp, ioSize); buf_setcount(bp, ioSize); buf_setblkno(bp, destSector); buf_setlblkno(bp, destSector); if (journal_uses_fua(hfsmp->jnl)) buf_markfua(bp); /* Do the write */ vnode_startwrite(hfsmp->hfs_devvp); err = VNOP_STRATEGY(bp); if (!err) err = buf_biowait(bp); if (err) { printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (write)\n", err); break; } resid -= ioSize; srcSector += ioSizeSectors; destSector += ioSizeSectors; } if (bp) buf_free(bp); if (buffer) kmem_free(kernel_map, (vm_offset_t)buffer, bufferSize); /* Make sure all writes have been flushed to disk. */ if (!journal_uses_fua(hfsmp->jnl)) { err = VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, context); if (err) { printf("hfs_copy_extent: DKIOCSYNCHRONIZECACHE failed (%d)\n", err); err = 0; /* Don't fail the copy. */ } } if (!err) hfs_invalidate_sectors(vp, (daddr64_t)oldStart*sectorsPerBlock, (daddr64_t)blockCount*sectorsPerBlock); return err; } /* * Reclaim space at the end of a volume, used by a given system file. * * This routine attempts to move any extent which contains allocation blocks * at or after "startblk." A separate transaction is used to do the move. * The contents of any moved extents are read and written via the volume's * device vnode -- NOT via "vp." During the move, moved blocks which are part * of a transaction have their physical block numbers invalidated so they will * eventually be written to their new locations. * * This routine can be used to move overflow extents for the allocation file. * * Inputs: * hfsmp The volume being resized. * startblk Blocks >= this allocation block need to be moved. * locks Which locks need to be taken for the given system file. * vp The vnode for the system file. * * Outputs: * moved Set to true if any extents were moved. */ static int hfs_relocate_callback(__unused HFSPlusExtentKey *key, HFSPlusExtentRecord *record, HFSPlusExtentRecord *state) { bcopy(state, record, sizeof(HFSPlusExtentRecord)); return 0; } static int hfs_reclaim_sys_file(struct hfsmount *hfsmp, struct vnode *vp, u_long startblk, int locks, Boolean *moved, vfs_context_t context) { int error; int lockflags; int i; u_long datablks; u_long block; u_int32_t oldStartBlock; u_int32_t newStartBlock; u_int32_t blockCount; struct filefork *fp; /* If there is no vnode for this file, then there's nothing to do. */ if (vp == NULL) return 0; /* printf("hfs_reclaim_sys_file: %.*s\n", VTOC(vp)->c_desc.cd_namelen, VTOC(vp)->c_desc.cd_nameptr); */ /* We always need the allocation bitmap and extents B-tree */ locks |= SFL_BITMAP | SFL_EXTENTS; error = hfs_start_transaction(hfsmp); if (error) { printf("hfs_reclaim_sys_file: hfs_start_transaction returned %d\n", error); return error; } lockflags = hfs_systemfile_lock(hfsmp, locks, HFS_EXCLUSIVE_LOCK); fp = VTOF(vp); datablks = 0; /* Relocate non-overflow extents */ for (i = 0; i < kHFSPlusExtentDensity; ++i) { if (fp->ff_extents[i].blockCount == 0) break; oldStartBlock = fp->ff_extents[i].startBlock; blockCount = fp->ff_extents[i].blockCount; datablks += blockCount; block = oldStartBlock + blockCount; if (block > startblk) { error = BlockAllocate(hfsmp, 1, blockCount, blockCount, true, true, &newStartBlock, &blockCount); if (error) { printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error); goto fail; } if (blockCount != fp->ff_extents[i].blockCount) { printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount, fp->ff_extents[i].blockCount); goto free_fail; } error = hfs_copy_extent(hfsmp, vp, oldStartBlock, newStartBlock, blockCount, context); if (error) { printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error); goto free_fail; } fp->ff_extents[i].startBlock = newStartBlock; VTOC(vp)->c_flag |= C_MODIFIED; *moved = true; error = BlockDeallocate(hfsmp, oldStartBlock, blockCount); if (error) { /* TODO: Mark volume inconsistent? */ printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error); goto fail; } error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH); if (error) { /* TODO: Mark volume inconsistent? */ printf("hfs_reclaim_sys_file: hfs_flushvolumeheader returned %d\n", error); goto fail; } } } /* Relocate overflow extents (if any) */ if (i == kHFSPlusExtentDensity && fp->ff_blocks > datablks) { struct BTreeIterator *iterator = NULL; struct FSBufferDescriptor btdata; HFSPlusExtentRecord record; HFSPlusExtentKey *key; FCB *fcb; u_int32_t fileID; u_int8_t forktype; forktype = VNODE_IS_RSRC(vp) ? 0xFF : 0; fileID = VTOC(vp)->c_cnid; if (kmem_alloc(kernel_map, (vm_offset_t*) &iterator, sizeof(*iterator))) { printf("hfs_reclaim_sys_file: kmem_alloc failed!\n"); error = ENOMEM; goto fail; } bzero(iterator, sizeof(*iterator)); key = (HFSPlusExtentKey *) &iterator->key; key->keyLength = kHFSPlusExtentKeyMaximumLength; key->forkType = forktype; key->fileID = fileID; key->startBlock = datablks; btdata.bufferAddress = &record; btdata.itemSize = sizeof(record); btdata.itemCount = 1; fcb = VTOF(hfsmp->hfs_extents_vp); error = BTSearchRecord(fcb, iterator, &btdata, NULL, iterator); while (error == 0) { /* Stop when we encounter a different file or fork. */ if ((key->fileID != fileID) || (key->forkType != forktype)) { break; } /* * Check if the file overlaps target space. */ for (i = 0; i < kHFSPlusExtentDensity; ++i) { if (record[i].blockCount == 0) { goto overflow_done; } oldStartBlock = record[i].startBlock; blockCount = record[i].blockCount; block = oldStartBlock + blockCount; if (block > startblk) { error = BlockAllocate(hfsmp, 1, blockCount, blockCount, true, true, &newStartBlock, &blockCount); if (error) { printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error); goto overflow_done; } if (blockCount != record[i].blockCount) { printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount, fp->ff_extents[i].blockCount); kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator)); goto free_fail; } error = hfs_copy_extent(hfsmp, vp, oldStartBlock, newStartBlock, blockCount, context); if (error) { printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error); kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator)); goto free_fail; } record[i].startBlock = newStartBlock; VTOC(vp)->c_flag |= C_MODIFIED; *moved = true; /* * NOTE: To support relocating overflow extents of the * allocation file, we must update the BTree record BEFORE * deallocating the old extent so that BlockDeallocate will * use the extent's new location to calculate physical block * numbers. (This is for the case where the old extent's * bitmap bits actually reside in the extent being moved.) */ error = BTUpdateRecord(fcb, iterator, (IterateCallBackProcPtr) hfs_relocate_callback, &record); if (error) { /* TODO: Mark volume inconsistent? */ printf("hfs_reclaim_sys_file: BTUpdateRecord returned %d\n", error); goto overflow_done; } error = BlockDeallocate(hfsmp, oldStartBlock, blockCount); if (error) { /* TODO: Mark volume inconsistent? */ printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error); goto overflow_done; } } } /* Look for more records. */ error = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL); if (error == btNotFound) { error = 0; break; } } overflow_done: kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator)); if (error) { goto fail; } } hfs_systemfile_unlock(hfsmp, lockflags); error = hfs_end_transaction(hfsmp); if (error) { printf("hfs_reclaim_sys_file: hfs_end_transaction returned %d\n", error); } return error; free_fail: (void) BlockDeallocate(hfsmp, newStartBlock, blockCount); fail: (void) hfs_systemfile_unlock(hfsmp, lockflags); (void) hfs_end_transaction(hfsmp); return error; } /* * This journal_relocate callback updates the journal info block to point * at the new journal location. This write must NOT be done using the * transaction. We must write the block immediately. We must also force * it to get to the media so that the new journal location will be seen by * the replay code before we can safely let journaled blocks be written * to their normal locations. * * The tests for journal_uses_fua below are mildly hacky. Since the journal * and the file system are both on the same device, I'm leveraging what * the journal has decided about FUA. */ struct hfs_journal_relocate_args { struct hfsmount *hfsmp; vfs_context_t context; u_int32_t newStartBlock; }; static errno_t hfs_journal_relocate_callback(void *_args) { int error; struct hfs_journal_relocate_args *args = _args; struct hfsmount *hfsmp = args->hfsmp; buf_t bp; JournalInfoBlock *jibp; error = buf_meta_bread(hfsmp->hfs_devvp, hfsmp->vcbJinfoBlock * (hfsmp->blockSize/hfsmp->hfs_logical_block_size), hfsmp->blockSize, vfs_context_ucred(args->context), &bp); if (error) { printf("hfs_reclaim_journal_file: failed to read JIB (%d)\n", error); return error; } jibp = (JournalInfoBlock*) buf_dataptr(bp); jibp->offset = SWAP_BE64((u_int64_t)args->newStartBlock * hfsmp->blockSize); jibp->size = SWAP_BE64(hfsmp->jnl_size); if (journal_uses_fua(hfsmp->jnl)) buf_markfua(bp); error = buf_bwrite(bp); if (error) { printf("hfs_reclaim_journal_file: failed to write JIB (%d)\n", error); return error; } if (!journal_uses_fua(hfsmp->jnl)) { error = VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, args->context); if (error) { printf("hfs_reclaim_journal_file: DKIOCSYNCHRONIZECACHE failed (%d)\n", error); error = 0; /* Don't fail the operation. */ } } return error; } static int hfs_reclaim_journal_file(struct hfsmount *hfsmp, vfs_context_t context) { int error; int lockflags; u_int32_t newStartBlock; u_int32_t oldBlockCount; u_int32_t newBlockCount; struct cat_desc journal_desc; struct cat_attr journal_attr; struct cat_fork journal_fork; struct hfs_journal_relocate_args callback_args; error = hfs_start_transaction(hfsmp); if (error) { printf("hfs_reclaim_journal_file: hfs_start_transaction returned %d\n", error); return error; } lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_BITMAP, HFS_EXCLUSIVE_LOCK); oldBlockCount = hfsmp->jnl_size / hfsmp->blockSize; /* TODO: Allow the journal to change size based on the new volume size. */ error = BlockAllocate(hfsmp, 1, oldBlockCount, oldBlockCount, true, true, &newStartBlock, &newBlockCount); if (error) { printf("hfs_reclaim_journal_file: BlockAllocate returned %d\n", error); goto fail; } if (newBlockCount != oldBlockCount) { printf("hfs_reclaim_journal_file: newBlockCount != oldBlockCount (%u, %u)\n", newBlockCount, oldBlockCount); goto free_fail; } error = BlockDeallocate(hfsmp, hfsmp->jnl_start, oldBlockCount); if (error) { printf("hfs_reclaim_journal_file: BlockDeallocate returned %d\n", error); goto free_fail; } /* Update the catalog record for .journal */ error = cat_idlookup(hfsmp, hfsmp->hfs_jnlfileid, 1, &journal_desc, &journal_attr, &journal_fork); if (error) { printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error); goto free_fail; } journal_fork.cf_size = newBlockCount * hfsmp->blockSize; journal_fork.cf_extents[0].startBlock = newStartBlock; journal_fork.cf_extents[0].blockCount = newBlockCount; journal_fork.cf_blocks = newBlockCount; error = cat_update(hfsmp, &journal_desc, &journal_attr, &journal_fork, NULL); cat_releasedesc(&journal_desc); /* all done with cat descriptor */ if (error) { printf("hfs_reclaim_journal_file: cat_update returned %d\n", error); goto free_fail; } callback_args.hfsmp = hfsmp; callback_args.context = context; callback_args.newStartBlock = newStartBlock; error = journal_relocate(hfsmp->jnl, (off_t)newStartBlock*hfsmp->blockSize, (off_t)newBlockCount*hfsmp->blockSize, 0, hfs_journal_relocate_callback, &callback_args); if (error) { /* NOTE: journal_relocate will mark the journal invalid. */ printf("hfs_reclaim_journal_file: journal_relocate returned %d\n", error); goto fail; } hfsmp->jnl_start = newStartBlock; hfsmp->jnl_size = (off_t)newBlockCount * hfsmp->blockSize; hfs_systemfile_unlock(hfsmp, lockflags); error = hfs_end_transaction(hfsmp); if (error) { printf("hfs_reclaim_journal_file: hfs_end_transaction returned %d\n", error); } return error; free_fail: (void) BlockDeallocate(hfsmp, newStartBlock, newBlockCount); fail: hfs_systemfile_unlock(hfsmp, lockflags); (void) hfs_end_transaction(hfsmp); return error; } /* * Move the journal info block to a new location. We have to make sure the * new copy of the journal info block gets to the media first, then change * the field in the volume header and the catalog record. */ static int hfs_reclaim_journal_info_block(struct hfsmount *hfsmp, vfs_context_t context) { int error; int lockflags; u_int32_t newBlock; u_int32_t blockCount; struct cat_desc jib_desc; struct cat_attr jib_attr; struct cat_fork jib_fork; buf_t old_bp, new_bp; error = hfs_start_transaction(hfsmp); if (error) { printf("hfs_reclaim_journal_info_block: hfs_start_transaction returned %d\n", error); return error; } lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_BITMAP, HFS_EXCLUSIVE_LOCK); error = BlockAllocate(hfsmp, 1, 1, 1, true, true, &newBlock, &blockCount); if (error) { printf("hfs_reclaim_journal_info_block: BlockAllocate returned %d\n", error); goto fail; } if (blockCount != 1) { printf("hfs_reclaim_journal_info_block: blockCount != 1 (%u)\n", blockCount); goto free_fail; } error = BlockDeallocate(hfsmp, hfsmp->vcbJinfoBlock, 1); if (error) { printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error); goto free_fail; } /* Copy the old journal info block content to the new location */ error = buf_meta_bread(hfsmp->hfs_devvp, hfsmp->vcbJinfoBlock * (hfsmp->blockSize/hfsmp->hfs_logical_block_size), hfsmp->blockSize, vfs_context_ucred(context), &old_bp); if (error) { printf("hfs_reclaim_journal_info_block: failed to read JIB (%d)\n", error); goto free_fail; } new_bp = buf_getblk(hfsmp->hfs_devvp, newBlock * (hfsmp->blockSize/hfsmp->hfs_logical_block_size), hfsmp->blockSize, 0, 0, BLK_META); bcopy((char*)buf_dataptr(old_bp), (char*)buf_dataptr(new_bp), hfsmp->blockSize); buf_brelse(old_bp); if (journal_uses_fua(hfsmp->jnl)) buf_markfua(new_bp); error = buf_bwrite(new_bp); if (error) { printf("hfs_reclaim_journal_info_block: failed to write new JIB (%d)\n", error); goto free_fail; } if (!journal_uses_fua(hfsmp->jnl)) { error = VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, context); if (error) { printf("hfs_reclaim_journal_info_block: DKIOCSYNCHRONIZECACHE failed (%d)\n", error); /* Don't fail the operation. */ } } /* Update the catalog record for .journal_info_block */ error = cat_idlookup(hfsmp, hfsmp->hfs_jnlinfoblkid, 1, &jib_desc, &jib_attr, &jib_fork); if (error) { printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error); goto fail; } jib_fork.cf_size = hfsmp->blockSize; jib_fork.cf_extents[0].startBlock = newBlock; jib_fork.cf_extents[0].blockCount = 1; jib_fork.cf_blocks = 1; error = cat_update(hfsmp, &jib_desc, &jib_attr, &jib_fork, NULL); cat_releasedesc(&jib_desc); /* all done with cat descriptor */ if (error) { printf("hfs_reclaim_journal_info_block: cat_update returned %d\n", error); goto fail; } /* Update the pointer to the journal info block in the volume header. */ hfsmp->vcbJinfoBlock = newBlock; error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH); if (error) { printf("hfs_reclaim_journal_info_block: hfs_flushvolumeheader returned %d\n", error); goto fail; } hfs_systemfile_unlock(hfsmp, lockflags); error = hfs_end_transaction(hfsmp); if (error) { printf("hfs_reclaim_journal_info_block: hfs_end_transaction returned %d\n", error); } error = hfs_journal_flush(hfsmp); if (error) { printf("hfs_reclaim_journal_info_block: journal_flush returned %d\n", error); } return error; free_fail: (void) BlockDeallocate(hfsmp, newBlock, blockCount); fail: hfs_systemfile_unlock(hfsmp, lockflags); (void) hfs_end_transaction(hfsmp); return error; } /* * Reclaim space at the end of a file system. */ static int hfs_reclaimspace(struct hfsmount *hfsmp, u_int32_t startblk, u_int32_t reclaimblks, vfs_context_t context) { struct vnode *vp = NULL; FCB *fcb; struct BTreeIterator * iterator = NULL; struct FSBufferDescriptor btdata; struct HFSPlusCatalogFile filerec; u_int32_t saved_next_allocation; cnid_t * cnidbufp; size_t cnidbufsize; int filecnt = 0; int maxfilecnt; u_int32_t block; u_int32_t datablks; u_int32_t rsrcblks; u_int32_t blkstomove = 0; int lockflags; int i; int error; int lastprogress = 0; Boolean system_file_moved = false; /* Relocate extents of the Allocation file if they're in the way. */ error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_allocation_vp, startblk, SFL_BITMAP, &system_file_moved, context); if (error) { printf("hfs_reclaimspace: reclaim allocation file returned %d\n", error); return error; } /* Relocate extents of the Extents B-tree if they're in the way. */ error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_extents_vp, startblk, SFL_EXTENTS, &system_file_moved, context); if (error) { printf("hfs_reclaimspace: reclaim extents b-tree returned %d\n", error); return error; } /* Relocate extents of the Catalog B-tree if they're in the way. */ error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_catalog_vp, startblk, SFL_CATALOG, &system_file_moved, context); if (error) { printf("hfs_reclaimspace: reclaim catalog b-tree returned %d\n", error); return error; } /* Relocate extents of the Attributes B-tree if they're in the way. */ error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_attribute_vp, startblk, SFL_ATTRIBUTE, &system_file_moved, context); if (error) { printf("hfs_reclaimspace: reclaim attribute b-tree returned %d\n", error); return error; } /* Relocate extents of the Startup File if there is one and they're in the way. */ error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_startup_vp, startblk, SFL_STARTUP, &system_file_moved, context); if (error) { printf("hfs_reclaimspace: reclaim startup file returned %d\n", error); return error; } /* * We need to make sure the alternate volume header gets flushed if we moved * any extents in the volume header. But we need to do that before * shrinking the size of the volume, or else the journal code will panic * with an invalid (too large) block number. * * Note that system_file_moved will be set if ANY extent was moved, even * if it was just an overflow extent. In this case, the journal_flush isn't * strictly required, but shouldn't hurt. */ if (system_file_moved) hfs_journal_flush(hfsmp); if (hfsmp->jnl_start + (hfsmp->jnl_size / hfsmp->blockSize) > startblk) { error = hfs_reclaim_journal_file(hfsmp, context); if (error) { printf("hfs_reclaimspace: hfs_reclaim_journal_file failed (%d)\n", error); return error; } } if (hfsmp->vcbJinfoBlock >= startblk) { error = hfs_reclaim_journal_info_block(hfsmp, context); if (error) { printf("hfs_reclaimspace: hfs_reclaim_journal_info_block failed (%d)\n", error); return error; } } /* For now move a maximum of 250,000 files. */ maxfilecnt = MIN(hfsmp->hfs_filecount, 250000); maxfilecnt = MIN((u_int32_t)maxfilecnt, reclaimblks); cnidbufsize = maxfilecnt * sizeof(cnid_t); if (kmem_alloc(kernel_map, (vm_offset_t *)&cnidbufp, cnidbufsize)) { return (ENOMEM); } if (kmem_alloc(kernel_map, (vm_offset_t *)&iterator, sizeof(*iterator))) { kmem_free(kernel_map, (vm_offset_t)cnidbufp, cnidbufsize); return (ENOMEM); } saved_next_allocation = hfsmp->nextAllocation; HFS_UPDATE_NEXT_ALLOCATION(hfsmp, hfsmp->hfs_metazone_start); fcb = VTOF(hfsmp->hfs_catalog_vp); bzero(iterator, sizeof(*iterator)); btdata.bufferAddress = &filerec; btdata.itemSize = sizeof(filerec); btdata.itemCount = 1; /* Keep the Catalog and extents files locked during iteration. */ lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_EXTENTS, HFS_SHARED_LOCK); error = BTIterateRecord(fcb, kBTreeFirstRecord, iterator, NULL, NULL); if (error) { goto end_iteration; } /* * Iterate over all the catalog records looking for files * that overlap into the space we're trying to free up. */ for (filecnt = 0; filecnt < maxfilecnt; ) { error = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL); if (error) { if (error == fsBTRecordNotFoundErr || error == fsBTEndOfIterationErr) { error = 0; } break; } if (filerec.recordType != kHFSPlusFileRecord) { continue; } datablks = rsrcblks = 0; /* * Check if either fork overlaps target space. */ for (i = 0; i < kHFSPlusExtentDensity; ++i) { if (filerec.dataFork.extents[i].blockCount != 0) { datablks += filerec.dataFork.extents[i].blockCount; block = filerec.dataFork.extents[i].startBlock + filerec.dataFork.extents[i].blockCount; if (block >= startblk) { if ((filerec.fileID == hfsmp->hfs_jnlfileid) || (filerec.fileID == hfsmp->hfs_jnlinfoblkid)) { printf("hfs_reclaimspace: cannot move active journal\n"); error = EPERM; goto end_iteration; } cnidbufp[filecnt++] = filerec.fileID; blkstomove += filerec.dataFork.totalBlocks; break; } } if (filerec.resourceFork.extents[i].blockCount != 0) { rsrcblks += filerec.resourceFork.extents[i].blockCount; block = filerec.resourceFork.extents[i].startBlock + filerec.resourceFork.extents[i].blockCount; if (block >= startblk) { cnidbufp[filecnt++] = filerec.fileID; blkstomove += filerec.resourceFork.totalBlocks; break; } } } /* * Check for any overflow extents that overlap. */ if (i == kHFSPlusExtentDensity) { if (filerec.dataFork.totalBlocks > datablks) { if (hfs_overlapped_overflow_extents(hfsmp, startblk, datablks, filerec.fileID, 0)) { cnidbufp[filecnt++] = filerec.fileID; blkstomove += filerec.dataFork.totalBlocks; } } else if (filerec.resourceFork.totalBlocks > rsrcblks) { if (hfs_overlapped_overflow_extents(hfsmp, startblk, rsrcblks, filerec.fileID, 1)) { cnidbufp[filecnt++] = filerec.fileID; blkstomove += filerec.resourceFork.totalBlocks; } } } } end_iteration: if (filecnt == 0 && !system_file_moved) { printf("hfs_reclaimspace: no files moved\n"); error = ENOSPC; } /* All done with catalog. */ hfs_systemfile_unlock(hfsmp, lockflags); if (error || filecnt == 0) goto out; /* * Double check space requirements to make sure * there is enough space to relocate any files * that reside in the reclaim area. * * Blocks To Move -------------- * | | | * V V V * ------------------------------------------------------------------------ * | | / /// // | * | | / /// // | * | | / /// // | * ------------------------------------------------------------------------ * * <------------------- New Total Blocks ------------------><-- Reclaim --> * * <------------------------ Original Total Blocks -----------------------> * */ if (blkstomove >= hfs_freeblks(hfsmp, 1)) { printf("hfs_truncatefs: insufficient space (need %u blocks; have %u blocks)\n", blkstomove, hfs_freeblks(hfsmp, 1)); error = ENOSPC; goto out; } hfsmp->hfs_resize_filesmoved = 0; hfsmp->hfs_resize_totalfiles = filecnt; /* Now move any files that are in the way. */ for (i = 0; i < filecnt; ++i) { struct vnode * rvp; struct cnode * cp; if (hfs_vget(hfsmp, cnidbufp[i], &vp, 0) != 0) continue; /* Relocating directory hard links is not supported, so we * punt (see radar 6217026). */ cp = VTOC(vp); if ((cp->c_flag & C_HARDLINK) && vnode_isdir(vp)) { printf("hfs_reclaimspace: unable to relocate directory hard link %d\n", cp->c_cnid); error = EINVAL; goto out; } /* Relocate any data fork blocks. */ if (VTOF(vp) && VTOF(vp)->ff_blocks > 0) { error = hfs_relocate(vp, hfsmp->hfs_metazone_end + 1, kauth_cred_get(), current_proc()); } if (error) break; /* Relocate any resource fork blocks. */ if ((cp->c_blocks - (VTOF(vp) ? VTOF((vp))->ff_blocks : 0)) > 0) { error = hfs_vgetrsrc(hfsmp, vp, &rvp, TRUE); if (error) break; error = hfs_relocate(rvp, hfsmp->hfs_metazone_end + 1, kauth_cred_get(), current_proc()); VTOC(rvp)->c_flag |= C_NEED_RVNODE_PUT; if (error) break; } hfs_unlock(cp); vnode_put(vp); vp = NULL; ++hfsmp->hfs_resize_filesmoved; /* Report intermediate progress. */ if (filecnt > 100) { int progress; progress = (i * 100) / filecnt; if (progress > (lastprogress + 9)) { printf("hfs_reclaimspace: %d%% done...\n", progress); lastprogress = progress; } } } if (vp) { hfs_unlock(VTOC(vp)); vnode_put(vp); vp = NULL; } if (hfsmp->hfs_resize_filesmoved != 0) { printf("hfs_reclaimspace: relocated %d files on \"%s\"\n", (int)hfsmp->hfs_resize_filesmoved, hfsmp->vcbVN); } out: kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator)); kmem_free(kernel_map, (vm_offset_t)cnidbufp, cnidbufsize); /* * Restore the roving allocation pointer on errors. * (but only if we didn't move any files) */ if (error && hfsmp->hfs_resize_filesmoved == 0) { HFS_UPDATE_NEXT_ALLOCATION(hfsmp, saved_next_allocation); } return (error); } /* * Check if there are any overflow extents that overlap. */ static int hfs_overlapped_overflow_extents(struct hfsmount *hfsmp, u_int32_t startblk, u_int32_t catblks, u_int32_t fileID, int rsrcfork) { struct BTreeIterator * iterator = NULL; struct FSBufferDescriptor btdata; HFSPlusExtentRecord extrec; HFSPlusExtentKey *extkeyptr; FCB *fcb; u_int32_t block; u_int8_t forktype; int overlapped = 0; int i; int error; forktype = rsrcfork ? 0xFF : 0; if (kmem_alloc(kernel_map, (vm_offset_t *)&iterator, sizeof(*iterator))) { return (0); } bzero(iterator, sizeof(*iterator)); extkeyptr = (HFSPlusExtentKey *)&iterator->key; extkeyptr->keyLength = kHFSPlusExtentKeyMaximumLength; extkeyptr->forkType = forktype; extkeyptr->fileID = fileID; extkeyptr->startBlock = catblks; btdata.bufferAddress = &extrec; btdata.itemSize = sizeof(extrec); btdata.itemCount = 1; fcb = VTOF(hfsmp->hfs_extents_vp); error = BTSearchRecord(fcb, iterator, &btdata, NULL, iterator); while (error == 0) { /* Stop when we encounter a different file. */ if ((extkeyptr->fileID != fileID) || (extkeyptr->forkType != forktype)) { break; } /* * Check if the file overlaps target space. */ for (i = 0; i < kHFSPlusExtentDensity; ++i) { if (extrec[i].blockCount == 0) { break; } block = extrec[i].startBlock + extrec[i].blockCount; if (block >= startblk) { overlapped = 1; break; } } /* Look for more records. */ error = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL); } kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator)); return (overlapped); } /* * Calculate the progress of a file system resize operation. */ __private_extern__ int hfs_resize_progress(struct hfsmount *hfsmp, u_int32_t *progress) { if ((hfsmp->hfs_flags & HFS_RESIZE_IN_PROGRESS) == 0) { return (ENXIO); } if (hfsmp->hfs_resize_totalfiles > 0) *progress = (hfsmp->hfs_resize_filesmoved * 100) / hfsmp->hfs_resize_totalfiles; else *progress = 0; return (0); } /* * Creates a UUID from a unique "name" in the HFS UUID Name space. * See version 3 UUID. */ static void hfs_getvoluuid(struct hfsmount *hfsmp, uuid_t result) { MD5_CTX md5c; uint8_t rawUUID[8]; ((uint32_t *)rawUUID)[0] = hfsmp->vcbFndrInfo[6]; ((uint32_t *)rawUUID)[1] = hfsmp->vcbFndrInfo[7]; MD5Init( &md5c ); MD5Update( &md5c, HFS_UUID_NAMESPACE_ID, sizeof( uuid_t ) ); MD5Update( &md5c, rawUUID, sizeof (rawUUID) ); MD5Final( result, &md5c ); result[6] = 0x30 | ( result[6] & 0x0F ); result[8] = 0x80 | ( result[8] & 0x3F ); } /* * Get file system attributes. */ static int hfs_vfs_getattr(struct mount *mp, struct vfs_attr *fsap, __unused vfs_context_t context) { #define HFS_ATTR_CMN_VALIDMASK (ATTR_CMN_VALIDMASK & ~(ATTR_CMN_NAMEDATTRCOUNT | ATTR_CMN_NAMEDATTRLIST)) #define HFS_ATTR_FILE_VALIDMASK (ATTR_FILE_VALIDMASK & ~(ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT | ATTR_FILE_FORKLIST)) ExtendedVCB *vcb = VFSTOVCB(mp); struct hfsmount *hfsmp = VFSTOHFS(mp); u_int32_t freeCNIDs; freeCNIDs = (u_int32_t)0xFFFFFFFF - (u_int32_t)hfsmp->vcbNxtCNID; VFSATTR_RETURN(fsap, f_objcount, (u_int64_t)hfsmp->vcbFilCnt + (u_int64_t)hfsmp->vcbDirCnt); VFSATTR_RETURN(fsap, f_filecount, (u_int64_t)hfsmp->vcbFilCnt); VFSATTR_RETURN(fsap, f_dircount, (u_int64_t)hfsmp->vcbDirCnt); VFSATTR_RETURN(fsap, f_maxobjcount, (u_int64_t)0xFFFFFFFF); VFSATTR_RETURN(fsap, f_iosize, (size_t)cluster_max_io_size(mp, 0)); VFSATTR_RETURN(fsap, f_blocks, (u_int64_t)hfsmp->totalBlocks); VFSATTR_RETURN(fsap, f_bfree, (u_int64_t)hfs_freeblks(hfsmp, 0)); VFSATTR_RETURN(fsap, f_bavail, (u_int64_t)hfs_freeblks(hfsmp, 1)); VFSATTR_RETURN(fsap, f_bsize, (u_int32_t)vcb->blockSize); /* XXX needs clarification */ VFSATTR_RETURN(fsap, f_bused, hfsmp->totalBlocks - hfs_freeblks(hfsmp, 1)); /* Maximum files is constrained by total blocks. */ VFSATTR_RETURN(fsap, f_files, (u_int64_t)(hfsmp->totalBlocks - 2)); VFSATTR_RETURN(fsap, f_ffree, MIN((u_int64_t)freeCNIDs, (u_int64_t)hfs_freeblks(hfsmp, 1))); fsap->f_fsid.val[0] = hfsmp->hfs_raw_dev; fsap->f_fsid.val[1] = vfs_typenum(mp); VFSATTR_SET_SUPPORTED(fsap, f_fsid); VFSATTR_RETURN(fsap, f_signature, vcb->vcbSigWord); VFSATTR_RETURN(fsap, f_carbon_fsid, 0); if (VFSATTR_IS_ACTIVE(fsap, f_capabilities)) { vol_capabilities_attr_t *cap; cap = &fsap->f_capabilities; if (hfsmp->hfs_flags & HFS_STANDARD) { cap->capabilities[VOL_CAPABILITIES_FORMAT] = VOL_CAP_FMT_PERSISTENTOBJECTIDS | VOL_CAP_FMT_CASE_PRESERVING | VOL_CAP_FMT_FAST_STATFS | VOL_CAP_FMT_HIDDEN_FILES | VOL_CAP_FMT_PATH_FROM_ID; } else { cap->capabilities[VOL_CAPABILITIES_FORMAT] = VOL_CAP_FMT_PERSISTENTOBJECTIDS | VOL_CAP_FMT_SYMBOLICLINKS | VOL_CAP_FMT_HARDLINKS | VOL_CAP_FMT_JOURNAL | VOL_CAP_FMT_ZERO_RUNS | (hfsmp->jnl ? VOL_CAP_FMT_JOURNAL_ACTIVE : 0) | (hfsmp->hfs_flags & HFS_CASE_SENSITIVE ? VOL_CAP_FMT_CASE_SENSITIVE : 0) | VOL_CAP_FMT_CASE_PRESERVING | VOL_CAP_FMT_FAST_STATFS | VOL_CAP_FMT_2TB_FILESIZE | VOL_CAP_FMT_HIDDEN_FILES | #if HFS_COMPRESSION VOL_CAP_FMT_PATH_FROM_ID | VOL_CAP_FMT_DECMPFS_COMPRESSION; #else VOL_CAP_FMT_PATH_FROM_ID; #endif } cap->capabilities[VOL_CAPABILITIES_INTERFACES] = VOL_CAP_INT_SEARCHFS | VOL_CAP_INT_ATTRLIST | VOL_CAP_INT_NFSEXPORT | VOL_CAP_INT_READDIRATTR | VOL_CAP_INT_EXCHANGEDATA | VOL_CAP_INT_ALLOCATE | VOL_CAP_INT_VOL_RENAME | VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK | #if NAMEDSTREAMS VOL_CAP_INT_EXTENDED_ATTR | VOL_CAP_INT_NAMEDSTREAMS; #else VOL_CAP_INT_EXTENDED_ATTR; #endif cap->capabilities[VOL_CAPABILITIES_RESERVED1] = 0; cap->capabilities[VOL_CAPABILITIES_RESERVED2] = 0; cap->valid[VOL_CAPABILITIES_FORMAT] = VOL_CAP_FMT_PERSISTENTOBJECTIDS | VOL_CAP_FMT_SYMBOLICLINKS | VOL_CAP_FMT_HARDLINKS | VOL_CAP_FMT_JOURNAL | VOL_CAP_FMT_JOURNAL_ACTIVE | VOL_CAP_FMT_NO_ROOT_TIMES | VOL_CAP_FMT_SPARSE_FILES | VOL_CAP_FMT_ZERO_RUNS | VOL_CAP_FMT_CASE_SENSITIVE | VOL_CAP_FMT_CASE_PRESERVING | VOL_CAP_FMT_FAST_STATFS | VOL_CAP_FMT_2TB_FILESIZE | VOL_CAP_FMT_OPENDENYMODES | VOL_CAP_FMT_HIDDEN_FILES | #if HFS_COMPRESSION VOL_CAP_FMT_PATH_FROM_ID | VOL_CAP_FMT_DECMPFS_COMPRESSION; #else VOL_CAP_FMT_PATH_FROM_ID; #endif cap->valid[VOL_CAPABILITIES_INTERFACES] = VOL_CAP_INT_SEARCHFS | VOL_CAP_INT_ATTRLIST | VOL_CAP_INT_NFSEXPORT | VOL_CAP_INT_READDIRATTR | VOL_CAP_INT_EXCHANGEDATA | VOL_CAP_INT_COPYFILE | VOL_CAP_INT_ALLOCATE | VOL_CAP_INT_VOL_RENAME | VOL_CAP_INT_ADVLOCK | VOL_CAP_INT_FLOCK | VOL_CAP_INT_MANLOCK | #if NAMEDSTREAMS VOL_CAP_INT_EXTENDED_ATTR | VOL_CAP_INT_NAMEDSTREAMS; #else VOL_CAP_INT_EXTENDED_ATTR; #endif cap->valid[VOL_CAPABILITIES_RESERVED1] = 0; cap->valid[VOL_CAPABILITIES_RESERVED2] = 0; VFSATTR_SET_SUPPORTED(fsap, f_capabilities); } if (VFSATTR_IS_ACTIVE(fsap, f_attributes)) { vol_attributes_attr_t *attrp = &fsap->f_attributes; attrp->validattr.commonattr = HFS_ATTR_CMN_VALIDMASK; attrp->validattr.volattr = ATTR_VOL_VALIDMASK & ~ATTR_VOL_INFO; attrp->validattr.dirattr = ATTR_DIR_VALIDMASK; attrp->validattr.fileattr = HFS_ATTR_FILE_VALIDMASK; attrp->validattr.forkattr = 0; attrp->nativeattr.commonattr = HFS_ATTR_CMN_VALIDMASK; attrp->nativeattr.volattr = ATTR_VOL_VALIDMASK & ~ATTR_VOL_INFO; attrp->nativeattr.dirattr = ATTR_DIR_VALIDMASK; attrp->nativeattr.fileattr = HFS_ATTR_FILE_VALIDMASK; attrp->nativeattr.forkattr = 0; VFSATTR_SET_SUPPORTED(fsap, f_attributes); } fsap->f_create_time.tv_sec = hfsmp->vcbCrDate; fsap->f_create_time.tv_nsec = 0; VFSATTR_SET_SUPPORTED(fsap, f_create_time); fsap->f_modify_time.tv_sec = hfsmp->vcbLsMod; fsap->f_modify_time.tv_nsec = 0; VFSATTR_SET_SUPPORTED(fsap, f_modify_time); fsap->f_backup_time.tv_sec = hfsmp->vcbVolBkUp; fsap->f_backup_time.tv_nsec = 0; VFSATTR_SET_SUPPORTED(fsap, f_backup_time); if (VFSATTR_IS_ACTIVE(fsap, f_fssubtype)) { u_int16_t subtype = 0; /* * Subtypes (flavors) for HFS * 0: Mac OS Extended * 1: Mac OS Extended (Journaled) * 2: Mac OS Extended (Case Sensitive) * 3: Mac OS Extended (Case Sensitive, Journaled) * 4 - 127: Reserved * 128: Mac OS Standard * */ if (hfsmp->hfs_flags & HFS_STANDARD) { subtype = HFS_SUBTYPE_STANDARDHFS; } else /* HFS Plus */ { if (hfsmp->jnl) subtype |= HFS_SUBTYPE_JOURNALED; if (hfsmp->hfs_flags & HFS_CASE_SENSITIVE) subtype |= HFS_SUBTYPE_CASESENSITIVE; } fsap->f_fssubtype = subtype; VFSATTR_SET_SUPPORTED(fsap, f_fssubtype); } if (VFSATTR_IS_ACTIVE(fsap, f_vol_name)) { strlcpy(fsap->f_vol_name, (char *) hfsmp->vcbVN, MAXPATHLEN); VFSATTR_SET_SUPPORTED(fsap, f_vol_name); } if (VFSATTR_IS_ACTIVE(fsap, f_uuid)) { hfs_getvoluuid(hfsmp, fsap->f_uuid); VFSATTR_SET_SUPPORTED(fsap, f_uuid); } return (0); } /* * Perform a volume rename. Requires the FS' root vp. */ static int hfs_rename_volume(struct vnode *vp, const char *name, proc_t p) { ExtendedVCB *vcb = VTOVCB(vp); struct cnode *cp = VTOC(vp); struct hfsmount *hfsmp = VTOHFS(vp); struct cat_desc to_desc; struct cat_desc todir_desc; struct cat_desc new_desc; cat_cookie_t cookie; int lockflags; int error = 0; /* * Ignore attempts to rename a volume to a zero-length name. */ if (name[0] == 0) return(0); bzero(&to_desc, sizeof(to_desc)); bzero(&todir_desc, sizeof(todir_desc)); bzero(&new_desc, sizeof(new_desc)); bzero(&cookie, sizeof(cookie)); todir_desc.cd_parentcnid = kHFSRootParentID; todir_desc.cd_cnid = kHFSRootFolderID; todir_desc.cd_flags = CD_ISDIR; to_desc.cd_nameptr = (const u_int8_t *)name; to_desc.cd_namelen = strlen(name); to_desc.cd_parentcnid = kHFSRootParentID; to_desc.cd_cnid = cp->c_cnid; to_desc.cd_flags = CD_ISDIR; if ((error = hfs_lock(cp, HFS_EXCLUSIVE_LOCK)) == 0) { if ((error = hfs_start_transaction(hfsmp)) == 0) { if ((error = cat_preflight(hfsmp, CAT_RENAME, &cookie, p)) == 0) { lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK); error = cat_rename(hfsmp, &cp->c_desc, &todir_desc, &to_desc, &new_desc); /* * If successful, update the name in the VCB, ensure it's terminated. */ if (!error) { strlcpy((char *)vcb->vcbVN, name, sizeof(vcb->vcbVN)); } hfs_systemfile_unlock(hfsmp, lockflags); cat_postflight(hfsmp, &cookie, p); if (error) MarkVCBDirty(vcb); (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); } hfs_end_transaction(hfsmp); } if (!error) { /* Release old allocated name buffer */ if (cp->c_desc.cd_flags & CD_HASBUF) { const char *tmp_name = (const char *)cp->c_desc.cd_nameptr; cp->c_desc.cd_nameptr = 0; cp->c_desc.cd_namelen = 0; cp->c_desc.cd_flags &= ~CD_HASBUF; vfs_removename(tmp_name); } /* Update cnode's catalog descriptor */ replace_desc(cp, &new_desc); vcb->volumeNameEncodingHint = new_desc.cd_encoding; cp->c_touch_chgtime = TRUE; } hfs_unlock(cp); } return(error); } /* * Get file system attributes. */ static int hfs_vfs_setattr(struct mount *mp, struct vfs_attr *fsap, __unused vfs_context_t context) { kauth_cred_t cred = vfs_context_ucred(context); int error = 0; /* * Must be superuser or owner of filesystem to change volume attributes */ if (!kauth_cred_issuser(cred) && (kauth_cred_getuid(cred) != vfs_statfs(mp)->f_owner)) return(EACCES); if (VFSATTR_IS_ACTIVE(fsap, f_vol_name)) { vnode_t root_vp; error = hfs_vfs_root(mp, &root_vp, context); if (error) goto out; error = hfs_rename_volume(root_vp, fsap->f_vol_name, vfs_context_proc(context)); (void) vnode_put(root_vp); if (error) goto out; VFSATTR_SET_SUPPORTED(fsap, f_vol_name); } out: return error; } /* If a runtime corruption is detected, set the volume inconsistent * bit in the volume attributes. The volume inconsistent bit is a persistent * bit which represents that the volume is corrupt and needs repair. * The volume inconsistent bit can be set from the kernel when it detects * runtime corruption or from file system repair utilities like fsck_hfs when * a repair operation fails. The bit should be cleared only from file system * verify/repair utility like fsck_hfs when a verify/repair succeeds. */ void hfs_mark_volume_inconsistent(struct hfsmount *hfsmp) { HFS_MOUNT_LOCK(hfsmp, TRUE); if ((hfsmp->vcbAtrb & kHFSVolumeInconsistentMask) == 0) { hfsmp->vcbAtrb |= kHFSVolumeInconsistentMask; MarkVCBDirty(hfsmp); } if ((hfsmp->hfs_flags & HFS_READ_ONLY)==0) { /* Log information to ASL log */ fslog_fs_corrupt(hfsmp->hfs_mp); printf("hfs: Runtime corruption detected on %s, fsck will be forced on next mount.\n", hfsmp->vcbVN); } HFS_MOUNT_UNLOCK(hfsmp, TRUE); } /* Replay the journal on the device node provided. Returns zero if * journal replay succeeded or no journal was supposed to be replayed. */ static int hfs_journal_replay(vnode_t devvp, vfs_context_t context) { int retval = 0; struct mount *mp = NULL; struct hfs_mount_args *args = NULL; /* Replay allowed only on raw devices */ if (!vnode_ischr(devvp)) { retval = EINVAL; goto out; } /* Create dummy mount structures */ MALLOC(mp, struct mount *, sizeof(struct mount), M_TEMP, M_WAITOK); if (mp == NULL) { retval = ENOMEM; goto out; } bzero(mp, sizeof(struct mount)); mount_lock_init(mp); MALLOC(args, struct hfs_mount_args *, sizeof(struct hfs_mount_args), M_TEMP, M_WAITOK); if (args == NULL) { retval = ENOMEM; goto out; } bzero(args, sizeof(struct hfs_mount_args)); retval = hfs_mountfs(devvp, mp, args, 1, context); buf_flushdirtyblks(devvp, MNT_WAIT, 0, "hfs_journal_replay"); out: if (mp) { mount_lock_destroy(mp); FREE(mp, M_TEMP); } if (args) { FREE(args, M_TEMP); } return retval; } /* * hfs vfs operations. */ struct vfsops hfs_vfsops = { hfs_mount, hfs_start, hfs_unmount, hfs_vfs_root, hfs_quotactl, hfs_vfs_getattr, /* was hfs_statfs */ hfs_sync, hfs_vfs_vget, hfs_fhtovp, hfs_vptofh, hfs_init, hfs_sysctl, hfs_vfs_setattr, {NULL} }; |