sys/kern/vfs_syscalls.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 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 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 | /* * Copyright (c) 1989, 1993 * 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. 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. * * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $ */ #include <sys/param.h> #include <sys/systm.h> #include <sys/buf.h> #include <sys/conf.h> #include <sys/sysent.h> #include <sys/malloc.h> #include <sys/mount.h> #include <sys/mountctl.h> #include <sys/sysmsg.h> #include <sys/filedesc.h> #include <sys/kernel.h> #include <sys/fcntl.h> #include <sys/file.h> #include <sys/linker.h> #include <sys/stat.h> #include <sys/unistd.h> #include <sys/vnode.h> #include <sys/proc.h> #include <sys/caps.h> #include <sys/jail.h> #include <sys/namei.h> #include <sys/nlookup.h> #include <sys/dirent.h> #include <sys/extattr.h> #include <sys/spinlock.h> #include <sys/kern_syscall.h> #include <sys/objcache.h> #include <sys/sysctl.h> #include <sys/buf2.h> #include <sys/file2.h> #include <sys/spinlock2.h> #include <vm/vm.h> #include <vm/vm_object.h> #include <vm/vm_page.h> #include <machine/limits.h> #include <machine/stdarg.h> #define UMOUNTF_RETRIES 50 /* 0.25 seconds per retry */ static void mount_warning(struct mount *mp, const char *ctl, ...) __printflike(2, 3); static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb); static int checkvp_chdir (struct vnode *vn, struct thread *td); static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch); static int get_fscap(const char *); static int chroot_refuse_vdir_fds (thread_t td, struct filedesc *fdp); static int chroot_visible_mnt(struct mount *mp, struct proc *p); static int getutimes (struct timeval *, struct timespec *); static int getutimens (const struct timespec *, struct timespec *, int *); static int setfown (struct mount *, struct vnode *, uid_t, gid_t); static int setfmode (struct vnode *, int); static int setfflags (struct vnode *, u_long); static int setutimes (struct vnode *, struct vattr *, const struct timespec *, int); static int usermount = 0; /* if 1, non-root can mount fs. */ SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "Allow non-root users to mount filesystems"); static int debug_unmount = 0; /* if 1 loop until unmount success */ SYSCTL_INT(_vfs, OID_AUTO, debug_unmount, CTLFLAG_RW, &debug_unmount, 0, "Stall failed unmounts in loop"); static struct krate krate_rename = { 1 }; /* * Virtual File System System Calls */ /* * Mount a file system. * * mount_args(char *type, char *path, int flags, caddr_t data) * * MPALMOSTSAFE */ int sys_mount(struct sysmsg *sysmsg, const struct mount_args *uap) { struct thread *td = curthread; struct vnode *vp; struct nchandle nch; struct mount *mp, *nullmp; struct vfsconf *vfsp; int error, flag = 0, flag2 = 0; int hasmount; int priv = 0; int flags = uap->flags; struct vattr va; struct nlookupdata nd; char fstypename[MFSNAMELEN]; struct ucred *cred; cred = td->td_ucred; /* We do not allow user mounts inside a jail for now */ if (usermount && jailed(cred)) { error = EPERM; goto done; } /* * Extract the file system type. We need to know this early, to take * appropriate actions for jails and the filesystems to mount. */ if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) goto done; /* * Select the correct cap according to the file system type. */ priv = get_fscap(fstypename); if (usermount) error = caps_priv_check_td(td, priv | __SYSCAP_NOROOTTEST); else error = caps_priv_check_td(td, priv); if (error) goto done; /* * Do not allow NFS export by non-root users. */ if (flags & MNT_EXPORTED) { error = caps_priv_check_td(td, priv); if (error) goto done; } /* * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users */ if (caps_priv_check_td(td, priv)) flags |= MNT_NOSUID | MNT_NODEV; /* * Lookup the requested path and extract the nch and vnode. */ error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) { if ((error = nlookup(&nd)) == 0) { if (nd.nl_nch.ncp->nc_vp == NULL) error = ENOENT; } } if (error) { nlookup_done(&nd); goto done; } /* * If the target filesystem is resolved via a nullfs mount, then * nd.nl_nch.mount will be pointing to the nullfs mount structure * instead of the target file system. We need it in case we are * doing an update. */ nullmp = nd.nl_nch.mount; /* * Extract the locked+refd ncp and cleanup the nd structure */ nch = nd.nl_nch; cache_zero(&nd.nl_nch); nlookup_done(&nd); if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && (mp = cache_findmount(&nch)) != NULL) { cache_dropmount(mp); hasmount = 1; } else { hasmount = 0; } /* * now we have the locked ref'd nch and unreferenced vnode. */ vp = nch.ncp->nc_vp; if ((error = vget(vp, LK_EXCLUSIVE)) != 0) { cache_put(&nch); goto done; } cache_unlock(&nch); /* * Now we have an unlocked ref'd nch and a locked ref'd vp */ if (flags & MNT_UPDATE) { if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) { cache_drop(&nch); vput(vp); error = EINVAL; goto done; } if (strncmp(fstypename, "null", 5) == 0) { KKASSERT(nullmp); mp = nullmp; } else { mp = vp->v_mount; } flag = mp->mnt_flag; flag2 = mp->mnt_kern_flag; /* * We only allow the filesystem to be reloaded if it * is currently mounted read-only. */ if ((flags & MNT_RELOAD) && ((mp->mnt_flag & MNT_RDONLY) == 0)) { cache_drop(&nch); vput(vp); error = EOPNOTSUPP; /* Needs translation */ goto done; } /* * Only root, or the user that did the original mount is * permitted to update it. */ if (mp->mnt_stat.f_owner != cred->cr_uid && (error = caps_priv_check_td(td, priv))) { cache_drop(&nch); vput(vp); goto done; } if (vfs_busy(mp, LK_NOWAIT)) { cache_drop(&nch); vput(vp); error = EBUSY; goto done; } if (hasmount) { cache_drop(&nch); vfs_unbusy(mp); vput(vp); error = EBUSY; goto done; } mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE); lwkt_gettoken(&mp->mnt_token); vn_unlock(vp); vfsp = mp->mnt_vfc; goto update; } /* * If the user is not root, ensure that they own the directory * onto which we are attempting to mount. */ if ((error = VOP_GETATTR(vp, &va)) || (va.va_uid != cred->cr_uid && (error = caps_priv_check_td(td, priv)))) { cache_drop(&nch); vput(vp); goto done; } if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) { cache_drop(&nch); vput(vp); goto done; } if (vp->v_type != VDIR) { cache_drop(&nch); vput(vp); error = ENOTDIR; goto done; } if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) { cache_drop(&nch); vput(vp); error = EPERM; goto done; } vfsp = vfsconf_find_by_name(fstypename); if (vfsp == NULL) { linker_file_t lf; /* Only load modules for root (very important!) */ error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT); if (error) { cache_drop(&nch); vput(vp); goto done; } error = linker_load_file(fstypename, &lf); if (error || lf == NULL) { cache_drop(&nch); vput(vp); if (lf == NULL) error = ENODEV; goto done; } lf->userrefs++; /* lookup again, see if the VFS was loaded */ vfsp = vfsconf_find_by_name(fstypename); if (vfsp == NULL) { lf->userrefs--; linker_file_unload(lf); cache_drop(&nch); vput(vp); error = ENODEV; goto done; } } if (hasmount) { cache_drop(&nch); vput(vp); error = EBUSY; goto done; } /* * Allocate and initialize the filesystem. */ mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK); mount_init(mp, vfsp->vfc_vfsops); vfs_busy(mp, LK_NOWAIT); mp->mnt_vfc = vfsp; mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT; vfsp->vfc_refcount++; mp->mnt_stat.f_type = vfsp->vfc_typenum; mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); mp->mnt_stat.f_owner = cred->cr_uid; lwkt_gettoken(&mp->mnt_token); vn_unlock(vp); update: /* * (per-mount token acquired at this point) * * Set the mount level flags. */ if (flags & MNT_RDONLY) mp->mnt_flag |= MNT_RDONLY; else if (mp->mnt_flag & MNT_RDONLY) mp->mnt_kern_flag |= MNTK_WANTRDWR; mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME | MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR | MNT_AUTOMOUNTED); mp->mnt_flag |= flags & (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE | MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM | MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR | MNT_AUTOMOUNTED); /* * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf. * This way the initial VFS_MOUNT() call will also be MPSAFE. */ if (vfsp->vfc_flags & VFCF_MPSAFE) mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* * Mount the filesystem. * XXX The final recipients of VFS_MOUNT just overwrite the ndp they * get. */ if (mp->mnt_flag & MNT_UPDATE) { error = VFS_MOUNT(mp, uap->path, uap->data, cred); if (mp->mnt_kern_flag & MNTK_WANTRDWR) mp->mnt_flag &= ~MNT_RDONLY; mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE); mp->mnt_kern_flag &=~ MNTK_WANTRDWR; if (error) { mp->mnt_flag = flag; mp->mnt_kern_flag = flag2; } lwkt_reltoken(&mp->mnt_token); vfs_unbusy(mp); vrele(vp); cache_drop(&nch); goto done; } mp->mnt_ncmounton = nch; error = VFS_MOUNT(mp, uap->path, uap->data, cred); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* * Put the new filesystem on the mount list after root. The mount * point gets its own mnt_ncmountpt (unless the VFS already set one * up) which represents the root of the mount. The lookup code * detects the mount point going forward and checks the root of * the mount going backwards. * * It is not necessary to invalidate or purge the vnode underneath * because elements under the mount will be given their own glue * namecache record. */ if (!error) { if (mp->mnt_ncmountpt.ncp == NULL) { /* * Allocate, then unlock, but leave the ref intact. * This is the mnt_refs (1) that we will retain * through to the unmount. */ cache_allocroot(&mp->mnt_ncmountpt, mp, NULL); cache_unlock(&mp->mnt_ncmountpt); } vn_unlock(vp); cache_lock(&nch); nch.ncp->nc_flag |= NCF_ISMOUNTPT; cache_unlock(&nch); cache_ismounting(mp); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); mountlist_insert(mp, MNTINS_LAST); vn_unlock(vp); checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt); error = vfs_allocate_syncvnode(mp); lwkt_reltoken(&mp->mnt_token); vfs_unbusy(mp); error = VFS_START(mp, 0); vrele(vp); KNOTE(&fs_klist, VQ_MOUNT); } else { bzero(&mp->mnt_ncmounton, sizeof(mp->mnt_ncmounton)); vn_syncer_thr_stop(mp); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); if (mp->mnt_cred) { crfree(mp->mnt_cred); mp->mnt_cred = NULL; } mp->mnt_vfc->vfc_refcount--; lwkt_reltoken(&mp->mnt_token); vfs_unbusy(mp); kfree(mp, M_MOUNT); cache_drop(&nch); vput(vp); } done: return (error); } /* * Scan all active processes to see if any of them have a current * or root directory onto which the new filesystem has just been * mounted. If so, replace them with the new mount point. * * Both old_nch and new_nch are ref'd on call but not locked. * new_nch must be temporarily locked so it can be associated with the * vnode representing the root of the mount point. */ struct checkdirs_info { struct nchandle old_nch; struct nchandle new_nch; struct vnode *old_vp; struct vnode *new_vp; }; static int checkdirs_callback(struct proc *p, void *data); static void checkdirs(struct nchandle *old_nch, struct nchandle *new_nch) { struct checkdirs_info info; struct vnode *olddp; struct vnode *newdp; struct mount *mp; /* * If the old mount point's vnode has a usecount of 1, it is not * being held as a descriptor anywhere. */ olddp = old_nch->ncp->nc_vp; if (olddp == NULL || VREFCNT(olddp) == 1) return; /* * Force the root vnode of the new mount point to be resolved * so we can update any matching processes. */ mp = new_nch->mount; if (VFS_ROOT(mp, &newdp)) panic("mount: lost mount"); vn_unlock(newdp); cache_lock(new_nch); vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY); cache_setunresolved(new_nch); cache_setvp(new_nch, newdp); cache_unlock(new_nch); /* * Special handling of the root node */ if (rootvnode == olddp) { vref(newdp); vfs_cache_setroot(newdp, cache_hold(new_nch)); } /* * Pass newdp separately so the callback does not have to access * it via new_nch->ncp->nc_vp. */ info.old_nch = *old_nch; info.new_nch = *new_nch; info.new_vp = newdp; allproc_scan(checkdirs_callback, &info, 0); vput(newdp); } /* * NOTE: callback is not MP safe because the scanned process's filedesc * structure can be ripped out from under us, amoung other things. */ static int checkdirs_callback(struct proc *p, void *data) { struct checkdirs_info *info = data; struct filedesc *fdp; struct nchandle ncdrop1; struct nchandle ncdrop2; struct vnode *vprele1; struct vnode *vprele2; if ((fdp = p->p_fd) != NULL) { cache_zero(&ncdrop1); cache_zero(&ncdrop2); vprele1 = NULL; vprele2 = NULL; /* * MPUNSAFE - XXX fdp can be pulled out from under a * foreign process. * * A shared filedesc is ok, we don't have to copy it * because we are making this change globally. */ spin_lock(&fdp->fd_spin); if (fdp->fd_ncdir.mount == info->old_nch.mount && fdp->fd_ncdir.ncp == info->old_nch.ncp) { vprele1 = fdp->fd_cdir; vref(info->new_vp); fdp->fd_cdir = info->new_vp; ncdrop1 = fdp->fd_ncdir; cache_copy(&info->new_nch, &fdp->fd_ncdir); } if (fdp->fd_nrdir.mount == info->old_nch.mount && fdp->fd_nrdir.ncp == info->old_nch.ncp) { vprele2 = fdp->fd_rdir; vref(info->new_vp); fdp->fd_rdir = info->new_vp; ncdrop2 = fdp->fd_nrdir; cache_copy(&info->new_nch, &fdp->fd_nrdir); } spin_unlock(&fdp->fd_spin); if (ncdrop1.ncp) cache_drop(&ncdrop1); if (ncdrop2.ncp) cache_drop(&ncdrop2); if (vprele1) vrele(vprele1); if (vprele2) vrele(vprele2); } return(0); } /* * Unmount a file system. * * Note: unmount takes a path to the vnode mounted on as argument, * not special file (as before). * * umount_args(char *path, int flags) * * MPALMOSTSAFE */ int sys_unmount(struct sysmsg *sysmsg, const struct unmount_args *uap) { struct thread *td = curthread; struct proc *p __debugvar = td->td_proc; struct mount *mp = NULL; struct nlookupdata nd; char fstypename[MFSNAMELEN]; int priv = 0; int error; struct ucred *cred; cred = td->td_ucred; KKASSERT(p); /* We do not allow user umounts inside a jail for now */ if (usermount && jailed(cred)) { error = EPERM; goto done; } error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW | NLC_IGNBADDIR); if (error == 0) error = nlookup(&nd); if (error) goto out; mp = nd.nl_nch.mount; /* Figure out the fsname in order to select proper privs */ ksnprintf(fstypename, MFSNAMELEN, "%s", mp->mnt_vfc->vfc_name); priv = get_fscap(fstypename); if (usermount) error = caps_priv_check_td(td, priv | __SYSCAP_NOROOTTEST); else error = caps_priv_check_td(td, priv); if (error) { nlookup_done(&nd); goto done; } /* * Only root, or the user that did the original mount is * permitted to unmount this filesystem. */ if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) && (error = caps_priv_check_td(td, priv))) { goto out; } /* * Don't allow unmounting the root file system. */ if (mp->mnt_flag & MNT_ROOTFS) { error = EINVAL; goto out; } /* * Must be the root of the filesystem */ if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) { error = EINVAL; goto out; } /* Check if this mount belongs to this prison */ if (jailed(cred) && mp->mnt_cred && (!mp->mnt_cred->cr_prison || mp->mnt_cred->cr_prison != cred->cr_prison)) { kprintf("mountpoint %s does not belong to this jail\n", uap->path); error = EPERM; goto out; } /* * If no error try to issue the unmount. We lose our cache * ref when we call nlookup_done so we must hold the mount point * to prevent use-after-free races. */ out: if (error == 0) { mount_hold(mp); nlookup_done(&nd); error = dounmount(mp, uap->flags, 0); mount_drop(mp); } else { nlookup_done(&nd); } done: return (error); } /* * Do the actual file system unmount (interlocked against the mountlist * token and mp->mnt_token). */ static int dounmount_interlock(struct mount *mp) { if (mp->mnt_kern_flag & MNTK_UNMOUNT) return (EBUSY); mp->mnt_kern_flag |= MNTK_UNMOUNT; return(0); } /* * Returns non-zero if the specified process uses the specified * mount point. */ static int process_uses_mount(struct proc *p, struct mount *mp) { struct filedesc *fdp; struct file *fp; int found; int n; fdp = p->p_fd; if (fdp == NULL) return 0; if (fdp->fd_ncdir.mount == mp || fdp->fd_nrdir.mount == mp || fdp->fd_njdir.mount == mp) { return 1; } found = 0; spin_lock_shared(&fdp->fd_spin); for (n = 0; n < fdp->fd_nfiles; ++n) { fp = fdp->fd_files[n].fp; if (fp && fp->f_nchandle.mount == mp) { found = 1; break; } } spin_unlock_shared(&fdp->fd_spin); return found; } /* * Cleanup processes that have references to the mount point * being force-unmounted. */ struct unmount_allproc_info { struct mount *mp; int sig; }; static int unmount_allproc_cb(struct proc *p, void *arg) { struct unmount_allproc_info *info; struct mount *mp; info = arg; mp = info->mp; if (p->p_textnch.mount == mp) cache_drop(&p->p_textnch); if (info->sig && process_uses_mount(p, mp)) { lwkt_gettoken(&p->p_token); p->p_flags |= P_MUSTKILL; lwkt_reltoken(&p->p_token); ksignal(p, info->sig); } return 0; } /* * The guts of the unmount code. The mount owns one ref and one hold * count. If we successfully interlock the unmount, those refs are ours. * (The ref is from mnt_ncmountpt). * * When halting we shortcut certain mount types such as devfs by not actually * issuing the VFS_SYNC() or VFS_UNMOUNT(). They are still disconnected * from the mountlist so higher-level filesytems can unmount cleanly. * * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs. */ int dounmount(struct mount *mp, int flags, int halting) { struct namecache *ncp; struct nchandle nch; struct vnode *vp; int error; int async_flag; int lflags; int freeok = 1; int hadsyncer = 0; int retry; int quickhalt; lwkt_gettoken(&mp->mnt_token); /* * When halting, certain mount points can essentially just * be unhooked and otherwise ignored. */ if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) { quickhalt = 1; freeok = 0; } else { quickhalt = 0; } /* * Exclusive access for unmounting purposes. */ if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0) goto out; /* * We now 'own' the last mp->mnt_refs * * Allow filesystems to detect that a forced unmount is in progress. */ if (flags & MNT_FORCE) mp->mnt_kern_flag |= MNTK_UNMOUNTF; lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK); error = lockmgr(&mp->mnt_lock, lflags); if (error) { mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); if (mp->mnt_kern_flag & MNTK_MWAIT) { mp->mnt_kern_flag &= ~MNTK_MWAIT; wakeup(mp); } goto out; } if (mp->mnt_flag & MNT_EXPUBLIC) vfs_setpublicfs(NULL, NULL, NULL); vfs_msync(mp, MNT_WAIT); async_flag = mp->mnt_flag & MNT_ASYNC; mp->mnt_flag &=~ MNT_ASYNC; /* * Decomission our special mnt_syncer vnode. This also stops * the vnlru code. If we are unable to unmount we recommission * the vnode. * * Then sync the filesystem. */ if ((vp = mp->mnt_syncer) != NULL) { mp->mnt_syncer = NULL; atomic_set_int(&vp->v_refcnt, VREF_FINALIZE); vrele(vp); hadsyncer = 1; } /* * Sync normally-mounted filesystem. */ if (quickhalt == 0) { if ((mp->mnt_flag & MNT_RDONLY) == 0) VFS_SYNC(mp, MNT_WAIT); } /* * nchandle records ref the mount structure. Expect a count of 1 * (our mount->mnt_ncmountpt). * * Scans can get temporary refs on a mountpoint (thought really * heavy duty stuff like cache_findmount() do not). */ for (retry = 0; (retry < UMOUNTF_RETRIES || debug_unmount); ++retry) { int dummy = 0; /* * Invalidate the namecache topology under the mount. * nullfs mounts alias a real mount's namecache topology * and it should not be invalidated in that case. */ if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) { cache_lock(&mp->mnt_ncmountpt); cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN); cache_unlock(&mp->mnt_ncmountpt); } /* * Clear pcpu caches */ cache_unmounting(mp); if (mp->mnt_refs != 1) cache_clearmntcache(mp); /* * Break out if we are good. Don't count ncp refs if the * mount is aliased. */ ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ? NULL : mp->mnt_ncmountpt.ncp; if (mp->mnt_refs == 1 && (ncp == NULL || (ncp->nc_refs == 1 && TAILQ_FIRST(&ncp->nc_list) == NULL))) { break; } /* * If forcing the unmount, clean out any p->p_textnch * nchandles that match this mount. * * In addition any process which has a current, root, or * jail directory matching the mount, or which has an open * descriptor matching the mount, will be killed. We first * try SIGINT, and if that doesn't work we issue SIGKILL. */ if (flags & MNT_FORCE) { struct unmount_allproc_info info; info.mp = mp; switch(retry) { case 3: info.sig = SIGINT; break; case 7: info.sig = SIGKILL; break; default: info.sig = 0; break; } allproc_scan(&unmount_allproc_cb, &info, 0); } /* * Sleep and retry. */ error = lockmgr(&mp->mnt_lock, LK_RELEASE); tsleep(&dummy, 0, "mntbsy", hz / 4 + 1); error = lockmgr(&mp->mnt_lock, LK_EXCLUSIVE); if (debug_unmount && (retry & 15) == 15) { mount_warning(mp, "(%p) debug - retry %d, " "%d namecache refs, %d mount refs", mp, retry, (ncp ? ncp->nc_refs - 1 : 0), mp->mnt_refs - 1); } } if (retry == UMOUNTF_RETRIES) { mount_warning(mp, "forced umount of \"%s\" - " "%d namecache refs, %d mount refs", (mp->mnt_ncmountpt.ncp ? mp->mnt_ncmountpt.ncp->nc_name : "?"), (ncp ? ncp->nc_refs - 1 : 0), mp->mnt_refs - 1); } error = 0; ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ? NULL : mp->mnt_ncmountpt.ncp; if (mp->mnt_refs != 1 || (ncp != NULL && (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list)))) { mount_warning(mp, "(%p): %d namecache refs, %d mount refs " "still present", mp, (ncp ? ncp->nc_refs - 1 : 0), mp->mnt_refs - 1); if (flags & MNT_FORCE) { freeok = 0; mount_warning(mp, "forcing unmount\n"); } else { error = EBUSY; } } /* * So far so good, sync the filesystem once more and * call the VFS unmount code if the sync succeeds. */ if (error == 0 && quickhalt == 0) { if (mp->mnt_flag & MNT_RDONLY) { error = VFS_UNMOUNT(mp, flags); } else { error = VFS_SYNC(mp, MNT_WAIT); if (error == 0 || /* no error */ error == EOPNOTSUPP || /* no sync avail */ (flags & MNT_FORCE)) { /* force anyway */ error = VFS_UNMOUNT(mp, flags); } } if (error) { mount_warning(mp, "(%p) unmount: vfs refused to unmount, " "error %d", mp, error); } } /* * If an error occurred we can still recover, restoring the * syncer vnode and misc flags. */ if (error) { if (mp->mnt_syncer == NULL && hadsyncer) vfs_allocate_syncvnode(mp); mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); mp->mnt_flag |= async_flag; lockmgr(&mp->mnt_lock, LK_RELEASE); if (mp->mnt_kern_flag & MNTK_MWAIT) { mp->mnt_kern_flag &= ~MNTK_MWAIT; wakeup(mp); } goto out; } /* * Clean up any journals still associated with the mount after * filesystem activity has ceased. */ journal_remove_all_journals(mp, ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0)); mountlist_remove(mp); /* * Remove any installed vnode ops here so the individual VFSs don't * have to. * * mnt_refs should go to zero when we scrap mnt_ncmountpt. * * When quickhalting we have to keep these intact because the * underlying vnodes have not been destroyed, and some might be * dirty. */ if (quickhalt == 0) { vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); } if (mp->mnt_ncmountpt.ncp != NULL) { nch = mp->mnt_ncmountpt; cache_zero(&mp->mnt_ncmountpt); cache_clrmountpt(&nch); cache_drop(&nch); } if (mp->mnt_ncmounton.ncp != NULL) { cache_unmounting(mp); nch = mp->mnt_ncmounton; cache_zero(&mp->mnt_ncmounton); cache_clrmountpt(&nch); cache_drop(&nch); } if (mp->mnt_cred) { crfree(mp->mnt_cred); mp->mnt_cred = NULL; } mp->mnt_vfc->vfc_refcount--; /* * If not quickhalting the mount, we expect there to be no * vnodes left. */ if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist)) panic("unmount: dangling vnode"); /* * Release the lock */ lockmgr(&mp->mnt_lock, LK_RELEASE); if (mp->mnt_kern_flag & MNTK_MWAIT) { mp->mnt_kern_flag &= ~MNTK_MWAIT; wakeup(mp); } /* * If we reach here and freeok != 0 we must free the mount. * mnt_refs should already have dropped to 0, so if it is not * zero we must cycle the caches and wait. * * When we are satisfied that the mount has disconnected we can * drop the hold on the mp that represented the mount (though the * caller might actually have another, so the caller's drop may * do the actual free). */ if (freeok) { if (mp->mnt_refs > 0) cache_clearmntcache(mp); while (mp->mnt_refs > 0) { cache_unmounting(mp); wakeup(mp); tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1); cache_clearmntcache(mp); } lwkt_reltoken(&mp->mnt_token); mount_drop(mp); mp = NULL; } else { cache_clearmntcache(mp); } error = 0; KNOTE(&fs_klist, VQ_UNMOUNT); out: if (mp) lwkt_reltoken(&mp->mnt_token); return (error); } static void mount_warning(struct mount *mp, const char *ctl, ...) { char *ptr; char *buf; __va_list va; __va_start(va, ctl); if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL, &ptr, &buf, 0) == 0) { kprintf("unmount(%s): ", ptr); kvprintf(ctl, va); kprintf("\n"); kfree(buf, M_TEMP); } else { kprintf("unmount(%p", mp); if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name) kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name); kprintf("): "); kvprintf(ctl, va); kprintf("\n"); } __va_end(va); } /* * Shim cache_fullpath() to handle the case where a process is chrooted into * a subdirectory of a mount. In this case if the root mount matches the * process root directory's mount we have to specify the process's root * directory instead of the mount point, because the mount point might * be above the root directory. */ static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb) { struct nchandle *nch; if (p && p->p_fd->fd_nrdir.mount == mp) nch = &p->p_fd->fd_nrdir; else nch = &mp->mnt_ncmountpt; return(cache_fullpath(p, nch, NULL, rb, fb, 0)); } /* * Sync each mounted filesystem. */ #ifdef DEBUG static int syncprt = 0; SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, ""); #endif /* DEBUG */ static int sync_callback(struct mount *mp, void *data); int sys_sync(struct sysmsg *sysmsg, const struct sync_args *uap) { mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD); return (0); } static int sync_callback(struct mount *mp, void *data __unused) { int asyncflag; if ((mp->mnt_flag & MNT_RDONLY) == 0) { lwkt_gettoken(&mp->mnt_token); asyncflag = mp->mnt_flag & MNT_ASYNC; mp->mnt_flag &= ~MNT_ASYNC; lwkt_reltoken(&mp->mnt_token); vfs_msync(mp, MNT_NOWAIT); VFS_SYNC(mp, MNT_NOWAIT); lwkt_gettoken(&mp->mnt_token); mp->mnt_flag |= asyncflag; lwkt_reltoken(&mp->mnt_token); } return(0); } /* XXX PRISON: could be per prison flag */ static int prison_quotas; #if 0 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, ""); #endif /* * quotactl_args(char *path, int fcmd, int uid, caddr_t arg) * * Change filesystem quotas. * * MPALMOSTSAFE */ int sys_quotactl(struct sysmsg *sysmsg, const struct quotactl_args *uap) { struct nlookupdata nd; struct thread *td; struct mount *mp; int error; td = curthread; if (td->td_ucred->cr_prison && !prison_quotas) { error = EPERM; goto done; } error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) { mp = nd.nl_nch.mount; error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, nd.nl_cred); } nlookup_done(&nd); done: return (error); } /* * mountctl(char *path, int op, int fd, const void *ctl, int ctllen, * void *buf, int buflen) * * This function operates on a mount point and executes the specified * operation using the specified control data, and possibly returns data. * * The actual number of bytes stored in the result buffer is returned, 0 * if none, otherwise an error is returned. * * MPALMOSTSAFE */ int sys_mountctl(struct sysmsg *sysmsg, const struct mountctl_args *uap) { struct thread *td = curthread; struct file *fp; void *ctl = NULL; void *buf = NULL; char *path = NULL; int error; /* * Sanity and permissions checks. We must be root. */ if (td->td_ucred->cr_prison != NULL) return (EPERM); if ((uap->op != MOUNTCTL_MOUNTFLAGS) && (error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)) != 0) { return (error); } /* * Argument length checks */ if (uap->ctllen < 0 || uap->ctllen > 1024) return (EINVAL); if (uap->buflen < 0 || uap->buflen > 16 * 1024) return (EINVAL); if (uap->path == NULL) return (EINVAL); /* * Allocate the necessary buffers and copyin data */ path = objcache_get(namei_oc, M_WAITOK); error = copyinstr(uap->path, path, MAXPATHLEN, NULL); if (error) goto done; if (uap->ctllen) { ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO); error = copyin(uap->ctl, ctl, uap->ctllen); if (error) goto done; } if (uap->buflen) buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO); /* * Validate the descriptor */ if (uap->fd >= 0) { fp = holdfp(td, uap->fd, -1); if (fp == NULL) { error = EBADF; goto done; } } else { fp = NULL; } /* * Execute the internal kernel function and clean up. */ error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &sysmsg->sysmsg_result); if (fp) dropfp(td, uap->fd, fp); if (error == 0 && sysmsg->sysmsg_result > 0) error = copyout(buf, uap->buf, sysmsg->sysmsg_result); done: if (path) objcache_put(namei_oc, path); if (ctl) kfree(ctl, M_TEMP); if (buf) kfree(buf, M_TEMP); return (error); } /* * Execute a mount control operation by resolving the path to a mount point * and calling vop_mountctl(). * * Use the mount point from the nch instead of the vnode so nullfs mounts * can properly spike the VOP. */ int kern_mountctl(const char *path, int op, struct file *fp, const void *ctl, int ctllen, void *buf, int buflen, int *res) { struct vnode *vp; struct nlookupdata nd; struct nchandle nch; struct mount *mp; int error; *res = 0; vp = NULL; error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW); if (error) return (error); error = nlookup(&nd); if (error) { nlookup_done(&nd); return (error); } error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); if (error) { nlookup_done(&nd); return (error); } /* * Yes, all this is needed to use the nch.mount below, because * we must maintain a ref on the mount to avoid ripouts (e.g. * due to heavy mount/unmount use by synth or poudriere). */ nch = nd.nl_nch; cache_zero(&nd.nl_nch); cache_unlock(&nch); nlookup_done(&nd); vn_unlock(vp); mp = nch.mount; /* * Must be the root of the filesystem */ if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) { cache_drop(&nch); vrele(vp); return (EINVAL); } if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) { kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n", path); cache_drop(&nch); vrele(vp); return (EINVAL); } error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen, buf, buflen, res); vrele(vp); cache_drop(&nch); return (error); } int kern_statfs(struct nlookupdata *nd, struct statfs *buf) { struct thread *td = curthread; struct proc *p = td->td_proc; struct mount *mp; struct statfs *sp; char *fullpath, *freepath; int error; if ((error = nlookup(nd)) != 0) return (error); mp = nd->nl_nch.mount; sp = &mp->mnt_stat; /* * Ignore refresh error, user should have visibility. * This can happen if a NFS mount goes bad (e.g. server * revokes perms or goes down). */ error = VFS_STATFS(mp, sp, nd->nl_cred); /* ignore error */ error = mount_path(p, mp, &fullpath, &freepath); if (error) return(error); bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); kfree(freepath, M_TEMP); sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; bcopy(sp, buf, sizeof(*buf)); /* Only root should have access to the fsid's. */ if (caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)) buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; return (0); } /* * statfs_args(char *path, struct statfs *buf) * * Get filesystem statistics. */ int sys_statfs(struct sysmsg *sysmsg, const struct statfs_args *uap) { struct nlookupdata nd; struct statfs buf; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_statfs(&nd, &buf); nlookup_done(&nd); if (error == 0) error = copyout(&buf, uap->buf, sizeof(*uap->buf)); return (error); } int kern_fstatfs(int fd, struct statfs *buf) { struct thread *td = curthread; struct proc *p = td->td_proc; struct file *fp; struct mount *mp; struct statfs *sp; char *fullpath, *freepath; int error; KKASSERT(p); if ((error = holdvnode(td, fd, &fp)) != 0) return (error); /* * Try to use mount info from any overlays rather than the * mount info for the underlying vnode, otherwise we will * fail when operating on null-mounted paths inside a chroot. */ if ((mp = fp->f_nchandle.mount) == NULL) mp = ((struct vnode *)fp->f_data)->v_mount; if (mp == NULL) { error = EBADF; goto done; } if (fp->f_cred == NULL) { error = EINVAL; goto done; } /* * Ignore refresh error, user should have visibility. * This can happen if a NFS mount goes bad (e.g. server * revokes perms or goes down). */ sp = &mp->mnt_stat; error = VFS_STATFS(mp, sp, fp->f_cred); if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0) goto done; bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); kfree(freepath, M_TEMP); sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; bcopy(sp, buf, sizeof(*buf)); /* Only root should have access to the fsid's. */ if (caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)) buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; error = 0; done: fdrop(fp); return (error); } /* * fstatfs_args(int fd, struct statfs *buf) * * Get filesystem statistics. */ int sys_fstatfs(struct sysmsg *sysmsg, const struct fstatfs_args *uap) { struct statfs buf; int error; error = kern_fstatfs(uap->fd, &buf); if (error == 0) error = copyout(&buf, uap->buf, sizeof(*uap->buf)); return (error); } int kern_statvfs(struct nlookupdata *nd, struct statvfs *buf) { struct mount *mp; struct statvfs *sp; int error; if ((error = nlookup(nd)) != 0) return (error); mp = nd->nl_nch.mount; sp = &mp->mnt_vstat; if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0) return (error); sp->f_flag = 0; if (mp->mnt_flag & MNT_RDONLY) sp->f_flag |= ST_RDONLY; if (mp->mnt_flag & MNT_NOSUID) sp->f_flag |= ST_NOSUID; bcopy(sp, buf, sizeof(*buf)); return (0); } /* * statfs_args(char *path, struct statfs *buf) * * Get filesystem statistics. */ int sys_statvfs(struct sysmsg *sysmsg, const struct statvfs_args *uap) { struct nlookupdata nd; struct statvfs buf; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_statvfs(&nd, &buf); nlookup_done(&nd); if (error == 0) error = copyout(&buf, uap->buf, sizeof(*uap->buf)); return (error); } int kern_fstatvfs(int fd, struct statvfs *buf) { struct thread *td = curthread; struct file *fp; struct mount *mp; struct statvfs *sp; int error; if ((error = holdvnode(td, fd, &fp)) != 0) return (error); if ((mp = fp->f_nchandle.mount) == NULL) mp = ((struct vnode *)fp->f_data)->v_mount; if (mp == NULL) { error = EBADF; goto done; } if (fp->f_cred == NULL) { error = EINVAL; goto done; } sp = &mp->mnt_vstat; if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0) goto done; sp->f_flag = 0; if (mp->mnt_flag & MNT_RDONLY) sp->f_flag |= ST_RDONLY; if (mp->mnt_flag & MNT_NOSUID) sp->f_flag |= ST_NOSUID; bcopy(sp, buf, sizeof(*buf)); error = 0; done: fdrop(fp); return (error); } /* * fstatfs_args(int fd, struct statfs *buf) * * Get filesystem statistics. */ int sys_fstatvfs(struct sysmsg *sysmsg, const struct fstatvfs_args *uap) { struct statvfs buf; int error; error = kern_fstatvfs(uap->fd, &buf); if (error == 0) error = copyout(&buf, uap->buf, sizeof(*uap->buf)); return (error); } /* * getfsstat_args(struct statfs *buf, long bufsize, int flags) * * Get statistics on all filesystems. */ struct getfsstat_info { struct statfs *sfsp; long count; long maxcount; int error; int flags; struct thread *td; }; static int getfsstat_callback(struct mount *, void *); int sys_getfsstat(struct sysmsg *sysmsg, const struct getfsstat_args *uap) { struct thread *td = curthread; struct getfsstat_info info; bzero(&info, sizeof(info)); info.maxcount = uap->bufsize / sizeof(struct statfs); info.sfsp = uap->buf; info.count = 0; info.flags = uap->flags; info.td = td; mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD); if (info.sfsp && info.count > info.maxcount) sysmsg->sysmsg_result = info.maxcount; else sysmsg->sysmsg_result = info.count; return (info.error); } static int getfsstat_callback(struct mount *mp, void *data) { struct getfsstat_info *info = data; struct statfs *sp; char *freepath; char *fullpath; int error; if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc)) return(0); if (info->sfsp && info->count < info->maxcount) { sp = &mp->mnt_stat; /* * If MNT_NOWAIT or MNT_LAZY is specified, do not * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY * overrides MNT_WAIT. * * Ignore refresh error, user should have visibility. * This can happen if a NFS mount goes bad (e.g. server * revokes perms or goes down). */ if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || (info->flags & MNT_WAIT)) && (error = VFS_STATFS(mp, sp, info->td->td_ucred))) { /* ignore error */ } sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; error = mount_path(info->td->td_proc, mp, &fullpath, &freepath); if (error) { info->error = error; return(-1); } bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); kfree(freepath, M_TEMP); error = copyout(sp, info->sfsp, sizeof(*sp)); if (error) { info->error = error; return (-1); } ++info->sfsp; } info->count++; return(0); } /* * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf, long bufsize, int flags) * * Get statistics on all filesystems. */ struct getvfsstat_info { struct statfs *sfsp; struct statvfs *vsfsp; long count; long maxcount; int error; int flags; struct thread *td; }; static int getvfsstat_callback(struct mount *, void *); int sys_getvfsstat(struct sysmsg *sysmsg, const struct getvfsstat_args *uap) { struct thread *td = curthread; struct getvfsstat_info info; bzero(&info, sizeof(info)); info.maxcount = uap->vbufsize / sizeof(struct statvfs); info.sfsp = uap->buf; info.vsfsp = uap->vbuf; info.count = 0; info.flags = uap->flags; info.td = td; mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD); if (info.vsfsp && info.count > info.maxcount) sysmsg->sysmsg_result = info.maxcount; else sysmsg->sysmsg_result = info.count; return (info.error); } static int getvfsstat_callback(struct mount *mp, void *data) { struct getvfsstat_info *info = data; struct statfs *sp; struct statvfs *vsp; char *freepath; char *fullpath; int error; if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc)) return(0); if (info->vsfsp && info->count < info->maxcount) { sp = &mp->mnt_stat; vsp = &mp->mnt_vstat; /* * If MNT_NOWAIT or MNT_LAZY is specified, do not * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY * overrides MNT_WAIT. * * Ignore refresh error, user should have visibility. * This can happen if a NFS mount goes bad (e.g. server * revokes perms or goes down). */ if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || (info->flags & MNT_WAIT)) && (error = VFS_STATFS(mp, sp, info->td->td_ucred))) { /* ignore error */ } sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || (info->flags & MNT_WAIT)) && (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) { /* ignore error */ } vsp->f_flag = 0; if (mp->mnt_flag & MNT_RDONLY) vsp->f_flag |= ST_RDONLY; if (mp->mnt_flag & MNT_NOSUID) vsp->f_flag |= ST_NOSUID; error = mount_path(info->td->td_proc, mp, &fullpath, &freepath); if (error) { info->error = error; return(-1); } bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); kfree(freepath, M_TEMP); error = copyout(sp, info->sfsp, sizeof(*sp)); if (error == 0) error = copyout(vsp, info->vsfsp, sizeof(*vsp)); if (error) { info->error = error; return (-1); } ++info->sfsp; ++info->vsfsp; } info->count++; return(0); } /* * fchdir_args(int fd) * * Change current working directory to a given file descriptor. */ int sys_fchdir(struct sysmsg *sysmsg, const struct fchdir_args *uap) { struct thread *td = curthread; struct proc *p = td->td_proc; struct filedesc *fdp = p->p_fd; struct vnode *vp, *ovp; struct mount *mp; struct file *fp; struct nchandle nch, onch, tnch; int error; if ((error = holdvnode(td, uap->fd, &fp)) != 0) return (error); lwkt_gettoken(&p->p_token); vp = (struct vnode *)fp->f_data; vref(vp); vn_lock(vp, LK_SHARED | LK_RETRY); if (fp->f_nchandle.ncp == NULL) error = ENOTDIR; else error = checkvp_chdir(vp, td); if (error) { vput(vp); goto done; } cache_copy(&fp->f_nchandle, &nch); /* * If the ncp has become a mount point, traverse through * the mount point. */ while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) && (mp = cache_findmount(&nch)) != NULL ) { error = nlookup_mp(mp, &tnch); if (error == 0) { cache_unlock(&tnch); /* leave ref intact */ vput(vp); vp = tnch.ncp->nc_vp; error = vget(vp, LK_SHARED); KKASSERT(error == 0); cache_drop(&nch); nch = tnch; } cache_dropmount(mp); } if (error == 0) { spin_lock(&fdp->fd_spin); ovp = fdp->fd_cdir; onch = fdp->fd_ncdir; fdp->fd_cdir = vp; fdp->fd_ncdir = nch; spin_unlock(&fdp->fd_spin); vn_unlock(vp); /* leave ref intact */ cache_drop(&onch); vrele(ovp); } else { cache_drop(&nch); vput(vp); } fdrop(fp); done: lwkt_reltoken(&p->p_token); return (error); } int kern_chdir(struct nlookupdata *nd) { struct thread *td = curthread; struct proc *p = td->td_proc; struct filedesc *fdp = p->p_fd; struct vnode *vp, *ovp; struct nchandle onch; int error; nd->nl_flags |= NLC_SHAREDLOCK; if ((error = nlookup(nd)) != 0) return (error); if ((vp = nd->nl_nch.ncp->nc_vp) == NULL) return (ENOENT); if ((error = vget(vp, LK_SHARED)) != 0) return (error); lwkt_gettoken(&p->p_token); error = checkvp_chdir(vp, td); vn_unlock(vp); if (error == 0) { spin_lock(&fdp->fd_spin); ovp = fdp->fd_cdir; onch = fdp->fd_ncdir; fdp->fd_ncdir = nd->nl_nch; fdp->fd_cdir = vp; spin_unlock(&fdp->fd_spin); cache_unlock(&nd->nl_nch); /* leave reference intact */ cache_drop(&onch); vrele(ovp); cache_zero(&nd->nl_nch); } else { vrele(vp); } lwkt_reltoken(&p->p_token); return (error); } /* * chdir_args(char *path) * * Change current working directory (``.''). */ int sys_chdir(struct sysmsg *sysmsg, const struct chdir_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_chdir(&nd); nlookup_done(&nd); return (error); } /* * Helper function for raised chroot(2) security function: Refuse if * any filedescriptors are open directories. */ static int chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp) { struct vnode *vp; struct file *fp; int error; int fd; for (fd = 0; fd < fdp->fd_nfiles ; fd++) { if ((error = holdvnode(td, fd, &fp)) != 0) continue; vp = (struct vnode *)fp->f_data; if (vp->v_type != VDIR) { fdrop(fp); continue; } fdrop(fp); return(EPERM); } return (0); } /* * This sysctl determines if we will allow a process to chroot(2) if it * has a directory open: * 0: disallowed for all processes. * 1: allowed for processes that were not already chroot(2)'ed. * 2: allowed for all processes. */ static int chroot_allow_open_directories = 1; SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW, &chroot_allow_open_directories, 0, ""); /* * chroot to the specified namecache entry. We obtain the vp from the * namecache data. The passed ncp must be locked and referenced and will * remain locked and referenced on return. */ int kern_chroot(struct nchandle *nch) { struct thread *td = curthread; struct proc *p = td->td_proc; struct filedesc *fdp = p->p_fd; struct vnode *vp; int error; /* * Only privileged user can chroot */ error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_CHROOT); if (error) return (error); /* * Disallow open directory descriptors (fchdir() breakouts). */ if (chroot_allow_open_directories == 0 || (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) { if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0) return (error); } if ((vp = nch->ncp->nc_vp) == NULL) return (ENOENT); if ((error = vget(vp, LK_SHARED)) != 0) return (error); /* * Check the validity of vp as a directory to change to and * associate it with rdir/jdir. */ error = checkvp_chdir(vp, td); vn_unlock(vp); /* leave reference intact */ if (error == 0) { lwkt_gettoken(&p->p_token); vrele(fdp->fd_rdir); fdp->fd_rdir = vp; /* reference inherited by fd_rdir */ cache_drop(&fdp->fd_nrdir); cache_copy(nch, &fdp->fd_nrdir); if (fdp->fd_jdir == NULL) { fdp->fd_jdir = vp; vref(fdp->fd_jdir); cache_copy(nch, &fdp->fd_njdir); } if ((p->p_flags & P_DIDCHROOT) == 0) { p->p_flags |= P_DIDCHROOT; if (p->p_depth <= 65535 - 32) p->p_depth += 32; } lwkt_reltoken(&p->p_token); } else { vrele(vp); } return (error); } /* * chroot_args(char *path) * * Change notion of root (``/'') directory. */ int sys_chroot(struct sysmsg *sysmsg, const struct chroot_args *uap) { struct thread *td __debugvar = curthread; struct nlookupdata nd; int error; KKASSERT(td->td_proc); error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) { nd.nl_flags |= NLC_EXEC; error = nlookup(&nd); if (error == 0) error = kern_chroot(&nd.nl_nch); } nlookup_done(&nd); return(error); } int sys_chroot_kernel(struct sysmsg *sysmsg, const struct chroot_kernel_args *uap) { struct thread *td = curthread; struct nlookupdata nd; struct nchandle *nch; struct vnode *vp; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error) goto error_nond; error = nlookup(&nd); if (error) goto error_out; nch = &nd.nl_nch; error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_CHROOT); if (error) goto error_out; if ((vp = nch->ncp->nc_vp) == NULL) { error = ENOENT; goto error_out; } if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0) goto error_out; vfs_cache_setroot(vp, cache_hold(nch)); error_out: nlookup_done(&nd); error_nond: return(error); } /* * Common routine for chroot and chdir. Given a locked, referenced vnode, * determine whether it is legal to chdir to the vnode. The vnode's state * is not changed by this call. */ static int checkvp_chdir(struct vnode *vp, struct thread *td) { int error; if (vp->v_type != VDIR) error = ENOTDIR; else error = VOP_EACCESS(vp, VEXEC, td->td_ucred); return (error); } int kern_open(struct nlookupdata *nd, int oflags, int mode, int *res) { struct thread *td = curthread; struct proc *p = td->td_proc; struct lwp *lp = td->td_lwp; struct filedesc *fdp = p->p_fd; int cmode, flags; struct file *nfp; struct file *fp; int type, indx, error = 0; struct flock lf; if ((oflags & O_ACCMODE) == O_ACCMODE) return (EINVAL); flags = FFLAGS(oflags); error = falloc(lp, &nfp, NULL); if (error) return (error); fp = nfp; cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT; /* * Call vn_open() to do the lookup and assign the vnode to the * file pointer. vn_open() does not change the ref count on fp * and the vnode, on success, will be inherited by the file pointer * and unlocked. * * Request a shared lock on the vnode if possible. * * When NLC_SHAREDLOCK is set we may still need an exclusive vnode * lock for O_RDWR opens on executables in order to avoid a VTEXT * detection race. The NLC_EXCLLOCK_IFEXEC handles this case. * * NOTE: We need a flag to separate terminal vnode locking from * parent locking. O_CREAT needs parent locking, but O_TRUNC * and O_RDWR only need to lock the terminal vnode exclusively. */ nd->nl_flags |= NLC_LOCKVP; if ((flags & (O_CREAT|O_TRUNC)) == 0) { nd->nl_flags |= NLC_SHAREDLOCK; if (flags & O_RDWR) nd->nl_flags |= NLC_EXCLLOCK_IFEXEC; } /* * Issue the vn_open, passing in the referenced fp. the vn_open() * is allowed to replace fp by fdrop()ing it and returning its own * referenced fp. */ nfp = fp; error = vn_open(nd, &nfp, flags, cmode); fp = nfp; nlookup_done(nd); /* * Deal with any error condition */ if (error) { fdrop(fp); /* our ref */ if (error == ERESTART) error = EINTR; return (error); } /* * Reserve a file descriptor. */ if ((error = fdalloc(p, 0, &indx)) != 0) { fdrop(fp); return (error); } /* * Handle advisory lock flags. This is only supported with vnodes. * For things like /dev/fd/N we might not actually get a vnode. */ if ((flags & (O_EXLOCK | O_SHLOCK)) && fp->f_type == DTYPE_VNODE) { struct vnode *vp; vp = (struct vnode *)fp->f_data; vref(vp); lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; if (flags & O_EXLOCK) lf.l_type = F_WRLCK; else lf.l_type = F_RDLCK; if (flags & FNONBLOCK) type = 0; else type = F_WAIT; error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type); if (error) { /* * lock request failed. Clean up the reserved * descriptor. */ vrele(vp); fsetfd(fdp, NULL, indx); fdrop(fp); return (error); } atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */ vrele(vp); } /* * release our private reference, leaving the one associated with the * descriptor table intact. */ if (oflags & O_CLOEXEC) fdp->fd_files[indx].fileflags |= UF_EXCLOSE; if (oflags & O_CLOFORK) fdp->fd_files[indx].fileflags |= UF_FOCLOSE; fsetfd(fdp, fp, indx); fdrop(fp); *res = indx; return (error); } /* * open_args(char *path, int flags, int mode) * * Check permissions, allocate an open file structure, * and call the device open routine if any. */ int sys_open(struct sysmsg *sysmsg, const struct open_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) { error = kern_open(&nd, uap->flags, uap->mode, &sysmsg->sysmsg_result); } nlookup_done(&nd); return (error); } /* * openat_args(int fd, char *path, int flags, int mode) */ int sys_openat(struct sysmsg *sysmsg, const struct openat_args *uap) { struct nlookupdata nd; int error; struct file *fp; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); if (error == 0) { error = kern_open(&nd, uap->flags, uap->mode, &sysmsg->sysmsg_result); } nlookup_done_at(&nd, fp); return (error); } int kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor) { struct thread *td = curthread; struct proc *p = td->td_proc; struct vnode *vp; struct vattr vattr; int error; int whiteout = 0; KKASSERT(p); VATTR_NULL(&vattr); vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; vattr.va_rmajor = rmajor; vattr.va_rminor = rminor; switch (mode & S_IFMT) { case S_IFMT: /* used by badsect to flag bad sectors */ error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_MKNOD_BAD); vattr.va_type = VBAD; break; case S_IFCHR: error = caps_priv_check_td(td, SYSCAP_NOVFS_MKNOD_DEV); vattr.va_type = VCHR; break; case S_IFBLK: error = caps_priv_check_td(td, SYSCAP_NOVFS_MKNOD_DEV); vattr.va_type = VBLK; break; case S_IFWHT: error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_MKNOD_WHT); whiteout = 1; break; case S_IFDIR: /* special directories support for HAMMER */ error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_MKNOD_DIR); vattr.va_type = VDIR; break; case S_IFIFO: return (kern_mkfifo(nd, mode)); break; default: error = EINVAL; break; } if (error) return (error); bwillinode(1); nd->nl_flags |= NLC_CREATE | NLC_REFDVP; if ((error = nlookup(nd)) != 0) return (error); if (nd->nl_nch.ncp->nc_vp) return (EEXIST); if (nd->nl_dvp == NULL) return (EINVAL); if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); if (whiteout) { error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp, nd->nl_cred, NAMEI_CREATE); } else { vp = NULL; error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr); if (error == 0) vput(vp); } return (error); } /* * mknod_args(char *path, int mode, int dev) * * Create a special file. */ int sys_mknod(struct sysmsg *sysmsg, const struct mknod_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) { error = kern_mknod(&nd, uap->mode, umajor(uap->dev), uminor(uap->dev)); } nlookup_done(&nd); return (error); } /* * mknodat_args(int fd, char *path, mode_t mode, dev_t dev) * * Create a special file. The path is relative to the directory associated * with fd. */ int sys_mknodat(struct sysmsg *sysmsg, const struct mknodat_args *uap) { struct nlookupdata nd; struct file *fp; int error; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); if (error == 0) { error = kern_mknod(&nd, uap->mode, umajor(uap->dev), uminor(uap->dev)); } nlookup_done_at(&nd, fp); return (error); } int kern_mkfifo(struct nlookupdata *nd, int mode) { struct thread *td = curthread; struct proc *p = td->td_proc; struct vattr vattr; struct vnode *vp; int error; bwillinode(1); nd->nl_flags |= NLC_CREATE | NLC_REFDVP; if ((error = nlookup(nd)) != 0) return (error); if (nd->nl_nch.ncp->nc_vp) return (EEXIST); if (nd->nl_dvp == NULL) return (EINVAL); if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); VATTR_NULL(&vattr); vattr.va_type = VFIFO; vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; vp = NULL; error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr); if (error == 0) vput(vp); return (error); } /* * mkfifo_args(char *path, int mode) * * Create a named pipe. */ int sys_mkfifo(struct sysmsg *sysmsg, const struct mkfifo_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_mkfifo(&nd, uap->mode); nlookup_done(&nd); return (error); } /* * mkfifoat_args(int fd, char *path, mode_t mode) * * Create a named pipe. The path is relative to the directory associated * with fd. */ int sys_mkfifoat(struct sysmsg *sysmsg, const struct mkfifoat_args *uap) { struct nlookupdata nd; struct file *fp; int error; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_mkfifo(&nd, uap->mode); nlookup_done_at(&nd, fp); return (error); } static int hardlink_check_uid = 0; SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW, &hardlink_check_uid, 0, "Unprivileged processes cannot create hard links to files owned by other " "users"); static int hardlink_check_gid = 0; SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW, &hardlink_check_gid, 0, "Unprivileged processes cannot create hard links to files owned by other " "groups"); static int can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred) { struct vattr va; int error; /* * Shortcut if disabled */ if (hardlink_check_uid == 0 && hardlink_check_gid == 0) return (0); /* * Privileged user can always hardlink */ if (caps_priv_check(cred, SYSCAP_NOVFS_LINK) == 0) return (0); /* * Otherwise only if the originating file is owned by the * same user or group. Note that any group is allowed if * the file is owned by the caller. */ error = VOP_GETATTR(vp, &va); if (error != 0) return (error); if (hardlink_check_uid) { if (cred->cr_uid != va.va_uid) return (EPERM); } if (hardlink_check_gid) { if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred)) return (EPERM); } return (0); } int kern_link(struct nlookupdata *nd, struct nlookupdata *linknd) { struct thread *td = curthread; struct vnode *vp; int error; /* * Lookup the source and obtained a locked vnode. * * You may only hardlink a file which you have write permission * on or which you own. * * XXX relookup on vget failure / race ? */ bwillinode(1); nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK; if ((error = nlookup(nd)) != 0) return (error); vp = nd->nl_nch.ncp->nc_vp; KKASSERT(vp != NULL); if (vp->v_type == VDIR) return (EPERM); /* POSIX */ if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); if ((error = vget(vp, LK_EXCLUSIVE)) != 0) return (error); /* * Unlock the source so we can lookup the target without deadlocking * (XXX vp is locked already, possible other deadlock?). The target * must not exist. */ KKASSERT(nd->nl_flags & NLC_NCPISLOCKED); nd->nl_flags &= ~NLC_NCPISLOCKED; cache_unlock(&nd->nl_nch); vn_unlock(vp); linknd->nl_flags |= NLC_CREATE | NLC_REFDVP; if ((error = nlookup(linknd)) != 0) { vrele(vp); return (error); } if (linknd->nl_nch.ncp->nc_vp) { vrele(vp); return (EEXIST); } if (linknd->nl_dvp == NULL) { vrele(vp); return (EINVAL); } VFS_MODIFYING(vp->v_mount); error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); if (error) { vrele(vp); return (error); } /* * Finally run the new API VOP. */ error = can_hardlink(vp, td, td->td_ucred); if (error == 0) { error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp, vp, linknd->nl_cred); } vput(vp); return (error); } /* * link_args(char *path, char *link) * * Make a hard file link. */ int sys_link(struct sysmsg *sysmsg, const struct link_args *uap) { struct nlookupdata nd, linknd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) { error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0); if (error == 0) error = kern_link(&nd, &linknd); nlookup_done(&linknd); } nlookup_done(&nd); return (error); } /* * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags) * * Make a hard file link. The path1 argument is relative to the directory * associated with fd1, and similarly the path2 argument is relative to * the directory associated with fd2. */ int sys_linkat(struct sysmsg *sysmsg, const struct linkat_args *uap) { struct nlookupdata nd, linknd; struct file *fp1, *fp2; int error; error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE, (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0); if (error == 0) { error = nlookup_init_at(&linknd, &fp2, uap->fd2, uap->path2, UIO_USERSPACE, 0); if (error == 0) error = kern_link(&nd, &linknd); nlookup_done_at(&linknd, fp2); } nlookup_done_at(&nd, fp1); return (error); } int kern_symlink(struct nlookupdata *nd, char *path, int mode) { struct vattr vattr; struct vnode *vp; struct vnode *dvp; int error; bwillinode(1); nd->nl_flags |= NLC_CREATE | NLC_REFDVP; if ((error = nlookup(nd)) != 0) return (error); if (nd->nl_nch.ncp->nc_vp) return (EEXIST); if (nd->nl_dvp == NULL) return (EINVAL); if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); dvp = nd->nl_dvp; VATTR_NULL(&vattr); vattr.va_mode = mode; error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path); if (error == 0) vput(vp); return (error); } /* * symlink(char *path, char *link) * * Make a symbolic link. */ int sys_symlink(struct sysmsg *sysmsg, const struct symlink_args *uap) { struct thread *td = curthread; struct nlookupdata nd; char *path; int error; int mode; path = objcache_get(namei_oc, M_WAITOK); error = copyinstr(uap->path, path, MAXPATHLEN, NULL); if (error == 0) { error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0); if (error == 0) { mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask; error = kern_symlink(&nd, path, mode); } nlookup_done(&nd); } objcache_put(namei_oc, path); return (error); } /* * symlinkat_args(char *path1, int fd, char *path2) * * Make a symbolic link. The path2 argument is relative to the directory * associated with fd. */ int sys_symlinkat(struct sysmsg *sysmsg, const struct symlinkat_args *uap) { struct thread *td = curthread; struct nlookupdata nd; struct file *fp; char *path1; int error; int mode; path1 = objcache_get(namei_oc, M_WAITOK); error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL); if (error == 0) { error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2, UIO_USERSPACE, 0); if (error == 0) { mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask; error = kern_symlink(&nd, path1, mode); } nlookup_done_at(&nd, fp); } objcache_put(namei_oc, path1); return (error); } /* * undelete_args(char *path) * * Delete a whiteout from the filesystem. */ int sys_undelete(struct sysmsg *sysmsg, const struct undelete_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); bwillinode(1); nd.nl_flags |= NLC_DELETE | NLC_REFDVP; if (error == 0) error = nlookup(&nd); if (error == 0 && nd.nl_dvp == NULL) error = EINVAL; if (error == 0) error = ncp_writechk(&nd.nl_nch); if (error == 0) { error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred, NAMEI_DELETE); } nlookup_done(&nd); return (error); } int kern_unlink(struct nlookupdata *nd) { int error; bwillinode(1); nd->nl_flags |= NLC_DELETE | NLC_REFDVP; if ((error = nlookup(nd)) != 0) return (error); if (nd->nl_dvp == NULL) return EINVAL; if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred); return (error); } /* * unlink_args(char *path) * * Delete a name from the filesystem. */ int sys_unlink(struct sysmsg *sysmsg, const struct unlink_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_unlink(&nd); nlookup_done(&nd); return (error); } /* * unlinkat_args(int fd, char *path, int flags) * * Delete the file or directory entry pointed to by fd/path. */ int sys_unlinkat(struct sysmsg *sysmsg, const struct unlinkat_args *uap) { struct nlookupdata nd; struct file *fp; int error; if (uap->flags & ~AT_REMOVEDIR) return (EINVAL); error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); if (error == 0) { if (uap->flags & AT_REMOVEDIR) error = kern_rmdir(&nd); else error = kern_unlink(&nd); } nlookup_done_at(&nd, fp); return (error); } int kern_lseek(int fd, off_t offset, int whence, off_t *res) { struct thread *td = curthread; struct file *fp; int error; fp = holdfp(td, fd, -1); if (fp == NULL) return (EBADF); error = fo_seek(fp, offset, whence, res); dropfp(td, fd, fp); return (error); } /* * lseek_args(int fd, int pad, off_t offset, int whence) * * Reposition read/write file offset. */ int sys_lseek(struct sysmsg *sysmsg, const struct lseek_args *uap) { int error; error = kern_lseek(uap->fd, uap->offset, uap->whence, &sysmsg->sysmsg_offset); return (error); } /* * Check if current process can access given file. amode is a bitmask of *_OK * access bits. flags is a bitmask of AT_* flags. */ int kern_access(struct nlookupdata *nd, int amode, int flags) { struct vnode *vp; int error, mode; if (flags & ~AT_EACCESS) return (EINVAL); nd->nl_flags |= NLC_SHAREDLOCK; if ((error = nlookup(nd)) != 0) return (error); if ((amode & W_OK) && (error = ncp_writechk(&nd->nl_nch)) != 0) return (error); retry: error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp); if (error) return (error); /* Flags == 0 means only check for existence. */ if (amode) { mode = 0; if (amode & R_OK) mode |= VREAD; if (amode & W_OK) mode |= VWRITE; if (amode & X_OK) mode |= VEXEC; if ((mode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0) { error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred); } /* * If the file handle is stale we have to re-resolve the * entry with the ncp held exclusively. This is a hack * at the moment. */ if (error == ESTALE) { u_int dummy_gen; vput(vp); cache_unlock(&nd->nl_nch); cache_lock(&nd->nl_nch); dummy_gen = nd->nl_nch.ncp->nc_generation; cache_setunresolved(&nd->nl_nch); error = cache_resolve(&nd->nl_nch, &dummy_gen, nd->nl_cred); if (error == 0) { vp = NULL; goto retry; } return(error); } } vput(vp); return (error); } /* * access_args(char *path, int flags) * * Check access permissions. */ int sys_access(struct sysmsg *sysmsg, const struct access_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_access(&nd, uap->flags, 0); nlookup_done(&nd); return (error); } /* * eaccess_args(char *path, int flags) * * Check access permissions. */ int sys_eaccess(struct sysmsg *sysmsg, const struct eaccess_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_access(&nd, uap->flags, AT_EACCESS); nlookup_done(&nd); return (error); } /* * faccessat_args(int fd, char *path, int amode, int flags) * * Check access permissions. */ int sys_faccessat(struct sysmsg *sysmsg, const struct faccessat_args *uap) { struct nlookupdata nd; struct file *fp; int error; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_access(&nd, uap->amode, uap->flags); nlookup_done_at(&nd, fp); return (error); } int kern_stat(struct nlookupdata *nd, struct stat *st) { int error; struct vnode *vp; nd->nl_flags |= NLC_SHAREDLOCK; if ((error = nlookup(nd)) != 0) return (error); again: if ((vp = nd->nl_nch.ncp->nc_vp) == NULL) return (ENOENT); #if 1 error = cache_vref(&nd->nl_nch, NULL, &vp); #else error = vget(vp, LK_SHARED); #endif if (error) return (error); error = vn_stat(vp, st, nd->nl_cred); /* * If the file handle is stale we have to re-resolve the * entry with the ncp held exclusively. This is a hack * at the moment. */ if (error == ESTALE) { u_int dummy_gen; #if 1 vrele(vp); #else vput(vp); #endif cache_unlock(&nd->nl_nch); cache_lock(&nd->nl_nch); dummy_gen = nd->nl_nch.ncp->nc_generation; cache_setunresolved(&nd->nl_nch); error = cache_resolve(&nd->nl_nch, &dummy_gen, nd->nl_cred); if (error == 0) goto again; } else { #if 1 vrele(vp); #else vput(vp); #endif } return (error); } /* * stat_args(char *path, struct stat *ub) * * Get file status; this version follows links. */ int sys_stat(struct sysmsg *sysmsg, const struct stat_args *uap) { struct nlookupdata nd; struct stat st; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) { error = kern_stat(&nd, &st); if (error == 0) error = copyout(&st, uap->ub, sizeof(*uap->ub)); } nlookup_done(&nd); return (error); } /* * lstat_args(char *path, struct stat *ub) * * Get file status; this version does not follow links. */ int sys_lstat(struct sysmsg *sysmsg, const struct lstat_args *uap) { struct nlookupdata nd; struct stat st; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) { error = kern_stat(&nd, &st); if (error == 0) error = copyout(&st, uap->ub, sizeof(*uap->ub)); } nlookup_done(&nd); return (error); } /* * fstatat_args(int fd, char *path, struct stat *sb, int flags) * * Get status of file pointed to by fd/path. */ int sys_fstatat(struct sysmsg *sysmsg, const struct fstatat_args *uap) { struct nlookupdata nd; struct stat st; int error; int flags; struct file *fp; if (uap->flags & ~AT_SYMLINK_NOFOLLOW) return (EINVAL); flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, flags); if (error == 0) { error = kern_stat(&nd, &st); if (error == 0) error = copyout(&st, uap->sb, sizeof(*uap->sb)); } nlookup_done_at(&nd, fp); return (error); } static int kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp) { struct nlookupdata nd; struct vnode *vp; int error; vp = NULL; error = nlookup_init(&nd, path, UIO_USERSPACE, flags); if (error == 0) error = nlookup(&nd); if (error == 0) error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); nlookup_done(&nd); if (error == 0) { error = VOP_PATHCONF(vp, name, sysmsg_regp); vput(vp); } return (error); } /* * pathconf_Args(char *path, int name) * * Get configurable pathname variables. */ int sys_pathconf(struct sysmsg *sysmsg, const struct pathconf_args *uap) { return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW, &sysmsg->sysmsg_reg)); } /* * lpathconf_Args(char *path, int name) * * Get configurable pathname variables, but don't follow symlinks. */ int sys_lpathconf(struct sysmsg *sysmsg, const struct lpathconf_args *uap) { return (kern_pathconf(uap->path, uap->name, 0, &sysmsg->sysmsg_reg)); } /* * XXX: daver * kern_readlink isn't properly split yet. There is a copyin burried * in VOP_READLINK(). */ int kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res) { struct thread *td = curthread; struct vnode *vp; struct iovec aiov; struct uio auio; int error; nd->nl_flags |= NLC_SHAREDLOCK; if ((error = nlookup(nd)) != 0) return (error); error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp); if (error) return (error); if (vp->v_type != VLNK) { error = EINVAL; } else { aiov.iov_base = buf; aiov.iov_len = count; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = 0; auio.uio_rw = UIO_READ; auio.uio_segflg = UIO_USERSPACE; auio.uio_td = td; auio.uio_resid = count; error = VOP_READLINK(vp, &auio, td->td_ucred); } vput(vp); *res = count - auio.uio_resid; return (error); } /* * readlink_args(char *path, char *buf, int count) * * Return target name of a symbolic link. */ int sys_readlink(struct sysmsg *sysmsg, const struct readlink_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) { error = kern_readlink(&nd, uap->buf, uap->count, &sysmsg->sysmsg_result); } nlookup_done(&nd); return (error); } /* * readlinkat_args(int fd, char *path, char *buf, size_t bufsize) * * Return target name of a symbolic link. The path is relative to the * directory associated with fd. */ int sys_readlinkat(struct sysmsg *sysmsg, const struct readlinkat_args *uap) { struct nlookupdata nd; struct file *fp; int error; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); if (error == 0) { error = kern_readlink(&nd, uap->buf, uap->bufsize, &sysmsg->sysmsg_result); } nlookup_done_at(&nd, fp); return (error); } static int setfflags(struct vnode *vp, u_long flags) { struct thread *td = curthread; int error; struct vattr vattr; /* * Prevent non-root users from setting flags on devices. When * a device is reused, users can retain ownership of the device * if they are allowed to set flags and programs assume that * chown can't fail when done as root. */ if ((vp->v_type == VCHR || vp->v_type == VBLK) && ((error = caps_priv_check(td->td_ucred, SYSCAP_NOVFS_CHFLAGS_DEV)) != 0)) { return (error); } /* * note: vget is required for any operation that might mod the vnode * so VINACTIVE is properly cleared. */ if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { VATTR_NULL(&vattr); vattr.va_flags = flags; error = VOP_SETATTR(vp, &vattr, td->td_ucred); vput(vp); } return (error); } /* * chflags(const char *path, u_long flags) * * Change flags of a file given a path name. */ int sys_chflags(struct sysmsg *sysmsg, const struct chflags_args *uap) { struct nlookupdata nd; struct vnode *vp; int error; vp = NULL; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = ncp_writechk(&nd.nl_nch); if (error == 0) error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); nlookup_done(&nd); if (error == 0) { error = setfflags(vp, uap->flags); vrele(vp); } return (error); } /* * lchflags(const char *path, u_long flags) * * Change flags of a file given a path name, but don't follow symlinks. */ int sys_lchflags(struct sysmsg *sysmsg, const struct lchflags_args *uap) { struct nlookupdata nd; struct vnode *vp; int error; vp = NULL; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = nlookup(&nd); if (error == 0) error = ncp_writechk(&nd.nl_nch); if (error == 0) error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); nlookup_done(&nd); if (error == 0) { error = setfflags(vp, uap->flags); vrele(vp); } return (error); } /* * fchflags_args(int fd, u_flags flags) * * Change flags of a file given a file descriptor. */ int sys_fchflags(struct sysmsg *sysmsg, const struct fchflags_args *uap) { struct thread *td = curthread; struct file *fp; int error; if ((error = holdvnode(td, uap->fd, &fp)) != 0) return (error); if (fp->f_nchandle.ncp) error = ncp_writechk(&fp->f_nchandle); if (error == 0) error = setfflags((struct vnode *) fp->f_data, uap->flags); fdrop(fp); return (error); } /* * chflagsat_args(int fd, const char *path, u_long flags, int atflags) * change flags given a pathname relative to a filedescriptor */ int sys_chflagsat(struct sysmsg *sysmsg, const struct chflagsat_args *uap) { struct nlookupdata nd; struct vnode *vp; struct file *fp; int error; int lookupflags; if (uap->atflags & ~AT_SYMLINK_NOFOLLOW) return (EINVAL); lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; vp = NULL; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags); if (error == 0) error = nlookup(&nd); if (error == 0) error = ncp_writechk(&nd.nl_nch); if (error == 0) error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); nlookup_done_at(&nd, fp); if (error == 0) { error = setfflags(vp, uap->flags); vrele(vp); } return (error); } static int setfmode(struct vnode *vp, int mode) { struct thread *td = curthread; int error; struct vattr vattr; /* * note: vget is required for any operation that might mod the vnode * so VINACTIVE is properly cleared. */ if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { VATTR_NULL(&vattr); vattr.va_mode = mode & ALLPERMS; error = VOP_SETATTR(vp, &vattr, td->td_ucred); cache_inval_wxok(vp); vput(vp); } return error; } int kern_chmod(struct nlookupdata *nd, int mode) { struct vnode *vp; int error; if ((error = nlookup(nd)) != 0) return (error); if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) return (error); if ((error = ncp_writechk(&nd->nl_nch)) == 0) error = setfmode(vp, mode); vrele(vp); return (error); } /* * chmod_args(char *path, int mode) * * Change mode of a file given path name. */ int sys_chmod(struct sysmsg *sysmsg, const struct chmod_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_chmod(&nd, uap->mode); nlookup_done(&nd); return (error); } /* * lchmod_args(char *path, int mode) * * Change mode of a file given path name (don't follow links.) */ int sys_lchmod(struct sysmsg *sysmsg, const struct lchmod_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_chmod(&nd, uap->mode); nlookup_done(&nd); return (error); } /* * fchmod_args(int fd, int mode) * * Change mode of a file given a file descriptor. */ int sys_fchmod(struct sysmsg *sysmsg, const struct fchmod_args *uap) { struct thread *td = curthread; struct file *fp; int error; if ((error = holdvnode(td, uap->fd, &fp)) != 0) return (error); if (fp->f_nchandle.ncp) error = ncp_writechk(&fp->f_nchandle); if (error == 0) error = setfmode((struct vnode *)fp->f_data, uap->mode); fdrop(fp); return (error); } /* * fchmodat_args(char *path, int mode) * * Change mode of a file pointed to by fd/path. */ int sys_fchmodat(struct sysmsg *sysmsg, const struct fchmodat_args *uap) { struct nlookupdata nd; struct file *fp; int error; int flags; if (uap->flags & ~AT_SYMLINK_NOFOLLOW) return (EINVAL); flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, flags); if (error == 0) error = kern_chmod(&nd, uap->mode); nlookup_done_at(&nd, fp); return (error); } static int setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid) { struct thread *td = curthread; int error; struct vattr vattr; uid_t o_uid; gid_t o_gid; uint64_t size; /* * note: vget is required for any operation that might mod the vnode * so VINACTIVE is properly cleared. */ if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { if ((error = VOP_GETATTR(vp, &vattr)) != 0) return error; o_uid = vattr.va_uid; o_gid = vattr.va_gid; size = vattr.va_size; VATTR_NULL(&vattr); vattr.va_uid = uid; vattr.va_gid = gid; error = VOP_SETATTR(vp, &vattr, td->td_ucred); vput(vp); } if (error == 0) { if (uid == -1) uid = o_uid; if (gid == -1) gid = o_gid; VFS_ACCOUNT(mp, o_uid, o_gid, -size); VFS_ACCOUNT(mp, uid, gid, size); } return error; } int kern_chown(struct nlookupdata *nd, int uid, int gid) { struct vnode *vp; int error; if ((error = nlookup(nd)) != 0) return (error); if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) return (error); if ((error = ncp_writechk(&nd->nl_nch)) == 0) error = setfown(nd->nl_nch.mount, vp, uid, gid); vrele(vp); return (error); } /* * chown(char *path, int uid, int gid) * * Set ownership given a path name. */ int sys_chown(struct sysmsg *sysmsg, const struct chown_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_chown(&nd, uap->uid, uap->gid); nlookup_done(&nd); return (error); } /* * lchown_args(char *path, int uid, int gid) * * Set ownership given a path name, do not cross symlinks. */ int sys_lchown(struct sysmsg *sysmsg, const struct lchown_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_chown(&nd, uap->uid, uap->gid); nlookup_done(&nd); return (error); } /* * fchown_args(int fd, int uid, int gid) * * Set ownership given a file descriptor. */ int sys_fchown(struct sysmsg *sysmsg, const struct fchown_args *uap) { struct thread *td = curthread; struct proc *p = td->td_proc; struct file *fp; int error; if ((error = holdvnode(td, uap->fd, &fp)) != 0) return (error); if (fp->f_nchandle.ncp) error = ncp_writechk(&fp->f_nchandle); if (error == 0) error = setfown(p->p_fd->fd_ncdir.mount, (struct vnode *)fp->f_data, uap->uid, uap->gid); fdrop(fp); return (error); } /* * fchownat(int fd, char *path, int uid, int gid, int flags) * * Set ownership of file pointed to by fd/path. */ int sys_fchownat(struct sysmsg *sysmsg, const struct fchownat_args *uap) { struct nlookupdata nd; struct file *fp; int error; int flags; if (uap->flags & ~AT_SYMLINK_NOFOLLOW) return (EINVAL); flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, flags); if (error == 0) error = kern_chown(&nd, uap->uid, uap->gid); nlookup_done_at(&nd, fp); return (error); } static int getutimes(struct timeval *tvp, struct timespec *tsp) { struct timeval tv[2]; int error; if (tvp == NULL) { microtime(&tv[0]); TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]); tsp[1] = tsp[0]; } else { if ((error = itimerfix(tvp)) != 0) return (error); TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]); TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]); } return 0; } static int getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag) { struct timespec tsnow; int error; *nullflag = 0; nanotime(&tsnow); if (ts == NULL) { newts[0] = tsnow; newts[1] = tsnow; *nullflag = 1; return (0); } newts[0] = ts[0]; newts[1] = ts[1]; if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT) { newts[0].tv_sec = VNOVAL; newts[1].tv_sec = VNOVAL; return (0); } if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW) *nullflag = 1; if (newts[0].tv_nsec == UTIME_OMIT) newts[0].tv_sec = VNOVAL; else if (newts[0].tv_nsec == UTIME_NOW) newts[0] = tsnow; else if ((error = itimespecfix(&newts[0])) != 0) return (error); if (newts[1].tv_nsec == UTIME_OMIT) newts[1].tv_sec = VNOVAL; else if (newts[1].tv_nsec == UTIME_NOW) newts[1] = tsnow; else if ((error = itimespecfix(&newts[1])) != 0) return (error); return (0); } static int setutimes(struct vnode *vp, struct vattr *vattr, const struct timespec *ts, int nullflag) { struct thread *td = curthread; int error; VATTR_NULL(vattr); vattr->va_atime = ts[0]; vattr->va_mtime = ts[1]; if (nullflag) vattr->va_vaflags |= VA_UTIMES_NULL; error = VOP_SETATTR(vp, vattr, td->td_ucred); return error; } int kern_utimes(struct nlookupdata *nd, struct timeval *tptr) { struct timespec ts[2]; int error; if (tptr) { if ((error = getutimes(tptr, ts)) != 0) return (error); } error = kern_utimensat(nd, tptr ? ts : NULL, 0); return (error); } /* * utimes_args(char *path, struct timeval *tptr) * * Set the access and modification times of a file. */ int sys_utimes(struct sysmsg *sysmsg, const struct utimes_args *uap) { struct timeval tv[2]; struct nlookupdata nd; int error; if (uap->tptr) { error = copyin(uap->tptr, tv, sizeof(tv)); if (error) return (error); } error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_utimes(&nd, uap->tptr ? tv : NULL); nlookup_done(&nd); return (error); } /* * lutimes_args(char *path, struct timeval *tptr) * * Set the access and modification times of a file. */ int sys_lutimes(struct sysmsg *sysmsg, const struct lutimes_args *uap) { struct timeval tv[2]; struct nlookupdata nd; int error; if (uap->tptr) { error = copyin(uap->tptr, tv, sizeof(tv)); if (error) return (error); } error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_utimes(&nd, uap->tptr ? tv : NULL); nlookup_done(&nd); return (error); } /* * Set utimes on a file descriptor. The creds used to open the * file are used to determine whether the operation is allowed * or not. */ int kern_futimens(int fd, struct timespec *ts) { struct thread *td = curthread; struct timespec newts[2]; struct file *fp; struct vnode *vp; struct vattr vattr; struct vattr_lite lva; int nullflag; int error; error = getutimens(ts, newts, &nullflag); if (error) return (error); if ((error = holdvnode(td, fd, &fp)) != 0) return (error); if (fp->f_nchandle.ncp) error = ncp_writechk(&fp->f_nchandle); if (error == 0) { vp = fp->f_data; error = vget(vp, LK_EXCLUSIVE); if (error == 0) { error = VOP_GETATTR_FP(vp, &vattr, fp); if (error == 0) { lva.va_type = vattr.va_type; lva.va_nlink = vattr.va_nlink; lva.va_mode = vattr.va_mode; lva.va_uid = vattr.va_uid; lva.va_gid = vattr.va_gid; lva.va_size = vattr.va_size; lva.va_flags = vattr.va_flags; error = naccess_lva(&lva, NLC_OWN | NLC_WRITE, fp->f_cred); } if (error == 0) { error = setutimes(vp, &vattr, newts, nullflag); } vput(vp); } } fdrop(fp); return (error); } /* * futimens_args(int fd, struct timespec *ts) * * Set the access and modification times of a file. */ int sys_futimens(struct sysmsg *sysmsg, const struct futimens_args *uap) { struct timespec ts[2]; int error; if (uap->ts) { error = copyin(uap->ts, ts, sizeof(ts)); if (error) return (error); } error = kern_futimens(uap->fd, uap->ts ? ts : NULL); return (error); } int kern_futimes(int fd, struct timeval *tptr) { struct timespec ts[2]; int error; if (tptr) { if ((error = getutimes(tptr, ts)) != 0) return (error); } error = kern_futimens(fd, tptr ? ts : NULL); return (error); } /* * futimes_args(int fd, struct timeval *tptr) * * Set the access and modification times of a file. */ int sys_futimes(struct sysmsg *sysmsg, const struct futimes_args *uap) { struct timeval tv[2]; int error; if (uap->tptr) { error = copyin(uap->tptr, tv, sizeof(tv)); if (error) return (error); } error = kern_futimes(uap->fd, uap->tptr ? tv : NULL); return (error); } /* * futimesat_args(int fd, const char *path, struct timeval *tptr) * * Set the access and modification times of a file. */ int sys_futimesat(struct sysmsg *sysmsg, const struct futimesat_args *uap) { struct timespec ts[2]; struct nlookupdata nd; struct file *fp; int error; if (uap->tptr) { struct timeval tv[2]; if ((error = copyin(uap->tptr, tv, sizeof(tv))) != 0) return error; if ((error = getutimes(tv, ts)) != 0) return error; } error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_utimensat(&nd, uap->tptr ? ts : NULL, 0); nlookup_done_at(&nd, fp); return (error); } int kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags) { struct timespec newts[2]; struct vnode *vp; struct vattr vattr; int nullflag; int error; if (flags & ~AT_SYMLINK_NOFOLLOW) return (EINVAL); error = getutimens(ts, newts, &nullflag); if (error) return (error); nd->nl_flags |= NLC_OWN | NLC_WRITE; if ((error = nlookup(nd)) != 0) return (error); if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) return (error); if ((error = vn_writechk(vp)) == 0) { error = vget(vp, LK_EXCLUSIVE); if (error == 0) { error = setutimes(vp, &vattr, newts, nullflag); vput(vp); } } vrele(vp); return (error); } /* * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags); * * Set file access and modification times of a file. */ int sys_utimensat(struct sysmsg *sysmsg, const struct utimensat_args *uap) { struct timespec ts[2]; struct nlookupdata nd; struct file *fp; int error; int flags; if (uap->ts) { error = copyin(uap->ts, ts, sizeof(ts)); if (error) return (error); } flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, flags); if (error == 0) error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags); nlookup_done_at(&nd, fp); return (error); } int kern_truncate(struct nlookupdata *nd, off_t length) { struct vnode *vp; struct vattr vattr; int error; uid_t uid = 0; gid_t gid = 0; uint64_t old_size = 0; if (length < 0) return(EINVAL); nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE; if ((error = nlookup(nd)) != 0) return (error); if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) return (error); error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); if (error) { vrele(vp); return (error); } if (vp->v_type == VDIR) { error = EISDIR; goto done; } if (vfs_quota_enabled) { error = VOP_GETATTR(vp, &vattr); KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0")); uid = vattr.va_uid; gid = vattr.va_gid; old_size = vattr.va_size; } if ((error = vn_writechk(vp)) == 0) { VATTR_NULL(&vattr); vattr.va_size = length; error = VOP_SETATTR(vp, &vattr, nd->nl_cred); VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size); } done: vput(vp); return (error); } /* * truncate(char *path, int pad, off_t length) * * Truncate a file given its path name. */ int sys_truncate(struct sysmsg *sysmsg, const struct truncate_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = kern_truncate(&nd, uap->length); nlookup_done(&nd); return error; } int kern_ftruncate(int fd, off_t length) { struct thread *td = curthread; struct vattr vattr; struct vnode *vp; struct file *fp; int error; uid_t uid = 0; gid_t gid = 0; uint64_t old_size = 0; struct mount *mp; if (length < 0) return(EINVAL); if ((error = holdvnode(td, fd, &fp)) != 0) return (error); if (fp->f_nchandle.ncp) { error = ncp_writechk(&fp->f_nchandle); if (error) goto done; } if ((fp->f_flag & FWRITE) == 0) { error = EINVAL; goto done; } if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */ error = EINVAL; goto done; } vp = (struct vnode *)fp->f_data; vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); if (vp->v_type == VDIR) { error = EISDIR; vn_unlock(vp); goto done; } if (vfs_quota_enabled) { error = VOP_GETATTR_FP(vp, &vattr, fp); KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0")); uid = vattr.va_uid; gid = vattr.va_gid; old_size = vattr.va_size; } if ((error = vn_writechk(vp)) == 0) { VATTR_NULL(&vattr); vattr.va_size = length; error = VOP_SETATTR_FP(vp, &vattr, fp->f_cred, fp); mp = vq_vptomp(vp); VFS_ACCOUNT(mp, uid, gid, length - old_size); } vn_unlock(vp); done: fdrop(fp); return (error); } /* * ftruncate_args(int fd, int pad, off_t length) * * Truncate a file given a file descriptor. */ int sys_ftruncate(struct sysmsg *sysmsg, const struct ftruncate_args *uap) { int error; error = kern_ftruncate(uap->fd, uap->length); return (error); } int kern_fsync(int fd, bool fullsync) { struct thread *td = curthread; struct vnode *vp; struct file *fp; vm_object_t obj; int error; if ((error = holdvnode(td, fd, &fp)) != 0) return (error); vp = (struct vnode *)fp->f_data; vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); if ((obj = vp->v_object) != NULL) { if (vp->v_mount == NULL || (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) { vm_object_page_clean(obj, 0, 0, 0); } } error = fullsync ? VOP_FSYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp) : VOP_FDATASYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp); if (error == 0 && vp->v_mount) error = buf_fsync(vp); vn_unlock(vp); fdrop(fp); return (error); } /* * fsync(int fd) * * Sync an open file. */ int sys_fsync(struct sysmsg *sysmsg, const struct fsync_args *uap) { return (kern_fsync(uap->fd, true)); } /* * fdatasync(int fd) * * Data-sync an open file. */ int sys_fdatasync(struct sysmsg *sysmsg, const struct fdatasync_args *uap) { return (kern_fsync(uap->fd, false)); } /* * rename op. * * NOTE: error == 0 and nl_dvp is NULL indicates a mount point, operation * disallowed. e.g. /var/cache where /var/cache is a null-mount, for * example. */ int kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond) { struct nchandle fnchd; struct nchandle tnchd; struct namecache *ncp; struct vnode *fdvp; struct vnode *tdvp; struct mount *mp; struct mount *userenlk; int error; u_int fncp_gen; u_int tncp_gen; bwillinode(1); fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC; if ((error = nlookup(fromnd)) != 0) return (error); /* * Attempt to rename a mount point (from or to) */ if (error == 0 && fromnd->nl_dvp == NULL) return (EINVAL); if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL) return (ENOENT); fnchd.mount = fromnd->nl_nch.mount; cache_hold(&fnchd); /* * unlock the source nch so we can lookup the target nch without * deadlocking. The target may or may not exist so we do not check * for a target vp like kern_mkdir() and other creation functions do. * * The source and target directories are ref'd and rechecked after * everything is relocked to determine if the source or target file * has been renamed. */ KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED); fromnd->nl_flags &= ~NLC_NCPISLOCKED; fncp_gen = fromnd->nl_nch.ncp->nc_generation; if (fromnd->nl_nch.ncp->nc_vp && fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) { userenlk = fnchd.mount; cache_unlock(&fromnd->nl_nch); lockmgr(&userenlk->mnt_renlock, LK_EXCLUSIVE); } else { userenlk = NULL; cache_unlock(&fromnd->nl_nch); } /* * Lookup target */ tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP; if ((error = nlookup(tond)) != 0) { cache_drop(&fnchd); goto done; } tncp_gen = tond->nl_nch.ncp->nc_generation; /* * Attempt to rename a mount point (from or to) */ if (error == 0 && tond->nl_dvp == NULL) { cache_drop(&fnchd); error = ENOENT; goto done; } if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) { cache_drop(&fnchd); error = ENOENT; goto done; } tnchd.mount = tond->nl_nch.mount; cache_hold(&tnchd); /* * If the source and target are the same there is nothing to do */ if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) { cache_drop(&fnchd); cache_drop(&tnchd); error = 0; goto done; } /* * Mount points cannot be renamed or overwritten */ if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) & NCF_ISMOUNTPT ) { cache_drop(&fnchd); cache_drop(&tnchd); error = EINVAL; goto done; } /* * Lock all four namecache entries. tond is already locked. */ cache_lock4_tondlocked(&fnchd, &fromnd->nl_nch, &tnchd, &tond->nl_nch, fromnd->nl_cred, tond->nl_cred); fromnd->nl_flags |= NLC_NCPISLOCKED; /* * If the namecache generation changed for either fromnd or tond, * we must retry. */ if (((fromnd->nl_nch.ncp->nc_generation - fncp_gen) & ~1) || ((tond->nl_nch.ncp->nc_generation - tncp_gen) & ~1)) { krateprintf(&krate_rename, "kern_rename: retry due to race on: " "\"%s\" -> \"%s\" (%d,%d)\n", fromnd->nl_nch.ncp->nc_name, tond->nl_nch.ncp->nc_name, fromnd->nl_nch.ncp->nc_generation - fncp_gen, tond->nl_nch.ncp->nc_generation - tncp_gen); error = EAGAIN; goto finish; } /* * If either fromnd or tond are marked destroyed a ripout occured * out from under us and we must retry. */ if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) || fromnd->nl_nch.ncp->nc_vp == NULL || (tond->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED))) { krateprintf(&krate_rename, "kern_rename: retry due to ripout on: " "\"%s\" -> \"%s\"\n", fromnd->nl_nch.ncp->nc_name, tond->nl_nch.ncp->nc_name); error = EAGAIN; goto finish; } /* * Make sure the parent directories linkages are the same. We have * already checked that fromnd and tond are not mount points so this * should not loop forever on a cross-mount. */ if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent || tnchd.ncp != tond->nl_nch.ncp->nc_parent) { error = EAGAIN; goto finish; } /* * Both the source and target must be within the same filesystem and * in the same filesystem as their parent directories within the * namecache topology. * * NOTE: fromnd's nc_mount or nc_vp could be NULL. */ mp = fnchd.mount; if (mp != tnchd.mount || mp != fromnd->nl_nch.mount || mp != tond->nl_nch.mount) { error = EXDEV; goto finish; } /* * Make sure the mount point is writable */ if ((error = ncp_writechk(&tond->nl_nch)) != 0) { goto finish; } /* * If the target exists and either the source or target is a directory, * then both must be directories. * * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h * have become NULL. */ if (tond->nl_nch.ncp->nc_vp) { if (fromnd->nl_nch.ncp->nc_vp == NULL) { error = ENOENT; } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) { if (tond->nl_nch.ncp->nc_vp->v_type != VDIR) error = ENOTDIR; } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) { error = EISDIR; } } /* * You cannot rename a source into itself or a subdirectory of itself. * We check this by travsersing the target directory upwards looking * for a match against the source. * * Only required when renaming a directory, in which case userenlk is * non-NULL. */ if (__predict_false(userenlk && error == 0)) { for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) { if (fromnd->nl_nch.ncp == ncp) { error = EINVAL; break; } } } /* * Even though the namespaces are different, they may still represent * hardlinks to the same file. The filesystem might have a hard time * with this so we issue a NREMOVE of the source instead of a NRENAME * when we detect the situation. */ if (error == 0) { fdvp = fromnd->nl_dvp; tdvp = tond->nl_dvp; if (fdvp == NULL || tdvp == NULL) { error = EPERM; } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) { error = VOP_NREMOVE(&fromnd->nl_nch, fdvp, fromnd->nl_cred); } else { error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch, fdvp, tdvp, tond->nl_cred); } } finish: cache_put(&tnchd); cache_put(&fnchd); done: if (userenlk) lockmgr(&userenlk->mnt_renlock, LK_RELEASE); return (error); } /* * rename_args(char *from, char *to) * * Rename files. Source and destination must either both be directories, * or both not be directories. If target is a directory, it must be empty. */ int sys_rename(struct sysmsg *sysmsg, const struct rename_args *uap) { struct nlookupdata fromnd, tond; int error; do { error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0); if (error == 0) { error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0); if (error == 0) error = kern_rename(&fromnd, &tond); nlookup_done(&tond); } nlookup_done(&fromnd); } while (error == EAGAIN); return (error); } /* * renameat_args(int oldfd, char *old, int newfd, char *new) * * Rename files using paths relative to the directories associated with * oldfd and newfd. Source and destination must either both be directories, * or both not be directories. If target is a directory, it must be empty. */ int sys_renameat(struct sysmsg *sysmsg, const struct renameat_args *uap) { struct nlookupdata oldnd, newnd; struct file *oldfp, *newfp; int error; do { error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old, UIO_USERSPACE, 0); if (error == 0) { error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new, UIO_USERSPACE, 0); if (error == 0) error = kern_rename(&oldnd, &newnd); nlookup_done_at(&newnd, newfp); } nlookup_done_at(&oldnd, oldfp); } while (error == EAGAIN); return (error); } int kern_mkdir(struct nlookupdata *nd, int mode) { struct thread *td = curthread; struct proc *p = td->td_proc; struct vnode *vp; struct vattr vattr; int error; bwillinode(1); nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP; if ((error = nlookup(nd)) != 0) return (error); if (nd->nl_nch.ncp->nc_vp) return (EEXIST); if (nd->nl_dvp == NULL) return (EINVAL); if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); VATTR_NULL(&vattr); vattr.va_type = VDIR; vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask; vp = NULL; error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr); if (error == 0) vput(vp); return (error); } /* * mkdir_args(char *path, int mode) * * Make a directory file. */ int sys_mkdir(struct sysmsg *sysmsg, const struct mkdir_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_mkdir(&nd, uap->mode); nlookup_done(&nd); return (error); } /* * mkdirat_args(int fd, char *path, mode_t mode) * * Make a directory file. The path is relative to the directory associated * with fd. */ int sys_mkdirat(struct sysmsg *sysmsg, const struct mkdirat_args *uap) { struct nlookupdata nd; struct file *fp; int error; error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_mkdir(&nd, uap->mode); nlookup_done_at(&nd, fp); return (error); } int kern_rmdir(struct nlookupdata *nd) { int error; bwillinode(1); nd->nl_flags |= NLC_DELETE | NLC_REFDVP; if ((error = nlookup(nd)) != 0) return (error); /* * Do not allow directories representing mount points to be * deleted, even if empty. Check write perms on mount point * in case the vnode is aliased (aka nullfs). */ if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT)) return (EBUSY); if (nd->nl_dvp == NULL) return (EINVAL); if ((error = ncp_writechk(&nd->nl_nch)) != 0) return (error); error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred); return (error); } /* * rmdir_args(char *path) * * Remove a directory file. */ int sys_rmdir(struct sysmsg *sysmsg, const struct rmdir_args *uap) { struct nlookupdata nd; int error; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); if (error == 0) error = kern_rmdir(&nd); nlookup_done(&nd); return (error); } int kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res, enum uio_seg direction) { struct thread *td = curthread; struct vnode *vp; struct file *fp; struct uio auio; struct iovec aiov; off_t loff; int error, eofflag; if ((error = holdvnode(td, fd, &fp)) != 0) return (error); if ((fp->f_flag & FREAD) == 0) { error = EBADF; goto done; } vp = (struct vnode *)fp->f_data; if (vp->v_type != VDIR) { error = EINVAL; goto done; } aiov.iov_base = buf; aiov.iov_len = count; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_rw = UIO_READ; auio.uio_segflg = direction; auio.uio_td = td; auio.uio_resid = count; loff = auio.uio_offset = fp->f_offset; error = VOP_READDIR_FP(vp, &auio, fp->f_cred, &eofflag, NULL, NULL, fp); fp->f_offset = auio.uio_offset; if (error) goto done; /* * WARNING! *basep may not be wide enough to accomodate the * seek offset. XXX should we hack this to return the upper 32 bits * for offsets greater then 4G? */ if (basep) { *basep = (long)loff; } *res = count - auio.uio_resid; done: fdrop(fp); return (error); } /* * getdirentries_args(int fd, char *buf, u_int conut, long *basep) * * Read a block of directory entries in a file system independent format. */ int sys_getdirentries(struct sysmsg *sysmsg, const struct getdirentries_args *uap) { long base; int error; error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base, &sysmsg->sysmsg_result, UIO_USERSPACE); if (error == 0 && uap->basep) error = copyout(&base, uap->basep, sizeof(*uap->basep)); return (error); } /* * getdents_args(int fd, char *buf, size_t count) */ int sys_getdents(struct sysmsg *sysmsg, const struct getdents_args *uap) { int error; error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL, &sysmsg->sysmsg_result, UIO_USERSPACE); return (error); } /* * Set the mode mask for creation of filesystem nodes. * * umask(int newmask) */ int sys_umask(struct sysmsg *sysmsg, const struct umask_args *uap) { struct thread *td = curthread; struct proc *p = td->td_proc; struct filedesc *fdp; fdp = p->p_fd; sysmsg->sysmsg_result = fdp->fd_cmask; fdp->fd_cmask = uap->newmask & ALLPERMS; return (0); } /* * revoke(char *path) * * Void all references to file by ripping underlying filesystem * away from vnode. */ int sys_revoke(struct sysmsg *sysmsg, const struct revoke_args *uap) { struct nlookupdata nd; struct vattr vattr; struct vnode *vp; struct ucred *cred; int error; vp = NULL; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); cred = crhold(nd.nl_cred); nlookup_done(&nd); if (error == 0) { if (error == 0) error = VOP_GETATTR(vp, &vattr); if (error == 0 && cred->cr_uid != vattr.va_uid) error = caps_priv_check(cred, SYSCAP_NOVFS_REVOKE); if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) { if (vcount(vp) > 0) error = vrevoke(vp, cred); } else if (error == 0) { error = vrevoke(vp, cred); } vrele(vp); } if (cred) crfree(cred); return (error); } /* * getfh_args(char *fname, fhandle_t *fhp) * * Get (NFS) file handle * * NOTE: We use the fsid of the covering mount, even if it is a nullfs * mount. This allows nullfs mounts to be explicitly exported. * * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe. * * nullfs mounts of subdirectories are not safe. That is, it will * work, but you do not really have protection against access to * the related parent directories. */ int sys_getfh(struct sysmsg *sysmsg, const struct getfh_args *uap) { struct nlookupdata nd; fhandle_t fh; struct vnode *vp; struct mount *mp; int error; /* * Must be super user */ if ((error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT)) != 0) return (error); vp = NULL; error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); mp = nd.nl_nch.mount; nlookup_done(&nd); if (error == 0) { bzero(&fh, sizeof(fh)); fh.fh_fsid = mp->mnt_stat.f_fsid; error = VFS_VPTOFH(vp, &fh.fh_fid); vput(vp); if (error == 0) error = copyout(&fh, uap->fhp, sizeof(fh)); } return (error); } /* * fhopen_args(const struct fhandle *u_fhp, int flags) * * syscall for the rpc.lockd to use to translate a NFS file handle into * an open descriptor. * * WARNING: Do not remove the caps_priv_check() call or this becomes * one giant security hole. */ int sys_fhopen(struct sysmsg *sysmsg, const struct fhopen_args *uap) { struct thread *td = curthread; struct filedesc *fdp = td->td_proc->p_fd; struct mount *mp; struct vnode *vp; struct fhandle fhp; struct vattr vat; struct vattr *vap = &vat; struct flock lf; int fmode, mode, error = 0, type; struct file *nfp; struct file *fp; int indx; /* * Must be super user */ error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT); if (error) return (error); fmode = FFLAGS(uap->flags); /* * Why not allow a non-read/write open for our lockd? */ if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT)) return (EINVAL); error = copyin(uap->u_fhp, &fhp, sizeof(fhp)); if (error) return(error); /* * Find the mount point */ mp = vfs_getvfs(&fhp.fh_fsid); if (mp == NULL) { error = ESTALE; goto done2; } /* now give me my vnode, it gets returned to me locked */ error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp); if (error) goto done; /* * from now on we have to make sure not * to forget about the vnode * any error that causes an abort must vput(vp) * just set error = err and 'goto bad;'. */ /* * from vn_open */ if (vp->v_type == VLNK) { error = EMLINK; goto bad; } if (vp->v_type == VSOCK) { error = EOPNOTSUPP; goto bad; } mode = 0; if (fmode & (FWRITE | O_TRUNC)) { if (vp->v_type == VDIR) { error = EISDIR; goto bad; } error = vn_writechk(vp); if (error) goto bad; mode |= VWRITE; } if (fmode & FREAD) mode |= VREAD; if (mode) { error = VOP_ACCESS(vp, mode, td->td_ucred); if (error) goto bad; } if (fmode & O_TRUNC) { vn_unlock(vp); /* XXX */ vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */ VATTR_NULL(vap); vap->va_size = 0; error = VOP_SETATTR(vp, vap, td->td_ucred); if (error) goto bad; } /* * VOP_OPEN needs the file pointer so it can potentially override * it. * * WARNING! no f_nchandle will be associated when fhopen()ing a * directory. XXX */ if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0) goto bad; error = VOP_OPEN(vp, fmode, td->td_ucred, &nfp); fp = nfp; if (error) { /* * setting f_ops this way prevents VOP_CLOSE from being * called or fdrop() releasing the vp from v_data. Since * the VOP_OPEN failed we don't want to VOP_CLOSE. */ fp->f_ops = &badfileops; fp->f_data = NULL; goto bad_drop; } /* * The fp is given its own reference, we still have our ref and lock. * * Assert that all regular files must be created with a VM object. */ if (vp->v_type == VREG && vp->v_object == NULL) { kprintf("fhopen: regular file did not " "have VM object: %p\n", vp); goto bad_drop; } /* * The open was successful. Handle any locking requirements. */ if (fmode & (O_EXLOCK | O_SHLOCK)) { lf.l_whence = SEEK_SET; lf.l_start = 0; lf.l_len = 0; if (fmode & O_EXLOCK) lf.l_type = F_WRLCK; else lf.l_type = F_RDLCK; if (fmode & FNONBLOCK) type = 0; else type = F_WAIT; vn_unlock(vp); if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) { /* * release our private reference. */ fsetfd(fdp, NULL, indx); fdrop(fp); vrele(vp); goto done; } vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */ } /* * Clean up. Associate the file pointer with the previously * reserved descriptor and return it. */ vput(vp); if (uap->flags & O_CLOEXEC) fdp->fd_files[indx].fileflags |= UF_EXCLOSE; if (uap->flags & O_CLOFORK) fdp->fd_files[indx].fileflags |= UF_FOCLOSE; fsetfd(fdp, fp, indx); fdrop(fp); sysmsg->sysmsg_result = indx; mount_drop(mp); return (error); bad_drop: fsetfd(fdp, NULL, indx); fdrop(fp); bad: vput(vp); done: mount_drop(mp); done2: return (error); } /* * fhstat_args(struct fhandle *u_fhp, struct stat *sb) */ int sys_fhstat(struct sysmsg *sysmsg, const struct fhstat_args *uap) { struct thread *td = curthread; struct stat sb; fhandle_t fh; struct mount *mp; struct vnode *vp; int error; /* * Must be super user */ error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT); if (error) return (error); error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); if (error) return (error); if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) error = ESTALE; if (error == 0) { if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) { error = vn_stat(vp, &sb, td->td_ucred); vput(vp); } } if (error == 0) error = copyout(&sb, uap->sb, sizeof(sb)); if (mp) mount_drop(mp); return (error); } /* * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf) */ int sys_fhstatfs(struct sysmsg *sysmsg, const struct fhstatfs_args *uap) { struct thread *td = curthread; struct proc *p = td->td_proc; struct statfs *sp; struct mount *mp; struct vnode *vp; struct statfs sb; char *fullpath, *freepath; fhandle_t fh; int error; /* * Must be super user */ error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT); if (error) return (error); if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) return (error); if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) { error = ESTALE; goto done; } if (p != NULL && !chroot_visible_mnt(mp, p)) { error = ESTALE; goto done; } if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0) goto done; mp = vp->v_mount; sp = &mp->mnt_stat; vput(vp); if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0) goto done; error = mount_path(p, mp, &fullpath, &freepath); if (error) goto done; bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); kfree(freepath, M_TEMP); sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; if (caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)) { bcopy(sp, &sb, sizeof(sb)); sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0; sp = &sb; } error = copyout(sp, uap->buf, sizeof(*sp)); done: if (mp) mount_drop(mp); return (error); } /* * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf) */ int sys_fhstatvfs(struct sysmsg *sysmsg, const struct fhstatvfs_args *uap) { struct thread *td = curthread; struct proc *p = td->td_proc; struct statvfs *sp; struct mount *mp; struct vnode *vp; fhandle_t fh; int error; /* * Must be super user */ if ((error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT))) return (error); if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0) return (error); if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) { error = ESTALE; goto done; } if (p != NULL && !chroot_visible_mnt(mp, p)) { error = ESTALE; goto done; } if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp))) goto done; mp = vp->v_mount; sp = &mp->mnt_vstat; vput(vp); if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0) goto done; sp->f_flag = 0; if (mp->mnt_flag & MNT_RDONLY) sp->f_flag |= ST_RDONLY; if (mp->mnt_flag & MNT_NOSUID) sp->f_flag |= ST_NOSUID; error = copyout(sp, uap->buf, sizeof(*sp)); done: if (mp) mount_drop(mp); return (error); } /* * Syscall to push extended attribute configuration information into the * VFS. Accepts a path, which it converts to a mountpoint, as well as * a command (int cmd), and attribute name and misc data. For now, the * attribute name is left in userspace for consumption by the VFS_op. * It will probably be changed to be copied into sysspace by the * syscall in the future, once issues with various consumers of the * attribute code have raised their hands. * * Currently this is used only by UFS Extended Attributes. */ int sys_extattrctl(struct sysmsg *sysmsg, const struct extattrctl_args *uap) { struct nlookupdata nd; struct vnode *vp; char attrname[EXTATTR_MAXNAMELEN]; int error; size_t size; attrname[0] = 0; vp = NULL; error = 0; if (error == 0 && uap->filename) { error = nlookup_init(&nd, uap->filename, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); nlookup_done(&nd); } if (error == 0 && uap->attrname) { error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, &size); } if (error == 0) { error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = ncp_writechk(&nd.nl_nch); if (error == 0) { error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp, uap->attrnamespace, uap->attrname, nd.nl_cred); } nlookup_done(&nd); } return (error); } /* * Syscall to get a named extended attribute on a file or directory. */ int sys_extattr_set_file(struct sysmsg *sysmsg, const struct extattr_set_file_args *uap) { char attrname[EXTATTR_MAXNAMELEN]; struct nlookupdata nd; struct vnode *vp; struct uio auio; struct iovec aiov; int error; error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN); if (error) return (error); vp = NULL; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = ncp_writechk(&nd.nl_nch); if (error == 0) error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); if (error) { nlookup_done(&nd); return (error); } bzero(&auio, sizeof(auio)); aiov.iov_base = uap->data; aiov.iov_len = uap->nbytes; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = 0; auio.uio_resid = uap->nbytes; auio.uio_rw = UIO_WRITE; auio.uio_td = curthread; error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname, &auio, nd.nl_cred); vput(vp); nlookup_done(&nd); return (error); } /* * Syscall to get a named extended attribute on a file or directory. */ int sys_extattr_get_file(struct sysmsg *sysmsg, const struct extattr_get_file_args *uap) { char attrname[EXTATTR_MAXNAMELEN]; struct nlookupdata nd; struct uio auio; struct iovec aiov; struct vnode *vp; int error; error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN); if (error) return (error); vp = NULL; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp); if (error) { nlookup_done(&nd); return (error); } bzero(&auio, sizeof(auio)); aiov.iov_base = uap->data; aiov.iov_len = uap->nbytes; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = 0; auio.uio_resid = uap->nbytes; auio.uio_rw = UIO_READ; auio.uio_td = curthread; error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname, &auio, nd.nl_cred); sysmsg->sysmsg_result = uap->nbytes - auio.uio_resid; vput(vp); nlookup_done(&nd); return(error); } /* * Syscall to delete a named extended attribute from a file or directory. * Accepts attribute name. The real work happens in VOP_SETEXTATTR(). */ int sys_extattr_delete_file(struct sysmsg *sysmsg, const struct extattr_delete_file_args *uap) { char attrname[EXTATTR_MAXNAMELEN]; struct nlookupdata nd; struct vnode *vp; int error; error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN); if (error) return(error); error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error == 0) error = nlookup(&nd); if (error == 0) error = ncp_writechk(&nd.nl_nch); if (error == 0) { error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); if (error == 0) { error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname, NULL, nd.nl_cred); vput(vp); } } nlookup_done(&nd); return(error); } /* * Determine if the mount is visible to the process. */ static int chroot_visible_mnt(struct mount *mp, struct proc *p) { struct nchandle nch; /* * Traverse from the mount point upwards. If we hit the process * root then the mount point is visible to the process. */ nch = mp->mnt_ncmountpt; while (nch.ncp) { if (nch.mount == p->p_fd->fd_nrdir.mount && nch.ncp == p->p_fd->fd_nrdir.ncp) { return(1); } if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) { nch = nch.mount->mnt_ncmounton; } else { nch.ncp = nch.ncp->nc_parent; } } /* * If the mount point is not visible to the process, but the * process root is in a subdirectory of the mount, return * TRUE anyway. */ if (p->p_fd->fd_nrdir.mount == mp) return(1); return(0); } /* * Return the appropriate system capability restriction. */ static int get_fscap(const char *fsname) { if (strncmp("null", fsname, 5) == 0) { return SYSCAP_NOMOUNT_NULLFS; } else if (strncmp(fsname, "devfs", 6) == 0) { return SYSCAP_NOMOUNT_DEVFS; } else if (strncmp(fsname, "procfs", 7) == 0) { return SYSCAP_NOMOUNT_PROCFS; } else if (strncmp(fsname, "tmpfs", 6) == 0) { return SYSCAP_NOMOUNT_TMPFS; } else if (strncmp(fsname, "fusefs", 7) == 0) { return SYSCAP_NOMOUNT_FUSE; } return SYSCAP_RESTRICTEDROOT; } int sys___realpath(struct sysmsg *sysmsg, const struct __realpath_args *uap) { struct nlookupdata nd; char *rbuf; char *fbuf; ssize_t rlen; int error; /* * Invalid length if less than 0. 0 is allowed */ if ((ssize_t)uap->len < 0) return EINVAL; rbuf = NULL; fbuf = NULL; error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); if (error) goto done; nd.nl_flags |= NLC_SHAREDLOCK; error = nlookup(&nd); if (error) goto done; if (nd.nl_nch.ncp->nc_vp == NULL) { error = ENOENT; goto done; } /* * Shortcut test for existence. */ if (uap->len == 0) { error = ENAMETOOLONG; goto done; } /* * Obtain the path relative to the process root. The nch must not * be locked for the cache_fullpath() call. */ if (nd.nl_flags & NLC_NCPISLOCKED) { nd.nl_flags &= ~NLC_NCPISLOCKED; cache_unlock(&nd.nl_nch); } error = cache_fullpath(curproc, &nd.nl_nch, NULL, &rbuf, &fbuf, 0); if (error) goto done; rlen = (ssize_t)strlen(rbuf); if (rlen >= uap->len) { error = ENAMETOOLONG; goto done; } error = copyout(rbuf, uap->buf, rlen + 1); if (error == 0) sysmsg->sysmsg_szresult = rlen; done: nlookup_done(&nd); if (fbuf) kfree(fbuf, M_TEMP); return error; } int sys_posix_fallocate(struct sysmsg *sysmsg, const struct posix_fallocate_args *uap) { return (kern_posix_fallocate(uap->fd, uap->offset, uap->len)); } int kern_posix_fallocate(int fd, off_t offset, off_t len) { struct thread *td = curthread; struct vnode *vp; struct file *fp; int error; if (offset < 0 || len <= 0) return (EINVAL); /* Check for wrap. */ if (offset > OFF_MAX - len) return (EFBIG); fp = holdfp(td, fd, -1); if (fp == NULL) return (EBADF); switch (fp->f_type) { case DTYPE_VNODE: break; case DTYPE_PIPE: case DTYPE_FIFO: error = ESPIPE; goto out; default: error = ENODEV; goto out; } if ((fp->f_flag & FWRITE) == 0) { error = EBADF; goto out; } vp = fp->f_data; if (vp->v_type != VREG) { error = ENODEV; goto out; } vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); error = VOP_ALLOCATE(vp, offset, len); vn_unlock(vp); out: dropfp(td, fd, fp); return (error); } |