sys/kern/vfs_subr.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 | /* * 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_subr.c 8.31 (Berkeley) 5/26/95 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $ */ /* * External virtual filesystem routines */ #include "opt_ddb.h" #include "opt_inet.h" #include "opt_inet6.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/uio.h> #include <sys/buf.h> #include <sys/conf.h> #include <sys/dirent.h> #include <sys/endian.h> #include <sys/eventhandler.h> #include <sys/fcntl.h> #include <sys/file.h> #include <sys/kernel.h> #include <sys/kthread.h> #include <sys/malloc.h> #include <sys/mbuf.h> #include <sys/mount.h> #include <sys/caps.h> #include <sys/proc.h> #include <sys/reboot.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/sysctl.h> #include <sys/syslog.h> #include <sys/unistd.h> #include <sys/vmmeter.h> #include <sys/vnode.h> #include <machine/limits.h> #include <vm/vm.h> #include <vm/vm_object.h> #include <vm/vm_extern.h> #include <vm/vm_kern.h> #include <vm/pmap.h> #include <vm/vm_map.h> #include <vm/vm_page.h> #include <vm/vm_pager.h> #include <vm/vnode_pager.h> #include <vm/vm_zone.h> #include <sys/buf2.h> #include <vm/vm_page2.h> #include <netinet/in.h> static MALLOC_DEFINE(M_NETCRED, "Export Host", "Export host address structure"); __read_mostly int numvnodes; SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "Number of vnodes allocated"); __read_mostly int verbose_reclaims; SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0, "Output filename of reclaimed vnode(s)"); __read_mostly enum vtype iftovt_tab[16] = { VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, }; __read_mostly int vttoif_tab[9] = { 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFSOCK, S_IFIFO, S_IFMT, }; static int reassignbufcalls; SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "Number of times buffers have been reassigned to the proper list"); __read_mostly static int check_buf_overlap = 2; /* invasive check */ SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap, 0, "Enable overlapping buffer checks"); int nfs_mount_type = -1; static struct lwkt_token spechash_token; struct nfs_public nfs_pub; /* publicly exported FS */ __read_mostly int maxvnodes; SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, &maxvnodes, 0, "Maximum number of vnodes"); static struct radix_node_head *vfs_create_addrlist_af(int af, struct netexport *nep); static void vclean_vxlocked(struct vnode *vp, int flags); __read_mostly int prtactive = 0; /* 1 => print out reclaim of active vnodes */ /* * Red black tree functions */ static int rb_buf_compare(struct buf *b1, struct buf *b2); RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset); RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset); static int rb_buf_compare(struct buf *b1, struct buf *b2) { if (b1->b_loffset < b2->b_loffset) return(-1); if (b1->b_loffset > b2->b_loffset) return(1); return(0); } /* * Initialize the vnode management data structures. * * Called from vfsinit() */ #define VNBREAKMEM1 (1L * 1024 * 1024 * 1024) #define VNBREAKMEM2 (7L * 1024 * 1024 * 1024) #define MINVNODES 2000 #define MAXVNODES 4000000 void vfs_subr_init(void) { int factor1; /* Limit based on ram (x 2 above 1GB) */ size_t freemem; /* * Size maxvnodes non-linearly to available memory. Don't bloat * the count on low-memory systems. Scale up for systems with * more than 1G and more than 8G of ram, but do so non-linearly * because the value of a large maxvnodes count diminishes * significantly beyond a certain point. * * The general minimum is maxproc * 8 (we want someone pushing * up maxproc a lot to also get more vnodes). Usually maxproc * does not affect this calculation. The KvaSize limitation also * typically does not affect this calculation (it is just in case * the kernel VM space is made much smaller than main memory, which * should no longer happen on 64-bit systems). * * There isn't much of a point allowing maxvnodes to exceed a * few million as modern filesystems cache pages in the * underlying block device and not so much hanging off of VM * objects. * * Also, VM objects, vnodes, and filesystem inode and other related * structures have gotten a lot larger in recent years and the kernel * memory use tends to scale with maxvnodes, so we don't want to bloat * it too much. But neither do we want the max set too low because * systems with large amounts of memory and cores are capable of * doing a hell of a lot. */ factor1 = 80 * (sizeof(struct vm_object) + sizeof(struct vnode)); freemem = (int64_t)vmstats.v_page_count * PAGE_SIZE; maxvnodes = freemem / factor1; if (freemem > VNBREAKMEM1) { freemem -= VNBREAKMEM1; if (freemem < VNBREAKMEM2) { maxvnodes += freemem / factor1 / 2; } else { maxvnodes += VNBREAKMEM2 / factor1 / 2; freemem -= VNBREAKMEM2; maxvnodes += freemem / factor1 / 4; } } maxvnodes = imax(maxvnodes, maxproc * 8); maxvnodes = imin(maxvnodes, KvaSize / factor1); maxvnodes = imin(maxvnodes, MAXVNODES); maxvnodes = imax(maxvnodes, MINVNODES); lwkt_token_init(&spechash_token, "spechash"); } /* * Knob to control the precision of file timestamps: * * 0 = seconds only; nanoseconds zeroed. * 1 = microseconds accurate to tick precision * 2 = microseconds accurate to tick precision (default, hz >= 100) * 3 = nanoseconds accurate to tick precision * 4 = microseconds, maximum precision (default, hz < 100) * 5 = nanoseconds, maximum precision * * Note that utimes() precision is microseconds because it takes a timeval * structure, so its probably best to default to USEC or USEC_PRECISE, and * not NSEC. */ enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC, TSP_USEC_PRECISE, TSP_NSEC_PRECISE }; __read_mostly static int timestamp_precision = -1; SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, ×tamp_precision, 0, "Precision of file timestamps"); /* * Get a current timestamp. * * MPSAFE */ void vfs_timestamp(struct timespec *tsp) { switch (timestamp_precision) { case TSP_SEC: /* seconds precision */ getnanotime(tsp); tsp->tv_nsec = 0; break; case TSP_HZ: /* ticks precision (limit to microseconds) */ getnanotime(tsp); tsp->tv_nsec -= tsp->tv_nsec % 1000; break; default: case TSP_USEC: /* microseconds (ticks precision) */ getnanotime(tsp); tsp->tv_nsec -= tsp->tv_nsec % 1000; break; case TSP_NSEC: /* nanoseconds (ticks precision) */ getnanotime(tsp); break; case TSP_USEC_PRECISE: /* microseconds (high preceision) */ nanotime(tsp); tsp->tv_nsec -= tsp->tv_nsec % 1000; break; case TSP_NSEC_PRECISE: /* nanoseconds (high precision) */ nanotime(tsp); break; } } /* * Set vnode attributes to VNOVAL */ void vattr_null(struct vattr *vap) { vap->va_type = VNON; vap->va_size = VNOVAL; vap->va_bytes = VNOVAL; vap->va_mode = VNOVAL; vap->va_nlink = VNOVAL; vap->va_uid = VNOVAL; vap->va_gid = VNOVAL; vap->va_fsid = VNOVAL; vap->va_fileid = VNOVAL; vap->va_blocksize = VNOVAL; vap->va_rmajor = VNOVAL; vap->va_rminor = VNOVAL; vap->va_atime.tv_sec = VNOVAL; vap->va_atime.tv_nsec = VNOVAL; vap->va_mtime.tv_sec = VNOVAL; vap->va_mtime.tv_nsec = VNOVAL; vap->va_ctime.tv_sec = VNOVAL; vap->va_ctime.tv_nsec = VNOVAL; vap->va_flags = VNOVAL; vap->va_gen = VNOVAL; vap->va_vaflags = 0; /* va_*_uuid fields are only valid if related flags are set */ } /* * Flush out and invalidate all buffers associated with a vnode. * * vp must be locked. */ static int vinvalbuf_bp(struct buf *bp, void *data); struct vinvalbuf_bp_info { struct vnode *vp; int slptimeo; int lkflags; int flags; int clean; }; int vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) { struct vinvalbuf_bp_info info; vm_object_t object; int error; lwkt_gettoken(&vp->v_token); /* * If we are being asked to save, call fsync to ensure that the inode * is updated. */ if (flags & V_SAVE) { error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo); if (error) goto done; if (!RB_EMPTY(&vp->v_rbdirty_tree)) { if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0) goto done; #if 0 /* * Dirty bufs may be left or generated via races * in circumstances where vinvalbuf() is called on * a vnode not undergoing reclamation. Only * panic if we are trying to reclaim the vnode. */ if ((vp->v_flag & VRECLAIMED) && (bio_track_active(&vp->v_track_write) || !RB_EMPTY(&vp->v_rbdirty_tree))) { panic("vinvalbuf: dirty bufs"); } #endif } } info.slptimeo = slptimeo; info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL; if (slpflag & PCATCH) info.lkflags |= LK_PCATCH; info.flags = flags; info.vp = vp; /* * Flush the buffer cache until nothing is left, wait for all I/O * to complete. At least one pass is required. We might block * in the pip code so we have to re-check. Order is important. */ do { /* * Flush buffer cache */ if (!RB_EMPTY(&vp->v_rbclean_tree)) { info.clean = 1; error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL, vinvalbuf_bp, &info); } if (!RB_EMPTY(&vp->v_rbdirty_tree)) { info.clean = 0; error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, vinvalbuf_bp, &info); } /* * Wait for I/O completion. */ bio_track_wait(&vp->v_track_write, 0, 0); if ((object = vp->v_object) != NULL) refcount_wait(&object->paging_in_progress, "vnvlbx"); } while (bio_track_active(&vp->v_track_write) || !RB_EMPTY(&vp->v_rbclean_tree) || !RB_EMPTY(&vp->v_rbdirty_tree)); /* * Destroy the copy in the VM cache, too. */ if ((object = vp->v_object) != NULL) { vm_object_page_remove(object, 0, 0, (flags & V_SAVE) ? TRUE : FALSE); } if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree)) panic("vinvalbuf: flush failed"); if (!RB_EMPTY(&vp->v_rbhash_tree)) panic("vinvalbuf: flush failed, buffers still present"); error = 0; done: lwkt_reltoken(&vp->v_token); return (error); } static int vinvalbuf_bp(struct buf *bp, void *data) { struct vinvalbuf_bp_info *info = data; int error; if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { atomic_add_int(&bp->b_refs, 1); error = BUF_TIMELOCK(bp, info->lkflags, "vinvalbuf", info->slptimeo); atomic_add_int(&bp->b_refs, -1); if (error == 0) { BUF_UNLOCK(bp); error = ENOLCK; } if (error == ENOLCK) return(0); return (-error); } KKASSERT(bp->b_vp == info->vp); /* * Must check clean/dirty status after successfully locking as * it may race. */ if ((info->clean && (bp->b_flags & B_DELWRI)) || (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) { BUF_UNLOCK(bp); return(0); } /* * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite() * check. This code will write out the buffer, period. */ bremfree(bp); if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && (info->flags & V_SAVE)) { cluster_awrite(bp); } else if (info->flags & V_SAVE) { /* * Cannot set B_NOCACHE on a clean buffer as this will * destroy the VM backing store which might actually * be dirty (and unsynchronized). */ bp->b_flags |= (B_INVAL | B_RELBUF); brelse(bp); } else { bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); brelse(bp); } return(0); } /* * Truncate a file's buffer and pages to a specified length. This * is in lieu of the old vinvalbuf mechanism, which performed unneeded * sync activity. * * The vnode must be locked. */ static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data); static int vtruncbuf_bp_trunc(struct buf *bp, void *data); static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data); static int vtruncbuf_bp_metasync(struct buf *bp, void *data); struct vtruncbuf_info { struct vnode *vp; off_t truncloffset; int clean; }; int vtruncbuf(struct vnode *vp, off_t length, int blksize) { struct vtruncbuf_info info; const char *filename; int count; /* * Round up to the *next* block, then destroy the buffers in question. * Since we are only removing some of the buffers we must rely on the * scan count to determine whether a loop is necessary. */ if ((count = (int)(length % blksize)) != 0) info.truncloffset = length + (blksize - count); else info.truncloffset = length; info.vp = vp; lwkt_gettoken(&vp->v_token); do { info.clean = 1; count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, vtruncbuf_bp_trunc_cmp, vtruncbuf_bp_trunc, &info); info.clean = 0; count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vtruncbuf_bp_trunc_cmp, vtruncbuf_bp_trunc, &info); } while(count); /* * For safety, fsync any remaining metadata if the file is not being * truncated to 0. Since the metadata does not represent the entire * dirty list we have to rely on the hit count to ensure that we get * all of it. */ if (length > 0) { do { count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vtruncbuf_bp_metasync_cmp, vtruncbuf_bp_metasync, &info); } while (count); } /* * Clean out any left over VM backing store. * * It is possible to have in-progress I/O from buffers that were * not part of the truncation. This should not happen if we * are truncating to 0-length. */ vnode_pager_setsize(vp, length); bio_track_wait(&vp->v_track_write, 0, 0); /* * Debugging only */ spin_lock(&vp->v_spin); filename = TAILQ_FIRST(&vp->v_namecache) ? TAILQ_FIRST(&vp->v_namecache)->nc_name : "?"; spin_unlock(&vp->v_spin); /* * Make sure no buffers were instantiated while we were trying * to clean out the remaining VM pages. This could occur due * to busy dirty VM pages being flushed out to disk. */ do { info.clean = 1; count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, vtruncbuf_bp_trunc_cmp, vtruncbuf_bp_trunc, &info); info.clean = 0; count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vtruncbuf_bp_trunc_cmp, vtruncbuf_bp_trunc, &info); if (count) { kprintf("Warning: vtruncbuf(): Had to re-clean %d " "left over buffers in %s\n", count, filename); } } while(count); lwkt_reltoken(&vp->v_token); return (0); } /* * The callback buffer is beyond the new file EOF and must be destroyed. * Note that the compare function must conform to the RB_SCAN's requirements. */ static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data) { struct vtruncbuf_info *info = data; if (bp->b_loffset >= info->truncloffset) return(0); return(-1); } static int vtruncbuf_bp_trunc(struct buf *bp, void *data) { struct vtruncbuf_info *info = data; /* * Do not try to use a buffer we cannot immediately lock, but sleep * anyway to prevent a livelock. The code will loop until all buffers * can be acted upon. * * We must always revalidate the buffer after locking it to deal * with MP races. */ if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { atomic_add_int(&bp->b_refs, 1); if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) BUF_UNLOCK(bp); atomic_add_int(&bp->b_refs, -1); } else if ((info->clean && (bp->b_flags & B_DELWRI)) || (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) || bp->b_vp != info->vp || vtruncbuf_bp_trunc_cmp(bp, data)) { BUF_UNLOCK(bp); } else { bremfree(bp); bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE); brelse(bp); } return(1); } /* * Fsync all meta-data after truncating a file to be non-zero. Only metadata * blocks (with a negative loffset) are scanned. * Note that the compare function must conform to the RB_SCAN's requirements. */ static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused) { if (bp->b_loffset < 0) return(0); return(1); } static int vtruncbuf_bp_metasync(struct buf *bp, void *data) { struct vtruncbuf_info *info = data; if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { atomic_add_int(&bp->b_refs, 1); if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) BUF_UNLOCK(bp); atomic_add_int(&bp->b_refs, -1); } else if ((bp->b_flags & B_DELWRI) == 0 || bp->b_vp != info->vp || vtruncbuf_bp_metasync_cmp(bp, data)) { BUF_UNLOCK(bp); } else { bremfree(bp); if (bp->b_vp == info->vp) bawrite(bp); else bwrite(bp); } return(1); } /* * vfsync - implements a multipass fsync on a file which understands * dependancies and meta-data. The passed vnode must be locked. The * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY. * * When fsyncing data asynchronously just do one consolidated pass starting * with the most negative block number. This may not get all the data due * to dependancies. * * When fsyncing data synchronously do a data pass, then a metadata pass, * then do additional data+metadata passes to try to get all the data out. * * Caller must ref the vnode but does not have to lock it. */ static int vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *)); static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused); static int vfsync_data_only_cmp(struct buf *bp, void *data); static int vfsync_meta_only_cmp(struct buf *bp, void *data); static int vfsync_lazy_range_cmp(struct buf *bp, void *data); static int vfsync_bp(struct buf *bp, void *data); struct vfsync_info { struct vnode *vp; int fastpass; int synchronous; int syncdeps; int lazycount; int lazylimit; int skippedbufs; int (*checkdef)(struct buf *); int (*cmpfunc)(struct buf *, void *); }; int vfsync(struct vnode *vp, int waitfor, int passes, int (*checkdef)(struct buf *), int (*waitoutput)(struct vnode *, struct thread *)) { struct vfsync_info info; int error; bzero(&info, sizeof(info)); info.vp = vp; if ((info.checkdef = checkdef) == NULL) info.syncdeps = 1; lwkt_gettoken(&vp->v_token); switch(waitfor) { case MNT_LAZY | MNT_NOWAIT: case MNT_LAZY: /* * Lazy (filesystem syncer typ) Asynchronous plus limit the * number of data (not meta) pages we try to flush to 1MB. * A non-zero return means that lazy limit was reached. */ info.lazylimit = 1024 * 1024; info.syncdeps = 1; info.cmpfunc = vfsync_lazy_range_cmp; error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_lazy_range_cmp, vfsync_bp, &info); info.cmpfunc = vfsync_meta_only_cmp; RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp, vfsync_bp, &info); if (error == 0) vp->v_lazyw = 0; else if (!RB_EMPTY(&vp->v_rbdirty_tree)) vn_syncer_add(vp, 1); error = 0; break; case MNT_NOWAIT: /* * Asynchronous. Do a data-only pass and a meta-only pass. */ info.syncdeps = 1; info.cmpfunc = vfsync_data_only_cmp; RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, vfsync_bp, &info); info.cmpfunc = vfsync_meta_only_cmp; RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp, vfsync_bp, &info); error = 0; break; default: /* * Synchronous. Do a data-only pass, then a meta-data+data * pass, then additional integrated passes to try to get * all the dependancies flushed. */ info.cmpfunc = vfsync_data_only_cmp; info.fastpass = 1; RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, vfsync_bp, &info); info.fastpass = 0; error = vfsync_wait_output(vp, waitoutput); if (error == 0) { info.skippedbufs = 0; info.cmpfunc = vfsync_dummy_cmp; RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, vfsync_bp, &info); error = vfsync_wait_output(vp, waitoutput); if (info.skippedbufs) { kprintf("Warning: vfsync skipped %d dirty " "buf%s in pass2!\n", info.skippedbufs, ((info.skippedbufs > 1) ? "s" : "")); } } while (error == 0 && passes > 0 && !RB_EMPTY(&vp->v_rbdirty_tree) ) { info.skippedbufs = 0; if (--passes == 0) { info.synchronous = 1; info.syncdeps = 1; } info.cmpfunc = vfsync_dummy_cmp; error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, vfsync_bp, &info); if (error < 0) error = -error; info.syncdeps = 1; if (error == 0) error = vfsync_wait_output(vp, waitoutput); if (info.skippedbufs && passes == 0) { kprintf("Warning: vfsync skipped %d dirty " "buf%s in final pass!\n", info.skippedbufs, ((info.skippedbufs > 1) ? "s" : "")); } } #if 0 /* * This case can occur normally because vnode lock might * not be held. */ if (!RB_EMPTY(&vp->v_rbdirty_tree)) kprintf("dirty bufs left after final pass\n"); #endif break; } lwkt_reltoken(&vp->v_token); return(error); } static int vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *)) { int error; error = bio_track_wait(&vp->v_track_write, 0, 0); if (waitoutput) error = waitoutput(vp, curthread); return(error); } static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused) { return(0); } static int vfsync_data_only_cmp(struct buf *bp, void *data) { if (bp->b_loffset < 0) return(-1); return(0); } static int vfsync_meta_only_cmp(struct buf *bp, void *data) { if (bp->b_loffset < 0) return(0); return(1); } static int vfsync_lazy_range_cmp(struct buf *bp, void *data) { struct vfsync_info *info = data; if (bp->b_loffset < info->vp->v_lazyw) return(-1); return(0); } static int vfsync_bp(struct buf *bp, void *data) { struct vfsync_info *info = data; struct vnode *vp = info->vp; int error; if (info->fastpass) { /* * Ignore buffers that we cannot immediately lock. */ if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { /* * Removed BUF_TIMELOCK(..., 1), even a 1-tick * delay can mess up performance * * Another reason is that during a dirty-buffer * scan a clustered write can start I/O on buffers * ahead of the scan, causing the scan to not * get a lock here. Usually this means the write * is already in progress so, in fact, we *want* * to skip the buffer. */ ++info->skippedbufs; return(0); } } else if (info->synchronous == 0) { /* * Normal pass, give the buffer a little time to become * available to us. */ atomic_add_int(&bp->b_refs, 1); if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst2", hz / 10)) { atomic_add_int(&bp->b_refs, -1); ++info->skippedbufs; return(0); } atomic_add_int(&bp->b_refs, -1); } else { /* * Synchronous pass, give the buffer a lot of time before * giving up. */ atomic_add_int(&bp->b_refs, 1); if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst3", hz * 10)) { atomic_add_int(&bp->b_refs, -1); ++info->skippedbufs; return(0); } atomic_add_int(&bp->b_refs, -1); } /* * We must revalidate the buffer after locking. */ if ((bp->b_flags & B_DELWRI) == 0 || bp->b_vp != info->vp || info->cmpfunc(bp, data)) { BUF_UNLOCK(bp); return(0); } /* * If syncdeps is not set we do not try to write buffers which have * dependancies. */ if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) { BUF_UNLOCK(bp); return(0); } /* * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer * has been written but an additional handshake with the device * is required before we can dispose of the buffer. We have no idea * how to do this so we have to skip these buffers. */ if (bp->b_flags & B_NEEDCOMMIT) { BUF_UNLOCK(bp); return(0); } /* * Ask bioops if it is ok to sync. If not the VFS may have * set B_LOCKED so we have to cycle the buffer. */ if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) { bremfree(bp); brelse(bp); return(0); } if (info->synchronous) { /* * Synchronous flush. An error may be returned and will * stop the scan. */ bremfree(bp); error = bwrite(bp); } else { /* * Asynchronous flush. We use the error return to support * MNT_LAZY flushes. * * In low-memory situations we revert to synchronous * operation. This should theoretically prevent the I/O * path from exhausting memory in a non-recoverable way. */ vp->v_lazyw = bp->b_loffset; bremfree(bp); if (vm_paging_min()) { /* low memory */ info->lazycount += bp->b_bufsize; bwrite(bp); } else { /* normal */ info->lazycount += cluster_awrite(bp); waitrunningbufspace(); /*vm_wait_nominal();*/ } if (info->lazylimit && info->lazycount >= info->lazylimit) error = 1; else error = 0; } return(-error); } /* * Associate a buffer with a vnode. bp and vp must be locked. bp's on * the vnode rbtrees do not add refs or holds to the vp. * * MPSAFE */ int bgetvp(struct vnode *vp, struct buf *bp, int testsize) { KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0); /* * Insert onto list for new vnode. */ lwkt_gettoken(&vp->v_token); if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) { lwkt_reltoken(&vp->v_token); return (EEXIST); } /* * Diagnostics (mainly for HAMMER debugging). Check for * overlapping buffers. */ if (check_buf_overlap) { struct buf *bx; bx = buf_rb_hash_RB_PREV(bp); if (bx) { if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) { kprintf("bgetvp: overlapl %016jx/%d %016jx " "bx %p bp %p\n", (intmax_t)bx->b_loffset, bx->b_bufsize, (intmax_t)bp->b_loffset, bx, bp); if (check_buf_overlap > 1) panic("bgetvp - overlapping buffer"); } } bx = buf_rb_hash_RB_NEXT(bp); if (bx) { if (bp->b_loffset + testsize > bx->b_loffset) { kprintf("bgetvp: overlapr %016jx/%d %016jx " "bp %p bx %p\n", (intmax_t)bp->b_loffset, testsize, (intmax_t)bx->b_loffset, bp, bx); if (check_buf_overlap > 1) panic("bgetvp - overlapping buffer"); } } } bp->b_vp = vp; bp->b_flags |= B_HASHED; bp->b_flags |= B_VNCLEAN; if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp); lwkt_reltoken(&vp->v_token); return(0); } /* * Disassociate a buffer from a vnode. bp must be locked. vp might not * be. * * MPSAFE */ void brelvp(struct buf *bp) { struct vnode *vp; KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); /* * Delete from old vnode list, if on one. vp token can be * broken during syncer adjustments so hold the vp across the * operation. */ vp = bp->b_vp; vhold(vp); lwkt_gettoken(&vp->v_token); if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) { if (bp->b_flags & B_VNDIRTY) buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); else buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN); } if (bp->b_flags & B_HASHED) { buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp); bp->b_flags &= ~B_HASHED; } bp->b_vp = NULL; /* * Only remove from synclist when no dirty buffers are left AND * the VFS has not flagged the vnode's inode as being dirty. * Our token can be temporarily lost in vn_syncer_remove(), so * vn_syncer_remove() will re-check these flags. */ if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST && RB_EMPTY(&vp->v_rbdirty_tree)) { vn_syncer_remove(vp, 0); } lwkt_reltoken(&vp->v_token); vdrop(vp); } /* * Reassign the buffer to the proper clean/dirty list based on B_DELWRI. * This routine is called when the state of the B_DELWRI bit is changed. * * Must be called with vp->v_token held. We must hold the vp temporarily * while reassigning the buffer. * * MPSAFE */ void reassignbuf(struct buf *bp) { struct vnode *vp = bp->b_vp; int delay; ASSERT_LWKT_TOKEN_HELD(&vp->v_token); ++reassignbufcalls; vhold(vp); /* * B_PAGING flagged buffers cannot be reassigned because their vp * is not fully linked in. */ if (bp->b_flags & B_PAGING) panic("cannot reassign paging buffer"); if (bp->b_flags & B_DELWRI) { /* * Move to the dirty list, add the vnode to the worklist */ if (bp->b_flags & B_VNCLEAN) { buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); bp->b_flags &= ~B_VNCLEAN; } if ((bp->b_flags & B_VNDIRTY) == 0) { if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) { panic("reassignbuf: dup lblk vp %p bp %p", vp, bp); } bp->b_flags |= B_VNDIRTY; } if ((vp->v_flag & VONWORKLST) == 0) { switch (vp->v_type) { case VDIR: delay = dirdelay; break; case VCHR: case VBLK: if (vp->v_rdev && vp->v_rdev->si_mountpoint != NULL) { delay = metadelay; break; } /* fall through */ default: delay = filedelay; } vn_syncer_add(vp, delay); } } else { /* * Move to the clean list, remove the vnode from the worklist * if no dirty blocks remain. */ if (bp->b_flags & B_VNDIRTY) { buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); bp->b_flags &= ~B_VNDIRTY; } if ((bp->b_flags & B_VNCLEAN) == 0) { if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) { panic("reassignbuf: dup lblk vp %p bp %p", vp, bp); } bp->b_flags |= B_VNCLEAN; } /* * Only remove from synclist when no dirty buffers are left * AND the VFS has not flagged the vnode's inode as being * dirty. */ if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST && RB_EMPTY(&vp->v_rbdirty_tree)) { vn_syncer_remove(vp, 0); } } vdrop(vp); } /* * Create a vnode for a block device. Used for mounting the root file * system. * * A vref()'d vnode is returned. */ extern struct vop_ops *devfs_vnode_dev_vops_p; int bdevvp(cdev_t dev, struct vnode **vpp) { struct vnode *vp; struct vnode *nvp; int error; if (dev == NULL) { *vpp = NULLVP; return (ENXIO); } error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p, &nvp, 0, 0); if (error) { *vpp = NULLVP; return (error); } vp = nvp; vp->v_type = VCHR; #if 0 vp->v_rdev = dev; #endif v_associate_rdev(vp, dev); vp->v_umajor = dev->si_umajor; vp->v_uminor = dev->si_uminor; vx_unlock(vp); *vpp = vp; return (0); } int v_associate_rdev(struct vnode *vp, cdev_t dev) { if (dev == NULL) return(ENXIO); if (dev_is_good(dev) == 0) return(ENXIO); KKASSERT(vp->v_rdev == NULL); vp->v_rdev = reference_dev(dev); lwkt_gettoken(&spechash_token); SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext); lwkt_reltoken(&spechash_token); return(0); } void v_release_rdev(struct vnode *vp) { cdev_t dev; if ((dev = vp->v_rdev) != NULL) { lwkt_gettoken(&spechash_token); SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext); vp->v_rdev = NULL; release_dev(dev); lwkt_reltoken(&spechash_token); } } /* * Add a vnode to the alias list hung off the cdev_t. We only associate * the device number with the vnode. The actual device is not associated * until the vnode is opened (usually in spec_open()), and will be * disassociated on last close. */ void addaliasu(struct vnode *nvp, int x, int y) { if (nvp->v_type != VBLK && nvp->v_type != VCHR) panic("addaliasu on non-special vnode"); nvp->v_umajor = x; nvp->v_uminor = y; } /* * Simple call that a filesystem can make to try to get rid of a * vnode. It will fail if anyone is referencing the vnode (including * the caller). * * The filesystem can check whether its in-memory inode structure still * references the vp on return. * * May only be called if the vnode is in a known state (i.e. being prevented * from being deallocated by some other condition such as a vfs inode hold). * * This call might not succeed. */ void vclean_unlocked(struct vnode *vp) { vx_get(vp); if (VREFCNT(vp) <= 1) vgone_vxlocked(vp); vx_put(vp); } /* * Disassociate a vnode from its underlying filesystem. * * The vnode must be VX locked and referenced. In all normal situations * there are no active references. If vclean_vxlocked() is called while * there are active references, the vnode is being ripped out and we have * to call VOP_CLOSE() as appropriate before we can reclaim it. */ static void vclean_vxlocked(struct vnode *vp, int flags) { int active; int n; vm_object_t object; struct namecache *ncp; /* * If the vnode has already been reclaimed we have nothing to do. */ if (vp->v_flag & VRECLAIMED) return; /* * Set flag to interlock operation, flag finalization to ensure * that the vnode winds up on the inactive list, and set v_act to 0. */ vsetflags(vp, VRECLAIMED); atomic_set_int(&vp->v_refcnt, VREF_FINALIZE); vp->v_act = 0; if (verbose_reclaims) { if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name); } /* * Scrap the vfs cache */ while (cache_inval_vp(vp, 0) != 0) { kprintf("Warning: vnode %p clean/cache_resolution " "race detected\n", vp); tsleep(vp, 0, "vclninv", 2); } /* * Check to see if the vnode is in use. If so we have to reference it * before we clean it out so that its count cannot fall to zero and * generate a race against ourselves to recycle it. */ active = (VREFCNT(vp) > 0); /* * Clean out any buffers associated with the vnode and destroy its * object, if it has one. */ vinvalbuf(vp, V_SAVE, 0, 0); /* * If purging an active vnode (typically during a forced unmount * or reboot), it must be closed and deactivated before being * reclaimed. This isn't really all that safe, but what can * we do? XXX. * * Note that neither of these routines unlocks the vnode. */ if (active && (flags & DOCLOSE)) { while ((n = vp->v_opencount) != 0) { if (vp->v_writecount) VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL); else VOP_CLOSE(vp, FNONBLOCK, NULL); if (vp->v_opencount == n) { kprintf("Warning: unable to force-close" " vnode %p\n", vp); break; } } } /* * If the vnode has not been deactivated, deactivated it. Deactivation * can create new buffers and VM pages so we have to call vinvalbuf() * again to make sure they all get flushed. * * This can occur if a file with a link count of 0 needs to be * truncated. * * If the vnode is already dead don't try to deactivate it. */ if ((vp->v_flag & VINACTIVE) == 0) { vsetflags(vp, VINACTIVE); if (vp->v_mount) VOP_INACTIVE(vp); vinvalbuf(vp, V_SAVE, 0, 0); } /* * If the vnode has an object, destroy it. */ while ((object = vp->v_object) != NULL) { vm_object_hold(object); if (object == vp->v_object) break; vm_object_drop(object); } if (object != NULL) { if (object->ref_count == 0) { if ((object->flags & OBJ_DEAD) == 0) vm_object_terminate(object); vm_object_drop(object); vclrflags(vp, VOBJBUF); } else { vm_pager_deallocate(object); vclrflags(vp, VOBJBUF); vm_object_drop(object); } } KKASSERT((vp->v_flag & VOBJBUF) == 0); if (vp->v_flag & VOBJDIRTY) vclrobjdirty(vp); /* * Reclaim the vnode if not already dead. */ if (vp->v_mount && VOP_RECLAIM(vp)) panic("vclean: cannot reclaim"); /* * Done with purge, notify sleepers of the grim news. */ vp->v_ops = &dead_vnode_vops_p; vn_gone(vp); vp->v_tag = VT_NON; /* * If we are destroying an active vnode, reactivate it now that * we have reassociated it with deadfs. This prevents the system * from crashing on the vnode due to it being unexpectedly marked * as inactive or reclaimed. */ if (active && (flags & DOCLOSE)) { vclrflags(vp, VINACTIVE | VRECLAIMED); } } /* * Eliminate all activity associated with the requested vnode * and with all vnodes aliased to the requested vnode. * * The vnode must be referenced but should not be locked. */ int vrevoke(struct vnode *vp, struct ucred *cred) { struct vnode *vq; struct vnode *vqn; cdev_t dev; int error; /* * If the vnode has a device association, scrap all vnodes associated * with the device. Don't let the device disappear on us while we * are scrapping the vnodes. * * The passed vp will probably show up in the list, do not VX lock * it twice! * * Releasing the vnode's rdev here can mess up specfs's call to * device close, so don't do it. The vnode has been disassociated * and the device will be closed after the last ref on the related * fp goes away (if not still open by e.g. the kernel). */ if (vp->v_type != VCHR) { error = fdrevoke(vp, DTYPE_VNODE, cred); return (error); } if ((dev = vp->v_rdev) == NULL) { return(0); } reference_dev(dev); lwkt_gettoken(&spechash_token); restart: vqn = SLIST_FIRST(&dev->si_hlist); if (vqn) vhold(vqn); while ((vq = vqn) != NULL) { if (VREFCNT(vq) > 0) { vref(vq); fdrevoke(vq, DTYPE_VNODE, cred); /*v_release_rdev(vq);*/ vrele(vq); if (vq->v_rdev != dev) { vdrop(vq); goto restart; } } vqn = SLIST_NEXT(vq, v_cdevnext); if (vqn) vhold(vqn); vdrop(vq); } lwkt_reltoken(&spechash_token); dev_drevoke(dev); release_dev(dev); return (0); } /* * This is called when the object underlying a vnode is being destroyed, * such as in a remove(). Try to recycle the vnode immediately if the * only active reference is our reference. * * Directory vnodes in the namecache with children cannot be immediately * recycled because numerous VOP_N*() ops require them to be stable. * * To avoid recursive recycling from VOP_INACTIVE implemenetations this * function is a NOP if VRECLAIMED is already set. */ int vrecycle(struct vnode *vp) { if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) { if (cache_inval_vp_nonblock(vp)) return(0); vgone_vxlocked(vp); return (1); } return (0); } /* * Return the maximum I/O size allowed for strategy calls on VP. * * If vp is VCHR or VBLK we dive the device, otherwise we use * the vp's mount info. * * The returned value is clamped at MAXPHYS as most callers cannot use * buffers larger than that size. */ int vmaxiosize(struct vnode *vp) { int maxiosize; if (vp->v_type == VBLK || vp->v_type == VCHR) maxiosize = vp->v_rdev->si_iosize_max; else maxiosize = vp->v_mount->mnt_iosize_max; if (maxiosize > MAXPHYS) maxiosize = MAXPHYS; return (maxiosize); } /* * Eliminate all activity associated with a vnode in preparation for * destruction. * * The vnode must be VX locked and refd and will remain VX locked and refd * on return. This routine may be called with the vnode in any state, as * long as it is VX locked. The vnode will be cleaned out and marked * VRECLAIMED but will not actually be reused until all existing refs and * holds go away. * * NOTE: This routine may be called on a vnode which has not yet been * already been deactivated (VOP_INACTIVE), or on a vnode which has * already been reclaimed. * * This routine is not responsible for placing us back on the freelist. * Instead, it happens automatically when the caller releases the VX lock * (assuming there aren't any other references). */ void vgone_vxlocked(struct vnode *vp) { /* * assert that the VX lock is held. This is an absolute requirement * now for vgone_vxlocked() to be called. */ KKASSERT(lockinuse(&vp->v_lock)); /* * Clean out the filesystem specific data and set the VRECLAIMED * bit. Also deactivate the vnode if necessary. * * The vnode should have automatically been removed from the syncer * list as syncer/dirty flags cleared during the cleaning. */ vclean_vxlocked(vp, DOCLOSE); /* * Normally panic if the vnode is still dirty, unless we are doing * a forced unmount (tmpfs typically). */ if (vp->v_flag & VONWORKLST) { if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) { /* force removal */ vn_syncer_remove(vp, 1); } else { panic("vp %p still dirty in vgone after flush", vp); } } /* * Delete from old mount point vnode list, if on one. */ if (vp->v_mount != NULL) { KKASSERT(vp->v_data == NULL); insmntque(vp, NULL); } /* * If special device, remove it from special device alias list * if it is on one. This should normally only occur if a vnode is * being revoked as the device should otherwise have been released * naturally. */ if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) { v_release_rdev(vp); } /* * Set us to VBAD */ vp->v_type = VBAD; } /* * Calculate the total number of references to a special device. This * routine may only be called for VBLK and VCHR vnodes since v_rdev is * an overloaded field. Since dev_from_devid() can now return NULL, we * have to check for a NULL v_rdev. */ int count_dev(cdev_t dev) { struct vnode *vp; int count = 0; if (SLIST_FIRST(&dev->si_hlist)) { lwkt_gettoken(&spechash_token); SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) { count += vp->v_opencount; } lwkt_reltoken(&spechash_token); } return(count); } int vcount(struct vnode *vp) { if (vp->v_rdev == NULL) return(0); return(count_dev(vp->v_rdev)); } /* * Initialize VMIO for a vnode. This routine MUST be called before a * VFS can issue buffer cache ops on a vnode. It is typically called * when a vnode is initialized from its inode. */ int vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff) { vm_object_t object; int error = 0; object = vp->v_object; if (object) { vm_object_hold(object); KKASSERT(vp->v_object == object); } if (object == NULL) { object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff); /* * Dereference the reference we just created. This assumes * that the object is associated with the vp. Allow it to * have zero refs. It cannot be destroyed as long as it * is associated with the vnode. */ vm_object_hold(object); atomic_add_int(&object->ref_count, -1); vrele(vp); } else { KKASSERT((object->flags & OBJ_DEAD) == 0); } KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object")); vsetflags(vp, VOBJBUF); vm_object_drop(object); return (error); } /* * Print out a description of a vnode. */ static char *typename[] = {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; void vprint(char *label, struct vnode *vp) { char buf[96]; if (label != NULL) kprintf("%s: %p: ", label, (void *)vp); else kprintf("%p: ", (void *)vp); kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,", typename[vp->v_type], vp->v_refcnt, vp->v_writecount, vp->v_auxrefs); buf[0] = '\0'; if (vp->v_flag & VROOT) strcat(buf, "|VROOT"); if (vp->v_flag & VPFSROOT) strcat(buf, "|VPFSROOT"); if (vp->v_flag & VTEXT) strcat(buf, "|VTEXT"); if (vp->v_flag & VSYSTEM) strcat(buf, "|VSYSTEM"); if (vp->v_flag & VOBJBUF) strcat(buf, "|VOBJBUF"); if (buf[0] != '\0') kprintf(" flags (%s)", &buf[1]); if (vp->v_data == NULL) { kprintf("\n"); } else { kprintf("\n\t"); VOP_PRINT(vp); } } /* * Do the usual access checking. * file_mode, uid and gid are from the vnode in question, * while acc_mode and cred are from the VOP_ACCESS parameter list */ int vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, mode_t acc_mode, struct ucred *cred) { mode_t mask; int ismember; /* * Super-user always gets read/write access, but execute access depends * on at least one execute bit being set. */ if (caps_priv_check(cred, SYSCAP_RESTRICTEDROOT) == 0) { if ((acc_mode & VEXEC) && type != VDIR && (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) return (EACCES); return (0); } mask = 0; /* Otherwise, check the owner. */ if (cred->cr_uid == uid) { if (acc_mode & VEXEC) mask |= S_IXUSR; if (acc_mode & VREAD) mask |= S_IRUSR; if (acc_mode & VWRITE) mask |= S_IWUSR; return ((file_mode & mask) == mask ? 0 : EACCES); } /* Otherwise, check the groups. */ ismember = groupmember(gid, cred); if (cred->cr_svgid == gid || ismember) { if (acc_mode & VEXEC) mask |= S_IXGRP; if (acc_mode & VREAD) mask |= S_IRGRP; if (acc_mode & VWRITE) mask |= S_IWGRP; return ((file_mode & mask) == mask ? 0 : EACCES); } /* Otherwise, check everyone else. */ if (acc_mode & VEXEC) mask |= S_IXOTH; if (acc_mode & VREAD) mask |= S_IROTH; if (acc_mode & VWRITE) mask |= S_IWOTH; return ((file_mode & mask) == mask ? 0 : EACCES); } #ifdef DDB #include <ddb/ddb.h> static int db_show_locked_vnodes(struct mount *mp, void *data); /* * List all of the locked vnodes in the system. * Called when debugging the kernel. */ DB_SHOW_COMMAND(lockedvnodes, lockedvnodes) { kprintf("Locked vnodes\n"); mountlist_scan(db_show_locked_vnodes, NULL, MNTSCAN_FORWARD|MNTSCAN_NOBUSY); } static int db_show_locked_vnodes(struct mount *mp, void *data __unused) { struct vnode *vp; TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { if (vn_islocked(vp)) vprint(NULL, vp); } return(0); } #endif /* * Top level filesystem related information gathering. */ static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS); static int vfs_sysctl(SYSCTL_HANDLER_ARGS) { int *name = (int *)arg1 - 1; /* XXX */ u_int namelen = arg2 + 1; /* XXX */ struct vfsconf *vfsp; int maxtypenum; #if 1 || defined(COMPAT_PRELITE2) /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ if (namelen == 1) return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); #endif #ifdef notyet /* all sysctl names at this level are at least name and field */ if (namelen < 2) return (ENOTDIR); /* overloaded */ if (name[0] != VFS_GENERIC) { vfsp = vfsconf_find_by_typenum(name[0]); if (vfsp == NULL) return (EOPNOTSUPP); return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, oldp, oldlenp, newp, newlen, p)); } #endif switch (name[1]) { case VFS_MAXTYPENUM: if (namelen != 2) return (ENOTDIR); maxtypenum = vfsconf_get_maxtypenum(); return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum))); case VFS_CONF: if (namelen != 3) return (ENOTDIR); /* overloaded */ vfsp = vfsconf_find_by_typenum(name[2]); if (vfsp == NULL) return (EOPNOTSUPP); return (SYSCTL_OUT(req, vfsp, sizeof *vfsp)); } return (EOPNOTSUPP); } SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl, "Generic filesystem"); #if 1 || defined(COMPAT_PRELITE2) static int sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data) { int error; struct ovfsconf ovfs; struct sysctl_req *req = (struct sysctl_req*) data; bzero(&ovfs, sizeof(ovfs)); ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ strcpy(ovfs.vfc_name, vfsp->vfc_name); ovfs.vfc_index = vfsp->vfc_typenum; ovfs.vfc_refcount = vfsp->vfc_refcount; ovfs.vfc_flags = vfsp->vfc_flags; error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); if (error) return error; /* abort iteration with error code */ else return 0; /* continue iterating with next element */ } static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) { return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req); } #endif /* 1 || COMPAT_PRELITE2 */ /* * Check to see if a filesystem is mounted on a block device. */ int vfs_mountedon(struct vnode *vp) { cdev_t dev; dev = vp->v_rdev; if (dev != NULL && dev->si_mountpoint) return (EBUSY); return (0); } /* * Unmount all filesystems. The list is traversed in reverse order * of mounting to avoid dependencies. * * We want the umountall to be able to break out of its loop if a * failure occurs, after scanning all possible mounts, so the callback * returns 0 on error. * * NOTE: Do not call mountlist_remove(mp) on error any more, this will * confuse mountlist_scan()'s unbusy check. */ static int vfs_umountall_callback(struct mount *mp, void *data); void vfs_unmountall(int halting) { int count; do { count = mountlist_scan(vfs_umountall_callback, &halting, MNTSCAN_REVERSE|MNTSCAN_NOBUSY); } while (count); } static int vfs_umountall_callback(struct mount *mp, void *data) { int error; int halting = *(int *)data; /* * NOTE: When halting, dounmount will disconnect but leave * certain mount points intact. e.g. devfs. */ error = dounmount(mp, MNT_FORCE, halting); if (error) { kprintf("unmount of filesystem mounted from %s failed (", mp->mnt_stat.f_mntfromname); if (error == EBUSY) kprintf("BUSY)\n"); else kprintf("%d)\n", error); return 0; } else { return 1; } } /* * Checks the mount flags for parameter mp and put the names comma-separated * into a string buffer buf with a size limit specified by len. * * It returns the number of bytes written into buf, and (*errorp) will be * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was * not large enough). The buffer will be 0-terminated if len was not 0. */ size_t vfs_flagstostr(int flags, const struct mountctl_opt *optp, char *buf, size_t len, int *errorp) { static const struct mountctl_opt optnames[] = { { MNT_RDONLY, "read-only" }, { MNT_SYNCHRONOUS, "synchronous" }, { MNT_NOEXEC, "noexec" }, { MNT_NOSUID, "nosuid" }, { MNT_NODEV, "nodev" }, { MNT_AUTOMOUNTED, "automounted" }, { MNT_ASYNC, "asynchronous" }, { MNT_SUIDDIR, "suiddir" }, { MNT_SOFTDEP, "soft-updates" }, { MNT_NOSYMFOLLOW, "nosymfollow" }, { MNT_TRIM, "trim" }, { MNT_NOATIME, "noatime" }, { MNT_NOCLUSTERR, "noclusterr" }, { MNT_NOCLUSTERW, "noclusterw" }, { MNT_EXRDONLY, "NFS read-only" }, { MNT_EXPORTED, "NFS exported" }, /* Remaining NFS flags could come here */ { MNT_LOCAL, "local" }, { MNT_QUOTA, "with-quotas" }, /* { MNT_ROOTFS, "rootfs" }, */ /* { MNT_IGNORE, "ignore" }, */ { 0, NULL} }; int bwritten; int bleft; int optlen; int actsize; *errorp = 0; bwritten = 0; bleft = len - 1; /* leave room for trailing \0 */ /* * Checks the size of the string. If it contains * any data, then we will append the new flags to * it. */ actsize = strlen(buf); if (actsize > 0) buf += actsize; /* Default flags if no flags passed */ if (optp == NULL) optp = optnames; if (bleft < 0) { /* degenerate case, 0-length buffer */ *errorp = EINVAL; return(0); } for (; flags && optp->o_opt; ++optp) { if ((flags & optp->o_opt) == 0) continue; optlen = strlen(optp->o_name); if (bwritten || actsize > 0) { if (bleft < 2) { *errorp = ENOSPC; break; } buf[bwritten++] = ','; buf[bwritten++] = ' '; bleft -= 2; } if (bleft < optlen) { *errorp = ENOSPC; break; } bcopy(optp->o_name, buf + bwritten, optlen); bwritten += optlen; bleft -= optlen; flags &= ~optp->o_opt; } /* * Space already reserved for trailing \0 */ buf[bwritten] = 0; return (bwritten); } /* * Build hash lists of net addresses and hang them off the mount point. * Called by ufs_mount() to set up the lists of export addresses. */ static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep, const struct export_args *argp) { struct netcred *np; struct radix_node_head *rnh; int i; struct radix_node *rn; struct sockaddr *saddr, *smask = NULL; int error; if (argp->ex_addrlen == 0) { if (mp->mnt_flag & MNT_DEFEXPORTED) return (EPERM); np = &nep->ne_defexported; np->netc_exflags = argp->ex_flags; np->netc_anon = argp->ex_anon; np->netc_anon.cr_ref = 1; mp->mnt_flag |= MNT_DEFEXPORTED; return (0); } if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN) return (EINVAL); if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN) return (EINVAL); i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; np = (struct netcred *)kmalloc(i, M_NETCRED, M_WAITOK | M_ZERO); saddr = (struct sockaddr *) (np + 1); if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen))) goto out; if (saddr->sa_len > argp->ex_addrlen) saddr->sa_len = argp->ex_addrlen; if (argp->ex_masklen) { smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen); if (error) goto out; if (smask->sa_len > argp->ex_masklen) smask->sa_len = argp->ex_masklen; } NE_LOCK(nep); if (nep->ne_maskhead == NULL) { if (!rn_inithead(&nep->ne_maskhead, NULL, 0)) { error = ENOBUFS; goto out; } } if ((rnh = vfs_create_addrlist_af(saddr->sa_family, nep)) == NULL) { error = ENOBUFS; goto out; } rn = rnh->rnh_addaddr(saddr, smask, rnh, np->netc_rnodes); NE_UNLOCK(nep); if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ error = EPERM; goto out; } np->netc_exflags = argp->ex_flags; np->netc_anon = argp->ex_anon; np->netc_anon.cr_ref = 1; return (0); out: kfree(np, M_NETCRED); return (error); } /* * Free netcred structures installed in the netexport */ static void vfs_free_netcred(struct radix_node *rn) { struct netcred *np; np = (struct netcred *)rn; kfree(np, M_NETCRED); } static struct radix_node_head * vfs_create_addrlist_af(int af, struct netexport *nep) { struct radix_node_head *rnh = NULL; #if defined(INET) || defined(INET6) struct radix_node_head *maskhead = nep->ne_maskhead; int off; #endif NE_ASSERT_LOCKED(nep); #if defined(INET) || defined(INET6) KKASSERT(maskhead != NULL); #endif switch (af) { #ifdef INET case AF_INET: if ((rnh = nep->ne_inethead) == NULL) { off = offsetof(struct sockaddr_in, sin_addr); if (!rn_inithead(&rnh, maskhead, off)) return (NULL); nep->ne_inethead = rnh; } break; #endif #ifdef INET6 case AF_INET6: if ((rnh = nep->ne_inet6head) == NULL) { off = offsetof(struct sockaddr_in6, sin6_addr); if (!rn_inithead(&rnh, maskhead, off)) return (NULL); nep->ne_inet6head = rnh; } break; #endif } return (rnh); } /* * Free the net address hash lists that are hanging off the mount points. */ static void vfs_free_addrlist(struct netexport *nep) { NE_LOCK(nep); if (nep->ne_inethead != NULL) { rn_flush(nep->ne_inethead, vfs_free_netcred); rn_freehead(nep->ne_inethead); nep->ne_inethead = NULL; } if (nep->ne_inet6head != NULL) { rn_flush(nep->ne_inet6head, vfs_free_netcred); rn_freehead(nep->ne_inet6head); nep->ne_inet6head = NULL; } if (nep->ne_maskhead != NULL) { rn_flush(nep->ne_maskhead, rn_freemask); rn_freehead(nep->ne_maskhead); nep->ne_maskhead = NULL; } NE_UNLOCK(nep); } int vfs_export(struct mount *mp, struct netexport *nep, const struct export_args *argp) { int error; if (argp->ex_flags & MNT_DELEXPORT) { if (mp->mnt_flag & MNT_EXPUBLIC) { vfs_setpublicfs(NULL, NULL, NULL); mp->mnt_flag &= ~MNT_EXPUBLIC; } vfs_free_addrlist(nep); mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); } if (argp->ex_flags & MNT_EXPORTED) { if (argp->ex_flags & MNT_EXPUBLIC) { if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) return (error); mp->mnt_flag |= MNT_EXPUBLIC; } if ((error = vfs_hang_addrlist(mp, nep, argp))) return (error); mp->mnt_flag |= MNT_EXPORTED; } return (0); } /* * Set the publicly exported filesystem (WebNFS). Currently, only * one public filesystem is possible in the spec (RFC 2054 and 2055) */ int vfs_setpublicfs(struct mount *mp, struct netexport *nep, const struct export_args *argp) { int error; struct vnode *rvp; char *cp; /* * mp == NULL -> invalidate the current info, the FS is * no longer exported. May be called from either vfs_export * or unmount, so check if it hasn't already been done. */ if (mp == NULL) { if (nfs_pub.np_valid) { nfs_pub.np_valid = 0; if (nfs_pub.np_index != NULL) { kfree(nfs_pub.np_index, M_TEMP); nfs_pub.np_index = NULL; } } return (0); } /* * Only one allowed at a time. */ if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) return (EBUSY); /* * Get real filehandle for root of exported FS. */ bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; if ((error = VFS_ROOT(mp, &rvp))) return (error); if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) return (error); vput(rvp); /* * If an indexfile was specified, pull it in. */ if (argp->ex_indexfile != NULL) { int namelen; error = vn_get_namelen(rvp, &namelen); if (error) return (error); nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK); error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, namelen, NULL); if (!error) { /* * Check for illegal filenames. */ for (cp = nfs_pub.np_index; *cp; cp++) { if (*cp == '/') { error = EINVAL; break; } } } if (error) { kfree(nfs_pub.np_index, M_TEMP); return (error); } } nfs_pub.np_mount = mp; nfs_pub.np_valid = 1; return (0); } struct netcred * vfs_export_lookup(struct mount *mp, struct netexport *nep, struct sockaddr *nam) { struct netcred *np; struct radix_node_head *rnh; struct sockaddr *saddr; np = NULL; if (mp->mnt_flag & MNT_EXPORTED) { /* * Lookup in the export list first. */ NE_LOCK(nep); if (nam != NULL) { saddr = nam; switch (saddr->sa_family) { #ifdef INET case AF_INET: rnh = nep->ne_inethead; break; #endif #ifdef INET6 case AF_INET6: rnh = nep->ne_inet6head; break; #endif default: rnh = NULL; } if (rnh != NULL) { np = (struct netcred *) rnh->rnh_matchaddr(saddr, rnh); if (np && np->netc_rnodes->rn_flags & RNF_ROOT) np = NULL; } } NE_UNLOCK(nep); /* * If no address match, use the default if it exists. */ if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) np = &nep->ne_defexported; } return (np); } /* * perform msync on all vnodes under a mount point. The mount point must * be locked. This code is also responsible for lazy-freeing unreferenced * vnodes whos VM objects no longer contain pages. * * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state. * * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked, * but vnode_pager_putpages() doesn't lock the vnode. We have to do it * way up in this high level function. */ static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data); static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data); void vfs_msync(struct mount *mp, int flags) { int vmsc_flags; /* * tmpfs sets this flag to prevent msync(), sync, and the * filesystem periodic syncer from trying to flush VM pages * to swap. Only pure memory pressure flushes tmpfs VM pages * to swap. */ if (mp->mnt_kern_flag & MNTK_NOMSYNC) return; /* * Ok, scan the vnodes for work. If the filesystem is using the * syncer thread feature we can use vsyncscan() instead of * vmntvnodescan(), which is much faster. */ vmsc_flags = VMSC_GETVP; if (flags != MNT_WAIT) vmsc_flags |= VMSC_NOWAIT; if (mp->mnt_kern_flag & MNTK_THR_SYNC) { vsyncscan(mp, vmsc_flags, vfs_msync_scan2, (void *)(intptr_t)flags); } else { vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2, (void *)(intptr_t)flags); } } /* * scan1 is a fast pre-check. There could be hundreds of thousands of * vnodes, we cannot afford to do anything heavy weight until we have a * fairly good indication that there is work to do. * * The new namecache holds the vnode for each v_namecache association * so allow these refs. */ static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data) { int flags = (int)(intptr_t)data; if ((vp->v_flag & VRECLAIMED) == 0) { if (vp->v_auxrefs == vp->v_namecache_count && VREFCNT(vp) <= 0 && vp->v_object) { return(0); /* call scan2 */ } if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY) && (flags == MNT_WAIT || vn_islocked(vp) == 0)) { return(0); /* call scan2 */ } } /* * do not call scan2, continue the loop */ return(-1); } /* * This callback is handed a locked vnode. */ static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data) { vm_object_t obj; int flags = (int)(intptr_t)data; int opcflags; if (vp->v_flag & VRECLAIMED) return(0); if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) { if ((obj = vp->v_object) != NULL) { if (flags == MNT_WAIT) { /* * VFS_MSYNC is called with MNT_WAIT when * unmounting. */ opcflags = OBJPC_SYNC; } else if (vp->v_writecount || obj->ref_count) { /* * VFS_MSYNC is otherwise called via the * periodic filesystem sync or the 'sync' * command. Honor MADV_NOSYNC / MAP_NOSYNC * if the file is open for writing or memory * mapped. Pages flagged PG_NOSYNC will not * be automatically flushed at this time. * * The obj->ref_count test is not perfect * since temporary refs may be present, but * the periodic filesystem sync will ultimately * catch it if the file is not open and not * mapped. */ opcflags = OBJPC_NOSYNC; } else { /* * If the file is no longer open for writing * and also no longer mapped, do not honor * MAP_NOSYNC. That is, fully synchronize * the file. * * This still occurs on the periodic fs sync, * so frontend programs which turn the file * over quickly enough can still avoid the * sync, but ultimately we do want to flush * even MADV_NOSYNC pages once it is no longer * mapped or open for writing. */ opcflags = 0; } vm_object_page_clean(obj, 0, 0, opcflags); } } return(0); } /* * Wake up anyone interested in vp because it is being revoked. */ void vn_gone(struct vnode *vp) { lwkt_gettoken(&vp->v_token); KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE); lwkt_reltoken(&vp->v_token); } /* * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened * (or v_rdev might be NULL). */ cdev_t vn_todev(struct vnode *vp) { if (vp->v_type != VBLK && vp->v_type != VCHR) return (NULL); KKASSERT(vp->v_rdev != NULL); return (vp->v_rdev); } /* * Check if vnode represents a disk device. The vnode does not need to be * opened. * * MPALMOSTSAFE */ int vn_isdisk(struct vnode *vp, int *errp) { cdev_t dev; if (vp->v_type != VCHR) { if (errp != NULL) *errp = ENOTBLK; return (0); } dev = vp->v_rdev; if (dev == NULL) { if (errp != NULL) *errp = ENXIO; return (0); } if (dev_is_good(dev) == 0) { if (errp != NULL) *errp = ENXIO; return (0); } if ((dev_dflags(dev) & D_DISK) == 0) { if (errp != NULL) *errp = ENOTBLK; return (0); } if (errp != NULL) *errp = 0; return (1); } int vn_get_namelen(struct vnode *vp, int *namelen) { int error; register_t retval[2]; error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval); if (error) return (error); *namelen = (int)retval[0]; return (0); } int vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type, uint16_t d_namlen, const char *d_name) { struct dirent *dp; size_t len; len = _DIRENT_RECLEN(d_namlen); if (len > uio->uio_resid) return(1); dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO); dp->d_ino = d_ino; dp->d_namlen = d_namlen; dp->d_type = d_type; bcopy(d_name, dp->d_name, d_namlen); *error = uiomove((caddr_t)dp, len, uio); kfree(dp, M_TEMP); return(0); } void vn_mark_atime(struct vnode *vp, struct thread *td) { struct proc *p = td->td_proc; struct ucred *cred = p ? p->p_ucred : proc0.p_ucred; if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) { VOP_MARKATIME(vp, cred); } } /* * Calculate the number of entries in an inode-related chained hash table. * With today's memory sizes, maxvnodes can wind up being a very large * number. There is no reason to waste memory, so tolerate some stacking. */ int vfs_inodehashsize(void) { int hsize; hsize = 32; while (hsize < maxvnodes) hsize <<= 1; while (hsize > maxvnodes * 2) hsize >>= 1; /* nominal 2x stacking */ if (maxvnodes > 1024 * 1024) hsize >>= 1; /* nominal 8x stacking */ if (maxvnodes > 128 * 1024) hsize >>= 1; /* nominal 4x stacking */ if (hsize < 16) hsize = 16; return hsize; } union _qcvt { quad_t qcvt; int32_t val[2]; }; #define SETHIGH(q, h) { \ union _qcvt tmp; \ tmp.qcvt = (q); \ tmp.val[_QUAD_HIGHWORD] = (h); \ (q) = tmp.qcvt; \ } #define SETLOW(q, l) { \ union _qcvt tmp; \ tmp.qcvt = (q); \ tmp.val[_QUAD_LOWWORD] = (l); \ (q) = tmp.qcvt; \ } u_quad_t init_va_filerev(void) { struct timeval tv; u_quad_t ret = 0; getmicrouptime(&tv); SETHIGH(ret, tv.tv_sec); SETLOW(ret, tv.tv_usec * 4294); return ret; } /* * Set default timestamp_precision. If hz is reasonably high we go for * performance and limit vfs timestamps to microseconds with tick resolution. * If hz is too low, however, we lose a bit of performance to get a more * precise timestamp, because the mtime/ctime granularity might just be too * rough otherwise (for make and Makefile's, for example). */ static void vfs_ts_prec_init(void *dummy) { if (timestamp_precision < 0) { if (hz >= 100) timestamp_precision = TSP_USEC; else timestamp_precision = TSP_USEC_PRECISE; } } SYSINIT(vfs_ts_prec_init, SI_SUB_VFS, SI_ORDER_ANY, vfs_ts_prec_init, NULL); |