sys/vfs/devfs/devfs_vnops.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 | /* * (MPSAFE) * * Copyright (c) 2009 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Alex Hornung <ahornung@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/time.h> #include <sys/kernel.h> #include <sys/lock.h> #include <sys/fcntl.h> #include <sys/proc.h> #include <sys/caps.h> #include <sys/signalvar.h> #include <sys/vnode.h> #include <sys/uio.h> #include <sys/mount.h> #include <sys/file.h> #include <sys/dirent.h> #include <sys/malloc.h> #include <sys/stat.h> #include <sys/reg.h> #include <vm/vm_pager.h> #include <vm/vm_zone.h> #include <vm/vm_object.h> #include <sys/filio.h> #include <sys/ttycom.h> #include <sys/tty.h> #include <sys/diskslice.h> #include <sys/sysctl.h> #include <sys/devfs.h> #include <sys/pioctl.h> #include <vfs/fifofs/fifo.h> #include <machine/limits.h> #include <sys/buf2.h> #include <vm/vm_page2.h> #ifndef SPEC_CHAIN_DEBUG #define SPEC_CHAIN_DEBUG 0 #endif MALLOC_DECLARE(M_DEVFS); #define DEVFS_BADOP (void *)devfs_vop_badop static int devfs_vop_badop(struct vop_generic_args *); static int devfs_vop_access(struct vop_access_args *); static int devfs_vop_inactive(struct vop_inactive_args *); static int devfs_vop_reclaim(struct vop_reclaim_args *); static int devfs_vop_readdir(struct vop_readdir_args *); static int devfs_vop_getattr(struct vop_getattr_args *); static int devfs_vop_setattr(struct vop_setattr_args *); static int devfs_vop_readlink(struct vop_readlink_args *); static int devfs_vop_print(struct vop_print_args *); static int devfs_vop_nresolve(struct vop_nresolve_args *); static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *); static int devfs_vop_nmkdir(struct vop_nmkdir_args *); static int devfs_vop_nsymlink(struct vop_nsymlink_args *); static int devfs_vop_nrmdir(struct vop_nrmdir_args *); static int devfs_vop_nremove(struct vop_nremove_args *); static int devfs_spec_open(struct vop_open_args *); static int devfs_spec_close(struct vop_close_args *); static int devfs_spec_fsync(struct vop_fsync_args *); static int devfs_spec_read(struct vop_read_args *); static int devfs_spec_write(struct vop_write_args *); static int devfs_spec_ioctl(struct vop_ioctl_args *); static int devfs_spec_kqfilter(struct vop_kqfilter_args *); static int devfs_spec_strategy(struct vop_strategy_args *); static void devfs_spec_strategy_done(struct bio *); static int devfs_spec_freeblks(struct vop_freeblks_args *); static int devfs_spec_bmap(struct vop_bmap_args *); static int devfs_spec_advlock(struct vop_advlock_args *); static void devfs_spec_getpages_iodone(struct bio *); static int devfs_spec_getpages(struct vop_getpages_args *); static int devfs_fo_close(struct file *); static int devfs_fo_read(struct file *, struct uio *, struct ucred *, int); static int devfs_fo_write(struct file *, struct uio *, struct ucred *, int); static int devfs_fo_stat(struct file *, struct stat *, struct ucred *); static int devfs_fo_kqfilter(struct file *, struct knote *); static int devfs_fo_ioctl(struct file *, u_long, caddr_t, struct ucred *, struct sysmsg *); static int devfs_fo_seek(struct file *, off_t, int, off_t *); static __inline int sequential_heuristic(struct uio *, struct file *); extern struct lock devfs_lock; /* * devfs vnode operations for regular files. All vnode ops are MPSAFE. */ struct vop_ops devfs_vnode_norm_vops = { .vop_default = vop_defaultop, .vop_access = devfs_vop_access, .vop_advlock = DEVFS_BADOP, .vop_bmap = DEVFS_BADOP, .vop_close = vop_stdclose, .vop_getattr = devfs_vop_getattr, .vop_inactive = devfs_vop_inactive, .vop_ncreate = DEVFS_BADOP, .vop_nresolve = devfs_vop_nresolve, .vop_nlookupdotdot = devfs_vop_nlookupdotdot, .vop_nlink = DEVFS_BADOP, .vop_nmkdir = devfs_vop_nmkdir, .vop_nmknod = DEVFS_BADOP, .vop_nremove = devfs_vop_nremove, .vop_nrename = DEVFS_BADOP, .vop_nrmdir = devfs_vop_nrmdir, .vop_nsymlink = devfs_vop_nsymlink, .vop_open = vop_stdopen, .vop_pathconf = vop_stdpathconf, .vop_print = devfs_vop_print, .vop_read = DEVFS_BADOP, .vop_readdir = devfs_vop_readdir, .vop_readlink = devfs_vop_readlink, .vop_reallocblks = DEVFS_BADOP, .vop_reclaim = devfs_vop_reclaim, .vop_setattr = devfs_vop_setattr, .vop_write = DEVFS_BADOP, .vop_ioctl = DEVFS_BADOP }; /* * devfs vnode operations for character devices. All vnode ops are MPSAFE. */ struct vop_ops devfs_vnode_dev_vops = { .vop_default = vop_defaultop, .vop_access = devfs_vop_access, .vop_advlock = devfs_spec_advlock, .vop_bmap = devfs_spec_bmap, .vop_close = devfs_spec_close, .vop_freeblks = devfs_spec_freeblks, .vop_fsync = devfs_spec_fsync, .vop_getattr = devfs_vop_getattr, .vop_getpages = devfs_spec_getpages, .vop_inactive = devfs_vop_inactive, .vop_open = devfs_spec_open, .vop_pathconf = vop_stdpathconf, .vop_print = devfs_vop_print, .vop_kqfilter = devfs_spec_kqfilter, .vop_read = devfs_spec_read, .vop_readdir = DEVFS_BADOP, .vop_readlink = DEVFS_BADOP, .vop_reallocblks = DEVFS_BADOP, .vop_reclaim = devfs_vop_reclaim, .vop_setattr = devfs_vop_setattr, .vop_strategy = devfs_spec_strategy, .vop_write = devfs_spec_write, .vop_ioctl = devfs_spec_ioctl }; /* * devfs file pointer operations. All fileops are MPSAFE. */ struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops; struct fileops devfs_dev_fileops = { .fo_read = devfs_fo_read, .fo_write = devfs_fo_write, .fo_ioctl = devfs_fo_ioctl, .fo_kqfilter = devfs_fo_kqfilter, .fo_stat = devfs_fo_stat, .fo_close = devfs_fo_close, .fo_shutdown = nofo_shutdown, .fo_seek = devfs_fo_seek }; /* * These two functions are possibly temporary hacks for devices (aka * the pty code) which want to control the node attributes themselves. * * XXX we may ultimately desire to simply remove the uid/gid/mode * from the node entirely. * * MPSAFE - sorta. Theoretically the overwrite can compete since they * are loading from the same fields. */ static __inline void node_sync_dev_get(struct devfs_node *node) { cdev_t dev; if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) { node->uid = dev->si_uid; node->gid = dev->si_gid; node->mode = dev->si_perms; } } static __inline void node_sync_dev_set(struct devfs_node *node) { cdev_t dev; if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) { dev->si_uid = node->uid; dev->si_gid = node->gid; dev->si_perms = node->mode; } } /* * generic entry point for unsupported operations */ static int devfs_vop_badop(struct vop_generic_args *ap) { return (EIO); } static int devfs_vop_access(struct vop_access_args *ap) { struct devfs_node *node = DEVFS_NODE(ap->a_vp); int error; if (!devfs_node_is_accessible(node)) return ENOENT; node_sync_dev_get(node); error = vop_helper_access(ap, node->uid, node->gid, node->mode, node->flags); return error; } static int devfs_vop_inactive(struct vop_inactive_args *ap) { struct devfs_node *node = DEVFS_NODE(ap->a_vp); if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0) vrecycle(ap->a_vp); return 0; } static int devfs_vop_reclaim(struct vop_reclaim_args *ap) { struct devfs_node *node; struct vnode *vp; int locked; /* * Check if it is locked already. if not, we acquire the devfs lock */ if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) { lockmgr(&devfs_lock, LK_EXCLUSIVE); locked = 1; } else { locked = 0; } /* * Get rid of the devfs_node if it is no longer linked into the * topology. Interlocked by devfs_lock. However, be careful * interposing other operations between cleaning out v_data and * devfs_freep() as the node is only protected by devfs_lock * once the vnode is disassociated. */ vp = ap->a_vp; node = DEVFS_NODE(vp); if (node) { if (node->v_node != vp) { kprintf("NODE->V_NODE MISMATCH VP=%p NODEVP=%p\n", vp, node->v_node); } vp->v_data = NULL; node->v_node = NULL; if ((node->flags & DEVFS_NODE_LINKED) == 0) devfs_freep(node); } v_release_rdev(vp); if (locked) lockmgr(&devfs_lock, LK_RELEASE); /* * v_rdev needs to be properly released using v_release_rdev * Make sure v_data is NULL as well. */ return 0; } static int devfs_vop_readdir(struct vop_readdir_args *ap) { struct devfs_node *dnode = DEVFS_NODE(ap->a_vp); struct devfs_node *node; int cookie_index; int ncookies; int error2; int error; int r; off_t *cookies; off_t saveoff; devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n"); if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX) return (EINVAL); error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); if (error) return (error); if (!devfs_node_is_accessible(dnode)) { vn_unlock(ap->a_vp); return ENOENT; } lockmgr(&devfs_lock, LK_EXCLUSIVE); saveoff = ap->a_uio->uio_offset; if (ap->a_ncookies) { ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */ if (ncookies > 256) ncookies = 256; cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK); cookie_index = 0; } else { ncookies = -1; cookies = NULL; cookie_index = 0; } vfs_timestamp(&dnode->atime); if (saveoff == 0) { r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino, DT_DIR, 1, "."); if (r) goto done; if (cookies) cookies[cookie_index] = saveoff; saveoff++; cookie_index++; if (cookie_index == ncookies) goto done; } if (saveoff == 1) { if (dnode->parent) { r = vop_write_dirent(&error, ap->a_uio, dnode->parent->d_dir.d_ino, DT_DIR, 2, ".."); } else { r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino, DT_DIR, 2, ".."); } if (r) goto done; if (cookies) cookies[cookie_index] = saveoff; saveoff++; cookie_index++; if (cookie_index == ncookies) goto done; } TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { if ((node->flags & DEVFS_HIDDEN) || (node->flags & DEVFS_INVISIBLE)) { continue; } /* * If the node type is a valid devfs alias, then we make * sure that the target isn't hidden. If it is, we don't * show the link in the directory listing. */ if ((node->node_type == Nlink) && (node->link_target != NULL) && (node->link_target->flags & DEVFS_HIDDEN)) continue; if (node->cookie < saveoff) continue; saveoff = node->cookie; error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino, node->d_dir.d_type, node->d_dir.d_namlen, node->d_dir.d_name); if (error2) break; saveoff++; if (cookies) cookies[cookie_index] = node->cookie; ++cookie_index; if (cookie_index == ncookies) break; } done: lockmgr(&devfs_lock, LK_RELEASE); vn_unlock(ap->a_vp); ap->a_uio->uio_offset = saveoff; if (error && cookie_index == 0) { if (cookies) { kfree(cookies, M_TEMP); *ap->a_ncookies = 0; *ap->a_cookies = NULL; } } else { if (cookies) { *ap->a_ncookies = cookie_index; *ap->a_cookies = cookies; } } return (error); } static int devfs_vop_nresolve(struct vop_nresolve_args *ap) { struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); struct devfs_node *node, *found = NULL; struct namecache *ncp; struct vnode *vp = NULL; int error = 0; int len; int depth; ncp = ap->a_nch->ncp; len = ncp->nc_nlen; if (!devfs_node_is_accessible(dnode)) return ENOENT; lockmgr(&devfs_lock, LK_EXCLUSIVE); if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) { error = ENOENT; cache_setvp(ap->a_nch, NULL); goto out; } TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { if (len == node->d_dir.d_namlen) { if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) { found = node; break; } } } if (found) { depth = 0; while ((found->node_type == Nlink) && (found->link_target)) { if (depth >= 8) { devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8"); break; } found = found->link_target; ++depth; } if (!(found->flags & DEVFS_HIDDEN)) devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found); } if (vp == NULL) { error = ENOENT; cache_setvp(ap->a_nch, NULL); goto out; } KKASSERT(vp); vn_unlock(vp); cache_setvp(ap->a_nch, vp); vrele(vp); out: lockmgr(&devfs_lock, LK_RELEASE); return error; } static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) { struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); *ap->a_vpp = NULL; if (!devfs_node_is_accessible(dnode)) return ENOENT; lockmgr(&devfs_lock, LK_EXCLUSIVE); if (dnode->parent != NULL) { devfs_allocv(ap->a_vpp, dnode->parent); vn_unlock(*ap->a_vpp); } lockmgr(&devfs_lock, LK_RELEASE); return ((*ap->a_vpp == NULL) ? ENOENT : 0); } /* * getattr() - Does not need a lock since the vp is refd */ static int devfs_vop_getattr(struct vop_getattr_args *ap) { struct devfs_node *node = DEVFS_NODE(ap->a_vp); struct vattr *vap = ap->a_vap; struct partinfo pinfo; int error = 0; #if 0 if (!devfs_node_is_accessible(node)) return ENOENT; #endif /* * XXX This is a temporary hack to prevent crashes when the device is * being destroyed (and so the underlying node will be gone) while * a userland program is blocked in a read(). */ if (node == NULL) return EIO; node_sync_dev_get(node); /* start by zeroing out the attributes */ VATTR_NULL(vap); /* next do all the common fields */ vap->va_type = ap->a_vp->v_type; vap->va_mode = node->mode; vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ; vap->va_flags = 0; vap->va_blocksize = DEV_BSIZE; vap->va_bytes = vap->va_size = 0; vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; vap->va_atime = node->atime; vap->va_mtime = node->mtime; vap->va_ctime = node->ctime; vap->va_nlink = 1; /* number of references to file */ vap->va_uid = node->uid; vap->va_gid = node->gid; vap->va_rmajor = 0; vap->va_rminor = 0; if ((node->node_type == Ndev) && node->d_dev) { reference_dev(node->d_dev); vap->va_rminor = node->d_dev->si_uminor; release_dev(node->d_dev); } /* For a softlink the va_size is the length of the softlink */ if (node->symlink_name != 0) { vap->va_bytes = vap->va_size = node->symlink_namelen; } /* * For a disk-type device, va_size is the size of the underlying * device, so that lseek() works properly. */ if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) { bzero(&pinfo, sizeof(pinfo)); error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo, 0, proc0.p_ucred, NULL, NULL); if ((error == 0) && (pinfo.media_blksize != 0)) { vap->va_size = pinfo.media_size; } else { vap->va_size = 0; error = 0; } } return (error); } static int devfs_vop_setattr(struct vop_setattr_args *ap) { struct devfs_node *node = DEVFS_NODE(ap->a_vp); struct vattr *vap; uid_t cur_uid; gid_t cur_gid; mode_t cur_mode; int error = 0; if (!devfs_node_is_accessible(node)) return ENOENT; node_sync_dev_get(node); vap = ap->a_vap; if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) { cur_uid = node->uid; cur_gid = node->gid; cur_mode = node->mode; error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid, ap->a_cred, &cur_uid, &cur_gid, &cur_mode); if (error) goto out; if (node->uid != cur_uid || node->gid != cur_gid) { node->uid = cur_uid; node->gid = cur_gid; node->mode = cur_mode; } } if (vap->va_mode != (mode_t)VNOVAL) { cur_mode = node->mode; error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred, node->uid, node->gid, &cur_mode); if (error == 0 && node->mode != cur_mode) { node->mode = cur_mode; } } out: node_sync_dev_set(node); vfs_timestamp(&node->ctime); return error; } static int devfs_vop_readlink(struct vop_readlink_args *ap) { struct devfs_node *node = DEVFS_NODE(ap->a_vp); int ret; if (!devfs_node_is_accessible(node)) return ENOENT; lockmgr(&devfs_lock, LK_SHARED); ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio); lockmgr(&devfs_lock, LK_RELEASE); return ret; } static int devfs_vop_print(struct vop_print_args *ap) { return (0); } static int devfs_vop_nmkdir(struct vop_nmkdir_args *ap) { struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); struct devfs_node *node; if (!devfs_node_is_accessible(dnode)) return ENOENT; if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) goto out; lockmgr(&devfs_lock, LK_EXCLUSIVE); devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Ndir, ap->a_nch->ncp->nc_name, dnode, NULL); if (*ap->a_vpp) { node = DEVFS_NODE(*ap->a_vpp); node->flags |= DEVFS_USER_CREATED; cache_setunresolved(ap->a_nch); cache_setvp(ap->a_nch, *ap->a_vpp); } lockmgr(&devfs_lock, LK_RELEASE); out: return ((*ap->a_vpp == NULL) ? ENOTDIR : 0); } static int devfs_vop_nsymlink(struct vop_nsymlink_args *ap) { struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); struct devfs_node *node; size_t targetlen; if (!devfs_node_is_accessible(dnode)) return ENOENT; ap->a_vap->va_type = VLNK; if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) goto out; lockmgr(&devfs_lock, LK_EXCLUSIVE); devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Nlink, ap->a_nch->ncp->nc_name, dnode, NULL); targetlen = strlen(ap->a_target); if (*ap->a_vpp) { node = DEVFS_NODE(*ap->a_vpp); node->flags |= DEVFS_USER_CREATED; node->symlink_namelen = targetlen; node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK); memcpy(node->symlink_name, ap->a_target, targetlen); node->symlink_name[targetlen] = '\0'; cache_setunresolved(ap->a_nch); cache_setvp(ap->a_nch, *ap->a_vpp); } lockmgr(&devfs_lock, LK_RELEASE); out: return ((*ap->a_vpp == NULL) ? ENOTDIR : 0); } static int devfs_vop_nrmdir(struct vop_nrmdir_args *ap) { struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); struct devfs_node *node; struct namecache *ncp; int error = ENOENT; ncp = ap->a_nch->ncp; if (!devfs_node_is_accessible(dnode)) return ENOENT; lockmgr(&devfs_lock, LK_EXCLUSIVE); if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) goto out; TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { if (ncp->nc_nlen != node->d_dir.d_namlen) continue; if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen)) continue; /* * only allow removal of user created dirs */ if ((node->flags & DEVFS_USER_CREATED) == 0) { error = EPERM; goto out; } else if (node->node_type != Ndir) { error = ENOTDIR; goto out; } else if (node->nchildren > 2) { error = ENOTEMPTY; goto out; } else { if (node->v_node) cache_inval_vp(node->v_node, CINV_DESTROY); devfs_unlinkp(node); error = 0; break; } } cache_unlink(ap->a_nch); out: lockmgr(&devfs_lock, LK_RELEASE); return error; } static int devfs_vop_nremove(struct vop_nremove_args *ap) { struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp); struct devfs_node *node; struct namecache *ncp; int error = ENOENT; ncp = ap->a_nch->ncp; if (!devfs_node_is_accessible(dnode)) return ENOENT; lockmgr(&devfs_lock, LK_EXCLUSIVE); if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) goto out; TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) { if (ncp->nc_nlen != node->d_dir.d_namlen) continue; if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen)) continue; /* * only allow removal of user created stuff (e.g. symlinks) */ if ((node->flags & DEVFS_USER_CREATED) == 0) { error = EPERM; goto out; } else if (node->node_type == Ndir) { error = EISDIR; goto out; } else { if (node->v_node) cache_inval_vp(node->v_node, CINV_DESTROY); devfs_unlinkp(node); error = 0; break; } } cache_unlink(ap->a_nch); out: lockmgr(&devfs_lock, LK_RELEASE); return error; } static int devfs_spec_open(struct vop_open_args *ap) { struct vnode *vp = ap->a_vp; struct vnode *orig_vp = NULL; struct devfs_node *node = DEVFS_NODE(vp); struct devfs_node *newnode; cdev_t dev, ndev = NULL; int error = 0; if (node) { if (node->d_dev == NULL) return ENXIO; if (!devfs_node_is_accessible(node)) return ENOENT; } if ((dev = vp->v_rdev) == NULL) return ENXIO; /* * Simple devices that don't care. Retain the shared lock. */ if (dev_dflags(dev) & D_QUICK) { vn_unlock(vp); error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred, ap->a_fpp, vp); vn_lock(vp, LK_SHARED | LK_RETRY); if (error) return error; vop_stdopen(ap); goto skip; } /* * Slow code */ vn_lock(vp, LK_UPGRADE | LK_RETRY); if (node && ap->a_fpp) { int exists; devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n"); lockmgr(&devfs_lock, LK_SHARED); ndev = devfs_clone(dev, node->d_dir.d_name, node->d_dir.d_namlen, ap->a_mode, ap->a_cred); if (ndev != NULL) { lockmgr(&devfs_lock, LK_RELEASE); lockmgr(&devfs_lock, LK_EXCLUSIVE); newnode = devfs_create_device_node( DEVFS_MNTDATA(vp->v_mount)->root_node, ndev, &exists, NULL, NULL); /* XXX: possibly destroy device if this happens */ if (newnode != NULL) { dev = ndev; if (exists == 0) devfs_link_dev(dev); devfs_debug(DEVFS_DEBUG_DEBUG, "parent here is: %s, node is: |%s|\n", ((node->parent->node_type == Nroot) ? "ROOT!" : node->parent->d_dir.d_name), newnode->d_dir.d_name); devfs_debug(DEVFS_DEBUG_DEBUG, "test: %s\n", ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name); /* * orig_vp is set to the original vp if we * cloned. */ /* node->flags |= DEVFS_CLONED; */ devfs_allocv(&vp, newnode); orig_vp = ap->a_vp; ap->a_vp = vp; } } lockmgr(&devfs_lock, LK_RELEASE); /* * Synchronize devfs here to make sure that, if the cloned * device creates other device nodes in addition to the * cloned one, all of them are created by the time we return * from opening the cloned one. */ if (ndev) devfs_config(); } devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open() called on %s! \n", dev->si_name); /* * Make this field valid before any I/O in ->d_open * * NOTE: Shared vnode lock probably held, but its ok as long * as assignments are consistent. */ if (!dev->si_iosize_max) /* XXX: old DFLTPHYS == 64KB dependency */ dev->si_iosize_max = min(MAXPHYS,64*1024); if (dev_dflags(dev) & D_TTY) vsetflags(vp, VISTTY); /* * Open the underlying device. * * NOTE: If the dev open returns EALREADY it has completed the open * operation and is returning a fully initialized *a->a_fpp * (which it may also have replaced). This includes issuing * any necessary VOP_OPEN(). * * Also, the returned ap->a_fpp might not be DTYPE_VNODE and * if it is might not be using the vp we supplied to it. */ vn_unlock(vp); error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred, ap->a_fpp, vp); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); if (__predict_false(error == EALREADY)) { if (orig_vp) vput(vp); return 0; } /* * Clean up any cloned vp if we error out. */ if (__predict_false(error != 0)) { if (orig_vp) { vput(vp); ap->a_vp = orig_vp; /* orig_vp = NULL; */ } return error; } /* * This checks if the disk device is going to be opened for writing. * It will be only allowed in the cases where securelevel permits it * and it's not mounted R/W. */ if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) && (ap->a_cred != FSCRED)) { /* Very secure mode. No open for writing allowed */ if (securelevel >= 2) return EPERM; /* * If it is mounted R/W, do not allow to open for writing. * In the case it's mounted read-only but securelevel * is >= 1, then do not allow opening for writing either. */ if (vfs_mountedon(vp)) { if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY)) return EBUSY; else if (securelevel >= 1) return EPERM; } } /* * NOTE: vnode is still locked shared. t_stop assignment should * remain consistent so we should be ok. */ if (dev_dflags(dev) & D_TTY) { if (dev->si_tty) { struct tty *tp; tp = dev->si_tty; if (!tp->t_stop) { devfs_debug(DEVFS_DEBUG_DEBUG, "devfs: no t_stop\n"); tp->t_stop = nottystop; } } } /* * NOTE: vnode is still locked shared. assignments should * remain consistent so we should be ok. However, * upgrade to exclusive if we need a VM object. */ if (vn_isdisk(vp, NULL)) { if (!dev->si_bsize_phys) dev->si_bsize_phys = DEV_BSIZE; vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1); } vop_stdopen(ap); #if 0 if (node) vfs_timestamp(&node->atime); #endif /* * If we replaced the vp the vop_stdopen() call will have loaded * it into fp->f_data and vref()d the vp, giving us two refs. So * instead of just unlocking it here we have to vput() it. */ if (orig_vp) vput(vp); /* Ugly pty magic, to make pty devices appear once they are opened */ if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) { if (node->flags & DEVFS_INVISIBLE) node->flags &= ~DEVFS_INVISIBLE; } skip: if (ap->a_fpp) { struct file *fp = *ap->a_fpp; KKASSERT(fp->f_type == DTYPE_VNODE); KKASSERT((fp->f_flag & FMASK) == (ap->a_mode & FMASK)); fp->f_ops = &devfs_dev_fileops; KKASSERT(fp->f_data == (void *)vp); } return 0; } static int devfs_spec_close(struct vop_close_args *ap) { struct devfs_node *node; struct proc *p = curproc; struct vnode *vp = ap->a_vp; cdev_t dev = vp->v_rdev; int error = 0; int needrelock; int opencount; /* * Devices flagged D_QUICK require no special handling. */ if (dev && dev_dflags(dev) & D_QUICK) { opencount = vp->v_opencount; if (opencount <= 1) opencount = count_dev(dev); /* XXX NOT SMP SAFE */ if (((vp->v_flag & VRECLAIMED) || (dev_dflags(dev) & D_TRACKCLOSE) || (opencount == 1))) { vn_unlock(vp); error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp); vn_lock(vp, LK_SHARED | LK_RETRY); } goto skip; } /* * We do special tests on the opencount so unfortunately we need * an exclusive lock. */ vn_lock(vp, LK_UPGRADE | LK_RETRY); if (dev) devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() called on %s! \n", dev->si_name); else devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() called, null vode!\n"); /* * A couple of hacks for devices and tty devices. The * vnode ref count cannot be used to figure out the * last close, but we can use v_opencount now that * revoke works properly. * * Detect the last close on a controlling terminal and clear * the session (half-close). * * XXX opencount is not SMP safe. The vnode is locked but there * may be multiple vnodes referencing the same device. */ if (dev) { /* * NOTE: Try to avoid global tokens when testing opencount * XXX hack, fixme. needs a struct lock and opencount in * struct cdev itself. */ reference_dev(dev); opencount = vp->v_opencount; if (opencount <= 1) opencount = count_dev(dev); /* XXX NOT SMP SAFE */ } else { opencount = 0; } if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) { p->p_session->s_ttyvp = NULL; vrele(vp); } /* * Vnodes can be opened and closed multiple times. Do not really * close the device unless (1) it is being closed forcibly, * (2) the device wants to track closes, or (3) this is the last * vnode doing its last close on the device. * * XXX the VXLOCK (force close) case can leave vnodes referencing * a closed device. This might not occur now that our revoke is * fixed. */ devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n"); if (dev && ((vp->v_flag & VRECLAIMED) || (dev_dflags(dev) & D_TRACKCLOSE) || (opencount == 1))) { /* * Ugly pty magic, to make pty devices disappear again once * they are closed. */ node = DEVFS_NODE(ap->a_vp); if (node && (node->flags & DEVFS_PTY)) node->flags |= DEVFS_INVISIBLE; /* * Unlock around dev_dclose(), unless the vnode is * undergoing a vgone/reclaim (during umount). */ needrelock = 0; if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) { needrelock = 1; vn_unlock(vp); } /* * WARNING! If the device destroys itself the devfs node * can disappear here. * * WARNING! vn_lock() will fail if the vp is in a VRECLAIM, * which can occur during umount. */ error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp); /* node is now stale */ if (needrelock) { if (vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM) != 0) { panic("devfs_spec_close: vnode %p " "unexpectedly could not be relocked", vp); } } } else { error = 0; } devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n"); /* * Track the actual opens and closes on the vnode. The last close * disassociates the rdev. If the rdev is already disassociated or * the opencount is already 0, the vnode might have been revoked * and no further opencount tracking occurs. */ if (dev) release_dev(dev); skip: if (vp->v_opencount > 0) vop_stdclose(ap); return(error); } static int devfs_fo_close(struct file *fp) { struct vnode *vp = (struct vnode *)fp->f_data; int error; fp->f_ops = &badfileops; error = vn_close(vp, fp->f_flag, fp); devfs_clear_cdevpriv(fp); return (error); } /* * Device-optimized file table vnode read routine. * * This bypasses the VOP table and talks directly to the device. Most * filesystems just route to specfs and can make this optimization. */ static int devfs_fo_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags) { struct devfs_node *node; struct vnode *vp; int ioflag; int error; cdev_t dev; KASSERT(uio->uio_td == curthread, ("uio_td %p is not td %p", uio->uio_td, curthread)); if (uio->uio_resid == 0) return 0; vp = (struct vnode *)fp->f_data; if (vp == NULL || vp->v_type == VBAD) return EBADF; node = DEVFS_NODE(vp); if ((dev = vp->v_rdev) == NULL) return EBADF; reference_dev(dev); if ((flags & O_FOFFSET) == 0) uio->uio_offset = fp->f_offset; ioflag = 0; if (flags & O_FBLOCKING) { /* ioflag &= ~IO_NDELAY; */ } else if (flags & O_FNONBLOCKING) { ioflag |= IO_NDELAY; } else if (fp->f_flag & FNONBLOCK) { ioflag |= IO_NDELAY; } if (fp->f_flag & O_DIRECT) { ioflag |= IO_DIRECT; } ioflag |= sequential_heuristic(uio, fp); error = dev_dread(dev, uio, ioflag, fp); release_dev(dev); if (node) vfs_timestamp(&node->atime); if ((flags & O_FOFFSET) == 0) fp->f_offset = uio->uio_offset; fp->f_nextoff = uio->uio_offset; return (error); } static int devfs_fo_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags) { struct devfs_node *node; struct vnode *vp; int ioflag; int error; cdev_t dev; KASSERT(uio->uio_td == curthread, ("uio_td %p is not p %p", uio->uio_td, curthread)); vp = (struct vnode *)fp->f_data; if (vp == NULL || vp->v_type == VBAD) return EBADF; node = DEVFS_NODE(vp); if (vp->v_type == VREG) bwillwrite(uio->uio_resid); vp = (struct vnode *)fp->f_data; if ((dev = vp->v_rdev) == NULL) return EBADF; reference_dev(dev); if ((flags & O_FOFFSET) == 0) uio->uio_offset = fp->f_offset; ioflag = IO_UNIT; if (vp->v_type == VREG && ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) { ioflag |= IO_APPEND; } if (flags & O_FBLOCKING) { /* ioflag &= ~IO_NDELAY; */ } else if (flags & O_FNONBLOCKING) { ioflag |= IO_NDELAY; } else if (fp->f_flag & FNONBLOCK) { ioflag |= IO_NDELAY; } if (fp->f_flag & O_DIRECT) { ioflag |= IO_DIRECT; } if (flags & O_FASYNCWRITE) { /* ioflag &= ~IO_SYNC; */ } else if (flags & O_FSYNCWRITE) { ioflag |= IO_SYNC; } else if (fp->f_flag & O_FSYNC) { ioflag |= IO_SYNC; } if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)) ioflag |= IO_SYNC; ioflag |= sequential_heuristic(uio, fp); error = dev_dwrite(dev, uio, ioflag, fp); release_dev(dev); if (node) { vfs_timestamp(&node->atime); vfs_timestamp(&node->mtime); } if ((flags & O_FOFFSET) == 0) fp->f_offset = uio->uio_offset; fp->f_nextoff = uio->uio_offset; return (error); } static int devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred) { struct vnode *vp; struct vattr vattr; struct vattr *vap; u_short mode; cdev_t dev; int error; vp = (struct vnode *)fp->f_data; if (vp == NULL || vp->v_type == VBAD) return EBADF; error = vn_stat(vp, sb, cred); if (error) return (error); vap = &vattr; error = VOP_GETATTR(vp, vap); if (error) return (error); /* * Zero the spare stat fields */ sb->st_lspare = 0; sb->st_qspare2 = 0; /* * Copy from vattr table ... or not in case it's a cloned device */ if (vap->va_fsid != VNOVAL) sb->st_dev = vap->va_fsid; else sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; sb->st_ino = vap->va_fileid; mode = vap->va_mode; mode |= S_IFCHR; sb->st_mode = mode; if (vap->va_nlink > (nlink_t)-1) sb->st_nlink = (nlink_t)-1; else sb->st_nlink = vap->va_nlink; sb->st_uid = vap->va_uid; sb->st_gid = vap->va_gid; sb->st_rdev = devid_from_dev(DEVFS_NODE(vp)->d_dev); sb->st_size = vap->va_bytes; sb->st_atimespec = vap->va_atime; sb->st_mtimespec = vap->va_mtime; sb->st_ctimespec = vap->va_ctime; /* * A VCHR and VBLK device may track the last access and last modified * time independantly of the filesystem. This is particularly true * because device read and write calls may bypass the filesystem. */ if (vp->v_type == VCHR || vp->v_type == VBLK) { dev = vp->v_rdev; if (dev != NULL) { if (dev->si_lastread) { sb->st_atimespec.tv_sec = time_second + (dev->si_lastread - time_uptime); sb->st_atimespec.tv_nsec = 0; } if (dev->si_lastwrite) { sb->st_mtimespec.tv_sec = time_second + (dev->si_lastwrite - time_uptime); sb->st_mtimespec.tv_nsec = 0; } } } /* * According to www.opengroup.org, the meaning of st_blksize is * "a filesystem-specific preferred I/O block size for this * object. In some filesystem types, this may vary from file * to file" * Default to PAGE_SIZE after much discussion. */ sb->st_blksize = PAGE_SIZE; sb->st_flags = vap->va_flags; error = caps_priv_check(cred, SYSCAP_NOVFS_GENERATION); if (error) sb->st_gen = 0; else sb->st_gen = (u_int32_t)vap->va_gen; sb->st_blocks = vap->va_bytes / S_BLKSIZE; /* * This is for ABI compatibility <= 5.7 (for ABI change made in * 5.7 master). */ sb->__old_st_blksize = sb->st_blksize; return (0); } static int devfs_fo_kqfilter(struct file *fp, struct knote *kn) { struct vnode *vp; int error; cdev_t dev; vp = (struct vnode *)fp->f_data; if (vp == NULL || vp->v_type == VBAD) { error = EBADF; goto done; } if ((dev = vp->v_rdev) == NULL) { error = EBADF; goto done; } reference_dev(dev); error = dev_dkqfilter(dev, kn, fp); release_dev(dev); done: return (error); } static int devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred, struct sysmsg *msg) { #if 0 struct devfs_node *node; #endif struct vnode *vp; struct vnode *ovp; cdev_t dev; int error; struct fiodname_args *name_args; size_t namlen; const char *name; vp = ((struct vnode *)fp->f_data); if ((dev = vp->v_rdev) == NULL) return EBADF; /* device was revoked */ reference_dev(dev); #if 0 node = DEVFS_NODE(vp); #endif devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() called! for dev %s\n", dev->si_name); if (com == FIODTYPE) { *(int *)data = dev_dflags(dev) & D_TYPEMASK; error = 0; goto out; } else if (com == FIODNAME) { name_args = (struct fiodname_args *)data; name = dev->si_name; namlen = strlen(name) + 1; devfs_debug(DEVFS_DEBUG_DEBUG, "ioctl, got: FIODNAME for %s\n", name); if (namlen <= name_args->len) error = copyout(dev->si_name, name_args->name, namlen); else error = EINVAL; devfs_debug(DEVFS_DEBUG_DEBUG, "ioctl stuff: error: %d\n", error); goto out; } error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp); #if 0 if (node) { vfs_timestamp(&node->atime); vfs_timestamp(&node->mtime); } #endif if (com == TIOCSCTTY) { devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl: got TIOCSCTTY on %s\n", dev->si_name); } if (error == 0 && com == TIOCSCTTY) { struct proc *p = curthread->td_proc; struct session *sess; devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n", dev->si_name); if (p == NULL) { error = ENOTTY; goto out; } sess = p->p_session; /* * Do nothing if reassigning same control tty */ if (sess->s_ttyvp == vp) { error = 0; goto out; } /* * Get rid of reference to old control tty */ ovp = sess->s_ttyvp; vref(vp); sess->s_ttyvp = vp; if (ovp) vrele(ovp); } out: release_dev(dev); devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n"); return (error); } int devfs_fo_seek(struct file *fp, off_t offset, int whence, off_t *res) { /* * NOTE: vnode_fileops uses exact same code */ struct vnode *vp; struct vattr_lite lva; off_t new_offset; int error; vp = (struct vnode *)fp->f_data; switch (whence) { case L_INCR: spin_lock(&fp->f_spin); new_offset = fp->f_offset + offset; error = 0; break; case L_XTND: error = VOP_GETATTR_LITE(vp, &lva); spin_lock(&fp->f_spin); new_offset = offset + lva.va_size; break; case L_SET: new_offset = offset; error = 0; spin_lock(&fp->f_spin); break; default: new_offset = 0; error = EINVAL; spin_lock(&fp->f_spin); break; } /* * Validate the seek position. Negative offsets are not allowed * for regular files or directories. * * Normally we would also not want to allow negative offsets for * character and block-special devices. However kvm addresses * on 64 bit architectures might appear to be negative and must * be allowed. */ if (error == 0) { if (new_offset < 0 && (vp->v_type == VREG || vp->v_type == VDIR)) { error = EINVAL; } else { fp->f_offset = new_offset; } } *res = fp->f_offset; spin_unlock(&fp->f_spin); return (error); } static int devfs_spec_fsync(struct vop_fsync_args *ap) { struct vnode *vp = ap->a_vp; int error; if (!vn_isdisk(vp, NULL)) return (0); /* * Flush all dirty buffers associated with a block device. */ error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL); return (error); } static int devfs_spec_read(struct vop_read_args *ap) { struct devfs_node *node; struct vnode *vp; struct uio *uio; cdev_t dev; int error; vp = ap->a_vp; dev = vp->v_rdev; uio = ap->a_uio; node = DEVFS_NODE(vp); if (dev == NULL) /* device was revoked */ return (EBADF); if (uio->uio_resid == 0) return (0); vn_unlock(vp); error = dev_dread(dev, uio, ap->a_ioflag, NULL); vn_lock(vp, LK_SHARED | LK_RETRY); if (node) vfs_timestamp(&node->atime); return (error); } /* * Vnode op for write * * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, * struct ucred *a_cred) */ static int devfs_spec_write(struct vop_write_args *ap) { struct devfs_node *node; struct vnode *vp; struct uio *uio; cdev_t dev; int error; vp = ap->a_vp; dev = vp->v_rdev; uio = ap->a_uio; node = DEVFS_NODE(vp); KKASSERT(uio->uio_segflg != UIO_NOCOPY); if (dev == NULL) /* device was revoked */ return (EBADF); vn_unlock(vp); error = dev_dwrite(dev, uio, ap->a_ioflag, NULL); vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); if (node) { vfs_timestamp(&node->atime); vfs_timestamp(&node->mtime); } return (error); } /* * Device ioctl operation. * * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, * int a_fflag, struct ucred *a_cred, struct sysmsg *msg) */ static int devfs_spec_ioctl(struct vop_ioctl_args *ap) { struct vnode *vp = ap->a_vp; #if 0 struct devfs_node *node; #endif cdev_t dev; if ((dev = vp->v_rdev) == NULL) return (EBADF); /* device was revoked */ #if 0 node = DEVFS_NODE(vp); if (node) { vfs_timestamp(&node->atime); vfs_timestamp(&node->mtime); } #endif return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag, ap->a_cred, ap->a_sysmsg, NULL)); } /* * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn) */ /* ARGSUSED */ static int devfs_spec_kqfilter(struct vop_kqfilter_args *ap) { struct vnode *vp = ap->a_vp; #if 0 struct devfs_node *node; #endif cdev_t dev; if ((dev = vp->v_rdev) == NULL) return (EBADF); /* device was revoked (EBADF) */ #if 0 node = DEVFS_NODE(vp); if (node) vfs_timestamp(&node->atime); #endif return (dev_dkqfilter(dev, ap->a_kn, NULL)); } /* * Convert a vnode strategy call into a device strategy call. Vnode strategy * calls are not limited to device DMA limits so we have to deal with the * case. * * spec_strategy(struct vnode *a_vp, struct bio *a_bio) */ static int devfs_spec_strategy(struct vop_strategy_args *ap) { struct bio *bio = ap->a_bio; struct buf *bp = bio->bio_buf; struct buf *nbp; struct vnode *vp; struct mount *mp; int chunksize; int maxiosize; if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL) buf_start(bp); /* * Collect statistics on synchronous and asynchronous read * and write counts for disks that have associated filesystems. */ vp = ap->a_vp; KKASSERT(vp->v_rdev != NULL); /* XXX */ if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) { if (bp->b_cmd == BUF_CMD_READ) { if (bp->b_flags & BIO_SYNC) mp->mnt_stat.f_syncreads++; else mp->mnt_stat.f_asyncreads++; } else { if (bp->b_flags & BIO_SYNC) mp->mnt_stat.f_syncwrites++; else mp->mnt_stat.f_asyncwrites++; } } /* * Device iosize limitations only apply to read and write. Shortcut * the I/O if it fits. */ if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) { devfs_debug(DEVFS_DEBUG_DEBUG, "%s: si_iosize_max not set!\n", dev_dname(vp->v_rdev)); maxiosize = MAXPHYS; } #if SPEC_CHAIN_DEBUG & 2 maxiosize = 4096; #endif if (bp->b_bcount <= maxiosize || (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) { dev_dstrategy_chain(vp->v_rdev, bio); return (0); } /* * Clone the buffer and set up an I/O chain to chunk up the I/O. */ nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO); initbufbio(nbp); buf_dep_init(nbp); BUF_LOCK(nbp, LK_EXCLUSIVE); BUF_KERNPROC(nbp); nbp->b_vp = vp; nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP); nbp->b_cpumask = bp->b_cpumask; nbp->b_data = bp->b_data; nbp->b_bio1.bio_done = devfs_spec_strategy_done; nbp->b_bio1.bio_offset = bio->bio_offset; nbp->b_bio1.bio_caller_info1.ptr = bio; /* * Start the first transfer */ if (vn_isdisk(vp, NULL)) chunksize = vp->v_rdev->si_bsize_phys; else chunksize = DEV_BSIZE; chunksize = rounddown(maxiosize, chunksize); #if SPEC_CHAIN_DEBUG & 1 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy chained I/O chunksize=%d\n", chunksize); #endif nbp->b_cmd = bp->b_cmd; nbp->b_bcount = chunksize; nbp->b_bufsize = chunksize; /* used to detect a short I/O */ nbp->b_bio1.bio_caller_info2.index = chunksize; #if SPEC_CHAIN_DEBUG & 1 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p offset %d/%d bcount %d\n", bp, 0, bp->b_bcount, nbp->b_bcount); #endif dev_dstrategy(vp->v_rdev, &nbp->b_bio1); if (DEVFS_NODE(vp)) { vfs_timestamp(&DEVFS_NODE(vp)->atime); vfs_timestamp(&DEVFS_NODE(vp)->mtime); } return (0); } /* * Chunked up transfer completion routine - chain transfers until done * * NOTE: MPSAFE callback. */ static void devfs_spec_strategy_done(struct bio *nbio) { struct buf *nbp = nbio->bio_buf; struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */ struct buf *bp = bio->bio_buf; /* original bp */ int chunksize = nbio->bio_caller_info2.index; /* chunking */ int boffset = nbp->b_data - bp->b_data; if (nbp->b_flags & B_ERROR) { /* * An error terminates the chain, propogate the error back * to the original bp */ bp->b_flags |= B_ERROR; bp->b_error = nbp->b_error; bp->b_resid = bp->b_bcount - boffset + (nbp->b_bcount - nbp->b_resid); #if SPEC_CHAIN_DEBUG & 1 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p error %d bcount %d/%d\n", bp, bp->b_error, bp->b_bcount, bp->b_bcount - bp->b_resid); #endif } else if (nbp->b_resid) { /* * A short read or write terminates the chain */ bp->b_error = nbp->b_error; bp->b_resid = bp->b_bcount - boffset + (nbp->b_bcount - nbp->b_resid); #if SPEC_CHAIN_DEBUG & 1 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p short read(1) " "bcount %d/%d\n", bp, bp->b_bcount - bp->b_resid, bp->b_bcount); #endif } else if (nbp->b_bcount != nbp->b_bufsize) { /* * A short read or write can also occur by truncating b_bcount */ #if SPEC_CHAIN_DEBUG & 1 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p short read(2) " "bcount %d/%d\n", bp, nbp->b_bcount + boffset, bp->b_bcount); #endif bp->b_error = 0; bp->b_bcount = nbp->b_bcount + boffset; bp->b_resid = nbp->b_resid; } else if (nbp->b_bcount + boffset == bp->b_bcount) { /* * No more data terminates the chain */ #if SPEC_CHAIN_DEBUG & 1 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p finished bcount %d\n", bp, bp->b_bcount); #endif bp->b_error = 0; bp->b_resid = 0; } else { /* * Continue the chain */ boffset += nbp->b_bcount; nbp->b_data = bp->b_data + boffset; nbp->b_bcount = bp->b_bcount - boffset; if (nbp->b_bcount > chunksize) nbp->b_bcount = chunksize; nbp->b_bio1.bio_done = devfs_spec_strategy_done; nbp->b_bio1.bio_offset = bio->bio_offset + boffset; #if SPEC_CHAIN_DEBUG & 1 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p offset %d/%d bcount %d\n", bp, boffset, bp->b_bcount, nbp->b_bcount); #endif dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1); return; } /* * Fall through to here on termination. biodone(bp) and * clean up and free nbp. */ biodone(bio); BUF_UNLOCK(nbp); uninitbufbio(nbp); kfree(nbp, M_DEVBUF); } /* * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length) */ static int devfs_spec_freeblks(struct vop_freeblks_args *ap) { struct buf *bp; /* * Must be a synchronous operation */ KKASSERT(ap->a_vp->v_rdev != NULL); if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0) return (0); bp = getpbuf(NULL); bp->b_cmd = BUF_CMD_FREEBLKS; bp->b_bio1.bio_flags |= BIO_SYNC; bp->b_bio1.bio_offset = ap->a_offset; bp->b_bio1.bio_done = biodone_sync; bp->b_bcount = ap->a_length; dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1); biowait(&bp->b_bio1, "TRIM"); relpbuf(bp, NULL); return (0); } /* * Implement degenerate case where the block requested is the block * returned, and assume that the entire device is contiguous in regards * to the contiguous block range (runp and runb). * * spec_bmap(struct vnode *a_vp, off_t a_loffset, * off_t *a_doffsetp, int *a_runp, int *a_runb) */ static int devfs_spec_bmap(struct vop_bmap_args *ap) { if (ap->a_doffsetp != NULL) *ap->a_doffsetp = ap->a_loffset; if (ap->a_runp != NULL) *ap->a_runp = MAXBSIZE; if (ap->a_runb != NULL) { if (ap->a_loffset < MAXBSIZE) *ap->a_runb = (int)ap->a_loffset; else *ap->a_runb = MAXBSIZE; } return (0); } /* * Special device advisory byte-level locks. * * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, * struct flock *a_fl, int a_flags) */ /* ARGSUSED */ static int devfs_spec_advlock(struct vop_advlock_args *ap) { return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP); } /* * NOTE: MPSAFE callback. */ static void devfs_spec_getpages_iodone(struct bio *bio) { bio->bio_buf->b_cmd = BUF_CMD_DONE; wakeup(bio->bio_buf); } /* * spec_getpages() - get pages associated with device vnode. * * Note that spec_read and spec_write do not use the buffer cache, so we * must fully implement getpages here. */ static int devfs_spec_getpages(struct vop_getpages_args *ap) { vm_offset_t kva; int error; int i, pcount, size; struct buf *bp; vm_page_t m; vm_ooffset_t offset; int toff, nextoff, nread; struct vnode *vp = ap->a_vp; int blksiz; int gotreqpage; error = 0; pcount = round_page(ap->a_count) / PAGE_SIZE; /* * Calculate the offset of the transfer and do sanity check. */ offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset; /* * Round up physical size for real devices. We cannot round using * v_mount's block size data because v_mount has nothing to do with * the device. i.e. it's usually '/dev'. We need the physical block * size for the device itself. * * We can't use v_rdev->si_mountpoint because it only exists when the * block device is mounted. However, we can use v_rdev. */ if (vn_isdisk(vp, NULL)) blksiz = vp->v_rdev->si_bsize_phys; else blksiz = DEV_BSIZE; size = roundup2(ap->a_count, blksiz); bp = getpbuf_kva(NULL); kva = (vm_offset_t)bp->b_data; /* * Map the pages to be read into the kva. */ pmap_qenter_noinval(kva, ap->a_m, pcount); /* Build a minimal buffer header. */ bp->b_cmd = BUF_CMD_READ; bp->b_flags |= B_KVABIO; bp->b_bcount = size; bp->b_resid = 0; bsetrunningbufspace(bp, size); bp->b_bio1.bio_offset = offset; bp->b_bio1.bio_done = devfs_spec_getpages_iodone; mycpu->gd_cnt.v_vnodein++; mycpu->gd_cnt.v_vnodepgsin += pcount; /* Do the input. */ vn_strategy(ap->a_vp, &bp->b_bio1); crit_enter(); /* We definitely need to be at splbio here. */ while (bp->b_cmd != BUF_CMD_DONE) tsleep(bp, 0, "spread", 0); crit_exit(); if (bp->b_flags & B_ERROR) { if (bp->b_error) error = bp->b_error; else error = EIO; } /* * If EOF is encountered we must zero-extend the result in order * to ensure that the page does not contain garabge. When no * error occurs, an early EOF is indicated if b_bcount got truncated. * b_resid is relative to b_bcount and should be 0, but some devices * might indicate an EOF with b_resid instead of truncating b_bcount. */ nread = bp->b_bcount - bp->b_resid; if (nread < ap->a_count) { bkvasync(bp); bzero((caddr_t)kva + nread, ap->a_count - nread); } pmap_qremove_noinval(kva, pcount); gotreqpage = 0; for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) { nextoff = toff + PAGE_SIZE; m = ap->a_m[i]; /* * NOTE: vm_page_undirty/clear_dirty etc do not clear the * pmap modified bit. pmap modified bit should have * already been cleared. */ if (nextoff <= nread) { m->valid = VM_PAGE_BITS_ALL; vm_page_undirty(m); } else if (toff < nread) { /* * Since this is a VM request, we have to supply the * unaligned offset to allow vm_page_set_valid() * to zero sub-DEV_BSIZE'd portions of the page. */ vm_page_set_valid(m, 0, nread - toff); vm_page_clear_dirty_end_nonincl(m, 0, nread - toff); } else { m->valid = 0; vm_page_undirty(m); } if (i != ap->a_reqpage) { /* * Just in case someone was asking for this page we * now tell them that it is ok to use. */ if (!error || (m->valid == VM_PAGE_BITS_ALL)) { if (m->valid) { if (m->flags & PG_REFERENCED) { vm_page_activate(m); } else { vm_page_deactivate(m); } vm_page_wakeup(m); } else { vm_page_free(m); } } else { vm_page_free(m); } } else if (m->valid) { gotreqpage = 1; /* * Since this is a VM request, we need to make the * entire page presentable by zeroing invalid sections. */ if (m->valid != VM_PAGE_BITS_ALL) vm_page_zero_invalid(m, FALSE); } } if (!gotreqpage) { m = ap->a_m[ap->a_reqpage]; devfs_debug(DEVFS_DEBUG_WARNING, "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n", devtoname(vp->v_rdev), error, bp, bp->b_vp); devfs_debug(DEVFS_DEBUG_WARNING, " size: %d, resid: %d, a_count: %d, valid: 0x%x\n", size, bp->b_resid, ap->a_count, m->valid); devfs_debug(DEVFS_DEBUG_WARNING, " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n", nread, ap->a_reqpage, (u_long)m->pindex, pcount); /* * Free the buffer header back to the swap buffer pool. */ relpbuf(bp, NULL); return VM_PAGER_ERROR; } /* * Free the buffer header back to the swap buffer pool. */ relpbuf(bp, NULL); if (DEVFS_NODE(ap->a_vp)) vfs_timestamp(&DEVFS_NODE(ap->a_vp)->mtime); return VM_PAGER_OK; } static __inline int sequential_heuristic(struct uio *uio, struct file *fp) { /* * Sequential heuristic - detect sequential operation */ if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || uio->uio_offset == fp->f_nextoff) { /* * XXX we assume that the filesystem block size is * the default. Not true, but still gives us a pretty * good indicator of how sequential the read operations * are. */ int tmpseq = fp->f_seqcount; tmpseq += howmany(uio->uio_resid, MAXBSIZE); if (tmpseq > IO_SEQMAX) tmpseq = IO_SEQMAX; fp->f_seqcount = tmpseq; return(fp->f_seqcount << IO_SEQSHIFT); } /* * Not sequential, quick draw-down of seqcount */ if (fp->f_seqcount > 1) fp->f_seqcount = 1; else fp->f_seqcount = 0; return(0); } |