sys/vfs/hammer2/hammer2_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 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 | /* * Copyright (c) 2011-2018 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@dragonflybsd.org> * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Kernel Filesystem interface * * NOTE! local ipdata pointers must be reloaded on any modifying operation * to the inode as its underlying chain may have changed. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/fcntl.h> #include <sys/buf.h> #include <sys/proc.h> #include <sys/mount.h> #include <sys/vnode.h> #include <sys/mountctl.h> #include <sys/dirent.h> #include <sys/uio.h> #include <sys/objcache.h> #include <sys/event.h> #include <sys/file.h> #include <vfs/fifofs/fifo.h> #include "hammer2.h" static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount); static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio, int ioflag, int seqcount); static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize); static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize); /* * Update directory mtime/ctime after a successful operation. * * hammer2_inode_modify() is MPSAFE w/a shared lock, so we allow the * meta.mtime update SMP race. */ static void hammer2_update_dir_mtime(hammer2_inode_t *dip) { uint64_t mtime; hammer2_update_time(&mtime); hammer2_inode_modify(dip); dip->meta.mtime = mtime; dip->meta.ctime = mtime; } /* * Last reference to a vnode is going away but it is still cached. */ static int hammer2_vop_inactive(struct vop_inactive_args *ap) { hammer2_inode_t *ip; struct vnode *vp; vp = ap->a_vp; ip = VTOI(vp); /* * Degenerate case */ if (ip == NULL) { vrecycle(vp); return (0); } /* * Aquire the inode lock to interlock against vp updates via * the inode path and file deletions and such (which can be * namespace-only operations that might not hold the vnode). */ hammer2_inode_lock(ip, 0); if (ip->flags & HAMMER2_INODE_ISUNLINKED) { int nblksize; /* * If the inode has been unlinked we can throw away all * buffers (dirty or not) and clean the file out. * * Because vrecycle() calls are not guaranteed, try to * dispose of the inode as much as possible right here. */ nblksize = hammer2_calc_logical(ip, 0, NULL, NULL); nvtruncbuf(vp, 0, nblksize, 0, 0); /* * Delete the file on-media. */ if ((ip->flags & HAMMER2_INODE_DELETING) == 0) { atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING); hammer2_inode_delayed_sideq(ip); } hammer2_inode_unlock(ip); /* * Recycle immediately if possible */ vrecycle(vp); } else { hammer2_inode_unlock(ip); } return (0); } /* * Reclaim a vnode so that it can be reused; after the inode is * disassociated, the filesystem must manage it alone. */ static int hammer2_vop_reclaim(struct vop_reclaim_args *ap) { hammer2_inode_t *ip; struct vnode *vp; vp = ap->a_vp; ip = VTOI(vp); if (ip == NULL) return(0); /* * NOTE! We do not attempt to flush chains here, flushing is * really fragile and could also deadlock. */ vclrisdirty(vp); /* * The inode lock is required to disconnect it. */ hammer2_inode_lock(ip, 0); vp->v_data = NULL; ip->vp = NULL; /* * Delete the file on-media. This should have been handled by the * inactivation. The operation is likely still queued on the inode * though so only complain if the stars don't align. */ if ((ip->flags & (HAMMER2_INODE_ISUNLINKED | HAMMER2_INODE_DELETING)) == HAMMER2_INODE_ISUNLINKED) { atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING); hammer2_inode_delayed_sideq(ip); kprintf("hammer2: vp=%p ip=%p unlinked but not disposed\n", vp, ip); } hammer2_inode_unlock(ip); /* * Modified inodes will already be on SIDEQ or SYNCQ, no further * action is needed. * * We cannot safely synchronize the inode from inside the reclaim * due to potentially deep locks held as-of when the reclaim occurs. * Interactions and potential deadlocks abound. We also can't do it * here without desynchronizing from the related directory entrie(s). */ hammer2_inode_drop(ip); /* vp ref */ /* * XXX handle background sync when ip dirty, kernel will no longer * notify us regarding this inode because there is no longer a * vnode attached to it. */ return (0); } /* * Currently this function synchronizes the front-end inode state to the * backend chain topology, then flushes the inode's chain and sub-topology * to backend media. This function does not flush the root topology down to * the inode. */ static int hammer2_vop_fsync(struct vop_fsync_args *ap) { hammer2_inode_t *ip; struct vnode *vp; int error1; int error2; vp = ap->a_vp; ip = VTOI(vp); error1 = 0; hammer2_trans_init(ip->pmp, 0); /* * Flush dirty buffers in the file's logical buffer cache. * It is best to wait for the strategy code to commit the * buffers to the device's backing buffer cache before * then trying to flush the inode. * * This should be quick, but certain inode modifications cached * entirely in the hammer2_inode structure may not trigger a * buffer read until the flush so the fsync can wind up also * doing scattered reads. */ vfsync(vp, ap->a_waitfor, 1, NULL, NULL); bio_track_wait(&vp->v_track_write, 0, 0); /* * Flush any inode changes */ hammer2_inode_lock(ip, 0); if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED)) error1 = hammer2_inode_chain_sync(ip); /* * Flush dirty chains related to the inode. * * NOTE! We are not in a flush transaction. The inode remains on * the sideq so the filesystem syncer can synchronize it to * the volume root. */ error2 = hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP); if (error2) error1 = error2; /* * We may be able to clear the vnode dirty flag. */ if ((ip->flags & (HAMMER2_INODE_MODIFIED | HAMMER2_INODE_RESIZED | HAMMER2_INODE_DIRTYDATA)) == 0 && RB_EMPTY(&vp->v_rbdirty_tree) && !bio_track_active(&vp->v_track_write)) { vclrisdirty(vp); } hammer2_inode_unlock(ip); hammer2_trans_done(ip->pmp, 0); return (error1); } /* * No lock needed, just handle ip->update */ static int hammer2_vop_access(struct vop_access_args *ap) { hammer2_inode_t *ip = VTOI(ap->a_vp); uid_t uid; gid_t gid; mode_t mode; uint32_t uflags; int error; int update; retry: update = spin_access_start(&ip->cluster_spin); /*hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);*/ uid = hammer2_to_unix_xid(&ip->meta.uid); gid = hammer2_to_unix_xid(&ip->meta.gid); mode = ip->meta.mode; uflags = ip->meta.uflags; /*hammer2_inode_unlock(ip);*/ if (__predict_false(spin_access_end(&ip->cluster_spin, update))) goto retry; error = vop_helper_access(ap, uid, gid, mode, uflags); return (error); } static int hammer2_vop_getattr(struct vop_getattr_args *ap) { hammer2_pfs_t *pmp; hammer2_inode_t *ip; struct vnode *vp; struct vattr *vap; int update; vp = ap->a_vp; vap = ap->a_vap; ip = VTOI(vp); pmp = ip->pmp; retry: update = spin_access_start(&ip->cluster_spin); vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0]; vap->va_fileid = ip->meta.inum; vap->va_mode = ip->meta.mode; vap->va_nlink = ip->meta.nlinks; vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid); vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid); vap->va_rmajor = 0; vap->va_rminor = 0; vap->va_size = ip->meta.size; /* protected by shared lock */ vap->va_blocksize = HAMMER2_PBUFSIZE; vap->va_flags = ip->meta.uflags; hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime); hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime); hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime); vap->va_gen = 1; vap->va_bytes = 0; if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) { /* * Can't really calculate directory use sans the files under * it, just assume one block for now. */ vap->va_bytes += HAMMER2_INODE_BYTES; } else { vap->va_bytes = hammer2_inode_data_count(ip); } vap->va_type = hammer2_get_vtype(ip->meta.type); vap->va_filerev = 0; vap->va_uid_uuid = ip->meta.uid; vap->va_gid_uuid = ip->meta.gid; vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID | VA_FSID_UUID_VALID; if (__predict_false(spin_access_end(&ip->cluster_spin, update))) goto retry; return (0); } static int hammer2_vop_getattr_lite(struct vop_getattr_lite_args *ap) { hammer2_pfs_t *pmp; hammer2_inode_t *ip; struct vnode *vp; struct vattr_lite *lvap; int update; vp = ap->a_vp; lvap = ap->a_lvap; ip = VTOI(vp); pmp = ip->pmp; retry: update = spin_access_start(&ip->cluster_spin); #if 0 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0]; vap->va_fileid = ip->meta.inum; #endif lvap->va_mode = ip->meta.mode; lvap->va_nlink = ip->meta.nlinks; lvap->va_uid = hammer2_to_unix_xid(&ip->meta.uid); lvap->va_gid = hammer2_to_unix_xid(&ip->meta.gid); #if 0 vap->va_rmajor = 0; vap->va_rminor = 0; #endif lvap->va_size = ip->meta.size; #if 0 vap->va_blocksize = HAMMER2_PBUFSIZE; #endif lvap->va_flags = ip->meta.uflags; lvap->va_type = hammer2_get_vtype(ip->meta.type); #if 0 vap->va_filerev = 0; vap->va_uid_uuid = ip->meta.uid; vap->va_gid_uuid = ip->meta.gid; vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID | VA_FSID_UUID_VALID; #endif if (__predict_false(spin_access_end(&ip->cluster_spin, update))) goto retry; return (0); } static int hammer2_vop_setattr(struct vop_setattr_args *ap) { hammer2_inode_t *ip; struct vnode *vp; struct vattr *vap; int error; int kflags = 0; uint64_t ctime; vp = ap->a_vp; vap = ap->a_vap; hammer2_update_time(&ctime); ip = VTOI(vp); if (ip->pmp->ronly) return (EROFS); /* * Normally disallow setattr if there is no space, unless we * are in emergency mode (might be needed to chflags -R noschg * files prior to removal). */ if ((ip->pmp->flags & HAMMER2_PMPF_EMERG) == 0 && hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1) { return (ENOSPC); } hammer2_trans_init(ip->pmp, 0); hammer2_inode_lock(ip, 0); error = 0; if (vap->va_flags != VNOVAL) { uint32_t flags; flags = ip->meta.uflags; error = vop_helper_setattr_flags(&flags, vap->va_flags, hammer2_to_unix_xid(&ip->meta.uid), ap->a_cred); if (error == 0) { if (ip->meta.uflags != flags) { hammer2_inode_modify(ip); hammer2_spin_lock_update(&ip->cluster_spin); ip->meta.uflags = flags; ip->meta.ctime = ctime; hammer2_spin_unlock_update(&ip->cluster_spin); kflags |= NOTE_ATTRIB; } if (ip->meta.uflags & (IMMUTABLE | APPEND)) { error = 0; goto done; } } goto done; } if (ip->meta.uflags & (IMMUTABLE | APPEND)) { error = EPERM; goto done; } if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { mode_t cur_mode = ip->meta.mode; uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid); gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid); uuid_t uuid_uid; uuid_t uuid_gid; error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid, ap->a_cred, &cur_uid, &cur_gid, &cur_mode); if (error == 0) { hammer2_guid_to_uuid(&uuid_uid, cur_uid); hammer2_guid_to_uuid(&uuid_gid, cur_gid); if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) || bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) || ip->meta.mode != cur_mode ) { hammer2_inode_modify(ip); hammer2_spin_lock_update(&ip->cluster_spin); ip->meta.uid = uuid_uid; ip->meta.gid = uuid_gid; ip->meta.mode = cur_mode; ip->meta.ctime = ctime; hammer2_spin_unlock_update(&ip->cluster_spin); } kflags |= NOTE_ATTRIB; } } /* * Resize the file */ if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) { switch(vp->v_type) { case VREG: if (vap->va_size == ip->meta.size) break; if (vap->va_size < ip->meta.size) { hammer2_mtx_ex(&ip->truncate_lock); hammer2_truncate_file(ip, vap->va_size); hammer2_mtx_unlock(&ip->truncate_lock); kflags |= NOTE_WRITE; } else { hammer2_extend_file(ip, vap->va_size); kflags |= NOTE_WRITE | NOTE_EXTEND; } hammer2_inode_modify(ip); ip->meta.ctime = ctime; ip->meta.mtime = ctime; vclrflags(vp, VLASTWRITETS); break; default: error = EINVAL; goto done; } } #if 0 /* atime not supported */ if (vap->va_atime.tv_sec != VNOVAL) { hammer2_inode_modify(ip); ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime); kflags |= NOTE_ATTRIB; } #endif if (vap->va_mode != (mode_t)VNOVAL) { mode_t cur_mode = ip->meta.mode; uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid); gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid); error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred, cur_uid, cur_gid, &cur_mode); if (error == 0) { hammer2_inode_modify(ip); hammer2_spin_lock_update(&ip->cluster_spin); ip->meta.mode = cur_mode; ip->meta.ctime = ctime; hammer2_spin_unlock_update(&ip->cluster_spin); kflags |= NOTE_ATTRIB; } } if (vap->va_mtime.tv_sec != VNOVAL) { hammer2_inode_modify(ip); ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime); kflags |= NOTE_ATTRIB; vclrflags(vp, VLASTWRITETS); } done: /* * If a truncation occurred we must call chain_sync() now in order * to trim the related data chains, otherwise a later expansion can * cause havoc. * * If an extend occured that changed the DIRECTDATA state, we must * call inode_chain_sync now in order to prepare the inode's indirect * block table. * * WARNING! This means we are making an adjustment to the inode's * chain outside of sync/fsync, and not just to inode->meta, which * may result in some consistency issues if a crash were to occur * at just the wrong time. */ if (ip->flags & HAMMER2_INODE_RESIZED) hammer2_inode_chain_sync(ip); /* * Cleanup. */ hammer2_inode_unlock(ip); hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); hammer2_knote(ip->vp, kflags); return (error); } static int hammer2_vop_readdir(struct vop_readdir_args *ap) { hammer2_xop_readdir_t *xop; hammer2_blockref_t bref; hammer2_inode_t *ip; hammer2_tid_t inum; hammer2_key_t lkey; struct uio *uio; off_t *cookies; off_t saveoff; int cookie_index; int ncookies; int error; int eofflag; int r; ip = VTOI(ap->a_vp); uio = ap->a_uio; saveoff = uio->uio_offset; eofflag = 0; error = 0; /* * Setup cookies directory entry cookies if requested */ if (ap->a_ncookies) { ncookies = uio->uio_resid / 16 + 1; if (ncookies > 1024) ncookies = 1024; cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); } else { ncookies = -1; cookies = NULL; } cookie_index = 0; hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED); /* * Handle artificial entries. To ensure that only positive 64 bit * quantities are returned to userland we always strip off bit 63. * The hash code is designed such that codes 0x0000-0x7FFF are not * used, allowing us to use these codes for artificial entries. * * Entry 0 is used for '.' and entry 1 is used for '..'. Do not * allow '..' to cross the mount point into (e.g.) the super-root. */ if (saveoff == 0) { inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK; r = vop_write_dirent(&error, uio, inum, 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) { inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK; if (ip != ip->pmp->iroot) inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK; r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, ".."); if (r) goto done; if (cookies) cookies[cookie_index] = saveoff; ++saveoff; ++cookie_index; if (cookie_index == ncookies) goto done; } lkey = saveoff | HAMMER2_DIRHASH_VISIBLE; if (hammer2_debug & 0x0020) kprintf("readdir: lkey %016jx\n", lkey); if (error) goto done; xop = hammer2_xop_alloc(ip, 0); xop->lkey = lkey; hammer2_xop_start(&xop->head, &hammer2_readdir_desc); for (;;) { const hammer2_inode_data_t *ripdata; const char *dname; int dtype; error = hammer2_xop_collect(&xop->head, 0); error = hammer2_error_to_errno(error); if (error) { break; } if (cookie_index == ncookies) break; if (hammer2_debug & 0x0020) kprintf("cluster chain %p %p\n", xop->head.cluster.focus, (xop->head.cluster.focus ? xop->head.cluster.focus->data : (void *)-1)); hammer2_cluster_bref(&xop->head.cluster, &bref); if (bref.type == HAMMER2_BREF_TYPE_INODE) { ripdata = &hammer2_xop_gdata(&xop->head)->ipdata; dtype = hammer2_get_dtype(ripdata->meta.type); saveoff = bref.key & HAMMER2_DIRHASH_USERMSK; r = vop_write_dirent(&error, uio, ripdata->meta.inum & HAMMER2_DIRHASH_USERMSK, dtype, ripdata->meta.name_len, ripdata->filename); hammer2_xop_pdata(&xop->head); if (r) break; if (cookies) cookies[cookie_index] = saveoff; ++cookie_index; } else if (bref.type == HAMMER2_BREF_TYPE_DIRENT) { uint16_t namlen; dtype = hammer2_get_dtype(bref.embed.dirent.type); saveoff = bref.key & HAMMER2_DIRHASH_USERMSK; namlen = bref.embed.dirent.namlen; if (namlen <= sizeof(bref.check.buf)) { dname = bref.check.buf; } else { dname = hammer2_xop_gdata(&xop->head)->buf; } r = vop_write_dirent(&error, uio, bref.embed.dirent.inum, dtype, namlen, dname); if (namlen > sizeof(bref.check.buf)) hammer2_xop_pdata(&xop->head); if (r) break; if (cookies) cookies[cookie_index] = saveoff; ++cookie_index; } else { /* XXX chain error */ kprintf("bad chain type readdir %d\n", bref.type); } } hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); if (error == ENOENT) { error = 0; eofflag = 1; saveoff = (hammer2_key_t)-1; } else { saveoff = bref.key & HAMMER2_DIRHASH_USERMSK; } done: hammer2_inode_unlock(ip); if (ap->a_eofflag) *ap->a_eofflag = eofflag; if (hammer2_debug & 0x0020) kprintf("readdir: done at %016jx\n", saveoff); uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE; 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); } /* * hammer2_vop_readlink { vp, uio, cred } */ static int hammer2_vop_readlink(struct vop_readlink_args *ap) { struct vnode *vp; hammer2_inode_t *ip; int error; vp = ap->a_vp; if (vp->v_type != VLNK) return (EINVAL); ip = VTOI(vp); error = hammer2_read_file(ip, ap->a_uio, 0); return (error); } static int hammer2_vop_read(struct vop_read_args *ap) { struct vnode *vp; hammer2_inode_t *ip; struct uio *uio; int error; int seqcount; /* * Read operations supported on this vnode? */ vp = ap->a_vp; if (vp->v_type == VDIR) return (EISDIR); if (vp->v_type != VREG) return (EINVAL); /* * Misc */ ip = VTOI(vp); uio = ap->a_uio; error = 0; seqcount = ap->a_ioflag >> IO_SEQSHIFT; error = hammer2_read_file(ip, uio, seqcount); return (error); } static int hammer2_vop_write(struct vop_write_args *ap) { hammer2_inode_t *ip; thread_t td; struct vnode *vp; struct uio *uio; int error; int seqcount; int ioflag; /* * Read operations supported on this vnode? */ vp = ap->a_vp; if (vp->v_type != VREG) return (EINVAL); /* * Misc */ ip = VTOI(vp); ioflag = ap->a_ioflag; uio = ap->a_uio; error = 0; if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) { case 2: return (ENOSPC); case 1: ioflag |= IO_DIRECT; /* semi-synchronous */ /* fall through */ default: break; } seqcount = ioflag >> IO_SEQSHIFT; /* * Check resource limit */ if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc && uio->uio_offset + uio->uio_resid > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ); return (EFBIG); } /* * The transaction interlocks against flush initiations * (note: but will run concurrently with the actual flush). * * To avoid deadlocking against the VM system, we must flag any * transaction related to the buffer cache or other direct * VM page manipulation. */ if (uio->uio_segflg == UIO_NOCOPY) { hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE); } else { hammer2_trans_init(ip->pmp, 0); } error = hammer2_write_file(ip, uio, ioflag, seqcount); if (uio->uio_segflg == UIO_NOCOPY) hammer2_trans_done(ip->pmp, HAMMER2_TRANS_BUFCACHE | HAMMER2_TRANS_SIDEQ); else hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); return (error); } /* * Perform read operations on a file or symlink given an UNLOCKED * inode and uio. * * The passed ip is not locked. */ static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount) { hammer2_off_t size; struct buf *bp; int error; error = 0; /* * UIO read loop. * * WARNING! Assumes that the kernel interlocks size changes at the * vnode level. */ hammer2_mtx_sh(&ip->lock); hammer2_mtx_sh(&ip->truncate_lock); size = ip->meta.size; hammer2_mtx_unlock(&ip->lock); while (uio->uio_resid > 0 && uio->uio_offset < size) { hammer2_key_t lbase; hammer2_key_t leof; int lblksize; int loff; int n; lblksize = hammer2_calc_logical(ip, uio->uio_offset, &lbase, &leof); #if 1 bp = NULL; error = cluster_readx(ip->vp, leof, lbase, lblksize, B_NOTMETA | B_KVABIO, uio->uio_resid, seqcount * MAXBSIZE, &bp); #else if (uio->uio_segflg == UIO_NOCOPY) { bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY | GETBLK_KVABIO, 0); if (bp->b_flags & B_CACHE) { int i; int j = 0; if (bp->b_xio.xio_npages != 16) kprintf("NPAGES BAD\n"); for (i = 0; i < bp->b_xio.xio_npages; ++i) { vm_page_t m; m = bp->b_xio.xio_pages[i]; if (m == NULL || m->valid == 0) { kprintf("bp %016jx %016jx pg %d inv", lbase, leof, i); if (m) kprintf("m->object %p/%p", m->object, ip->vp->v_object); kprintf("\n"); j = 1; } } if (j) kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error); } bqrelse(bp); } error = bread_kvabio(ip->vp, lbase, lblksize, &bp); #endif if (error) { brelse(bp); break; } bkvasync(bp); loff = (int)(uio->uio_offset - lbase); n = lblksize - loff; if (n > uio->uio_resid) n = uio->uio_resid; if (n > size - uio->uio_offset) n = (int)(size - uio->uio_offset); bp->b_flags |= B_AGE; uiomovebp(bp, bp->b_data + loff, n, uio); bqrelse(bp); } hammer2_mtx_unlock(&ip->truncate_lock); return (error); } /* * Write to the file represented by the inode via the logical buffer cache. * The inode may represent a regular file or a symlink. * * The inode must not be locked. */ static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio, int ioflag, int seqcount) { hammer2_key_t old_eof; hammer2_key_t new_eof; struct buf *bp; int kflags; int error; int modified; /* * Setup if append * * WARNING! Assumes that the kernel interlocks size changes at the * vnode level. */ hammer2_mtx_ex(&ip->lock); hammer2_mtx_sh(&ip->truncate_lock); if (ioflag & IO_APPEND) uio->uio_offset = ip->meta.size; old_eof = ip->meta.size; /* * Extend the file if necessary. If the write fails at some point * we will truncate it back down to cover as much as we were able * to write. * * Doing this now makes it easier to calculate buffer sizes in * the loop. */ kflags = 0; error = 0; modified = 0; if (uio->uio_offset + uio->uio_resid > old_eof) { new_eof = uio->uio_offset + uio->uio_resid; modified = 1; hammer2_extend_file(ip, new_eof); kflags |= NOTE_EXTEND; } else { new_eof = old_eof; } hammer2_mtx_unlock(&ip->lock); /* * UIO write loop */ while (uio->uio_resid > 0) { hammer2_key_t lbase; int trivial; int endofblk; int lblksize; int loff; int n; /* * Don't allow the buffer build to blow out the buffer * cache. */ if ((ioflag & IO_RECURSE) == 0) bwillwrite(HAMMER2_PBUFSIZE); /* * This nominally tells us how much we can cluster and * what the logical buffer size needs to be. Currently * we don't try to cluster the write and just handle one * block at a time. */ lblksize = hammer2_calc_logical(ip, uio->uio_offset, &lbase, NULL); loff = (int)(uio->uio_offset - lbase); KKASSERT(lblksize <= MAXBSIZE); /* * Calculate bytes to copy this transfer and whether the * copy completely covers the buffer or not. */ trivial = 0; n = lblksize - loff; if (n > uio->uio_resid) { n = uio->uio_resid; if (loff == lbase && uio->uio_offset + n == new_eof) trivial = 1; endofblk = 0; } else { if (loff == 0) trivial = 1; endofblk = 1; } if (lbase >= new_eof) trivial = 1; /* * Get the buffer */ if (uio->uio_segflg == UIO_NOCOPY) { /* * Issuing a write with the same data backing the * buffer. Instantiate the buffer to collect the * backing vm pages, then read-in any missing bits. * * This case is used by vop_stdputpages(). */ bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY | GETBLK_KVABIO, 0); if ((bp->b_flags & B_CACHE) == 0) { bqrelse(bp); error = bread_kvabio(ip->vp, lbase, lblksize, &bp); } } else if (trivial) { /* * Even though we are entirely overwriting the buffer * we may still have to zero it out to avoid a * mmap/write visibility issue. */ bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY | GETBLK_KVABIO, 0); if ((bp->b_flags & B_CACHE) == 0) vfs_bio_clrbuf(bp); } else { /* * Partial overwrite, read in any missing bits then * replace the portion being written. * * (The strategy code will detect zero-fill physical * blocks for this case). */ error = bread_kvabio(ip->vp, lbase, lblksize, &bp); if (error == 0) bheavy(bp); } if (error) { brelse(bp); break; } /* * Ok, copy the data in */ bkvasync(bp); error = uiomovebp(bp, bp->b_data + loff, n, uio); kflags |= NOTE_WRITE; modified = 1; if (error) { brelse(bp); break; } /* * WARNING: Pageout daemon will issue UIO_NOCOPY writes * with IO_SYNC or IO_ASYNC set. These writes * must be handled as the pageout daemon expects. * * NOTE! H2 relies on cluster_write() here because it * cannot preallocate disk blocks at the logical * level due to not knowing what the compression * size will be at this time. * * We must use cluster_write() here and we depend * on the write-behind feature to flush buffers * appropriately. If we let the buffer daemons do * it the block allocations will be all over the * map. */ if (ioflag & IO_SYNC) { bwrite(bp); } else if ((ioflag & IO_DIRECT) && endofblk) { bawrite(bp); } else if (ioflag & IO_ASYNC) { bawrite(bp); } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) { bdwrite(bp); } else { #if 1 bp->b_flags |= B_CLUSTEROK; cluster_write(bp, new_eof, lblksize, seqcount); #else bp->b_flags |= B_CLUSTEROK; bdwrite(bp); #endif } } /* * Cleanup. If we extended the file EOF but failed to write through * the entire write is a failure and we have to back-up. */ if (error && new_eof != old_eof) { hammer2_mtx_unlock(&ip->truncate_lock); hammer2_mtx_ex(&ip->lock); /* note lock order */ hammer2_mtx_ex(&ip->truncate_lock); /* note lock order */ hammer2_truncate_file(ip, old_eof); if (ip->flags & HAMMER2_INODE_MODIFIED) hammer2_inode_chain_sync(ip); hammer2_mtx_unlock(&ip->lock); } else if (modified) { struct vnode *vp = ip->vp; hammer2_mtx_ex(&ip->lock); hammer2_inode_modify(ip); if (uio->uio_segflg == UIO_NOCOPY) { if (vp->v_flag & VLASTWRITETS) { ip->meta.mtime = (unsigned long)vp->v_lastwrite_ts.tv_sec * 1000000 + vp->v_lastwrite_ts.tv_nsec / 1000; } } else { hammer2_update_time(&ip->meta.mtime); vclrflags(vp, VLASTWRITETS); } #if 0 /* * REMOVED - handled by hammer2_extend_file(). Do not issue * a chain_sync() outside of a sync/fsync except for DIRECTDATA * state changes. * * Under normal conditions we only issue a chain_sync if * the inode's DIRECTDATA state changed. */ if (ip->flags & HAMMER2_INODE_RESIZED) hammer2_inode_chain_sync(ip); #endif hammer2_mtx_unlock(&ip->lock); hammer2_knote(ip->vp, kflags); } hammer2_trans_assert_strategy(ip->pmp); hammer2_mtx_unlock(&ip->truncate_lock); return error; } /* * Truncate the size of a file. The inode must be locked. * * We must unconditionally set HAMMER2_INODE_RESIZED to properly * ensure that any on-media data beyond the new file EOF has been destroyed. * * WARNING: nvtruncbuf() can only be safely called without the inode lock * held due to the way our write thread works. If the truncation * occurs in the middle of a buffer, nvtruncbuf() is responsible * for dirtying that buffer and zeroing out trailing bytes. * * WARNING! Assumes that the kernel interlocks size changes at the * vnode level. * * WARNING! Caller assumes responsibility for removing dead blocks * if INODE_RESIZED is set. */ static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize) { int nblksize; hammer2_mtx_unlock(&ip->lock); if (ip->vp) { nblksize = hammer2_calc_logical(ip, 0, NULL, NULL); nvtruncbuf(ip->vp, nsize, nblksize, (int)nsize & (nblksize - 1), 0); } hammer2_mtx_ex(&ip->lock); KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0); ip->osize = ip->meta.size; ip->meta.size = nsize; atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED); hammer2_inode_modify(ip); } /* * Extend the size of a file. The inode must be locked. * * Even though the file size is changing, we do not have to set the * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES * boundary. When this occurs a hammer2_inode_chain_sync() is required * to prepare the inode cluster's indirect block table, otherwise * async execution of the strategy code will implode on us. * * WARNING! Assumes that the kernel interlocks size changes at the * vnode level. * * WARNING! Caller assumes responsibility for transitioning out * of the inode DIRECTDATA mode if INODE_RESIZED is set. */ static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize) { hammer2_key_t osize; int oblksize; int nblksize; int error; KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0); hammer2_inode_modify(ip); osize = ip->meta.size; ip->osize = osize; ip->meta.size = nsize; /* * We must issue a chain_sync() when the DIRECTDATA state changes * to prevent confusion between the flush code and the in-memory * state. This is not perfect because we are doing it outside of * a sync/fsync operation, so it might not be fully synchronized * with the meta-data topology flush. * * We must retain and re-dirty the buffer cache buffer containing * the direct data so it can be written to a real block. It should * not be possible for a bread error to occur since the original data * is extracted from the inode structure directly. */ if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) { if (osize) { struct buf *bp; oblksize = hammer2_calc_logical(ip, 0, NULL, NULL); error = bread_kvabio(ip->vp, 0, oblksize, &bp); atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED); hammer2_inode_chain_sync(ip); if (error == 0) { bheavy(bp); bdwrite(bp); } else { brelse(bp); } } else { atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED); hammer2_inode_chain_sync(ip); } } hammer2_mtx_unlock(&ip->lock); if (ip->vp) { oblksize = hammer2_calc_logical(ip, 0, NULL, NULL); nblksize = hammer2_calc_logical(ip, 0, NULL, NULL); nvextendbuf(ip->vp, osize, nsize, oblksize, nblksize, -1, -1, 0); } hammer2_mtx_ex(&ip->lock); } static int hammer2_vop_nresolve(struct vop_nresolve_args *ap) { hammer2_xop_nresolve_t *xop; hammer2_inode_t *ip; hammer2_inode_t *dip; struct namecache *ncp; struct vnode *vp; int error; dip = VTOI(ap->a_dvp); xop = hammer2_xop_alloc(dip, 0); ncp = ap->a_nch->ncp; hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); /* * Note: In DragonFly the kernel handles '.' and '..'. */ hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED); hammer2_xop_start(&xop->head, &hammer2_nresolve_desc); error = hammer2_xop_collect(&xop->head, 0); error = hammer2_error_to_errno(error); if (error) { ip = NULL; } else { ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); } hammer2_inode_unlock(dip); /* * Acquire the related vnode * * NOTE: For error processing, only ENOENT resolves the namecache * entry to NULL, otherwise we just return the error and * leave the namecache unresolved. * * WARNING: inode structure is locked exclusively via inode_get * but chain was locked shared. inode_unlock() * will handle it properly. */ if (ip) { vp = hammer2_igetv(ip, &error); /* error set to UNIX error */ if (error == 0) { vn_unlock(vp); cache_setvp(ap->a_nch, vp); } else if (error == ENOENT) { cache_setvp(ap->a_nch, NULL); } hammer2_inode_unlock(ip); /* * The vp should not be released until after we've disposed * of our locks, because it might cause vop_inactive() to * be called. */ if (vp) vrele(vp); } else { error = ENOENT; cache_setvp(ap->a_nch, NULL); } hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); KASSERT(error || ap->a_nch->ncp->nc_vp != NULL, ("resolve error %d/%p ap %p\n", error, ap->a_nch->ncp->nc_vp, ap)); return error; } static int hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) { hammer2_inode_t *dip; hammer2_tid_t inum; int error; dip = VTOI(ap->a_dvp); inum = dip->meta.iparent; *ap->a_vpp = NULL; if (inum) { error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL, inum, ap->a_vpp); } else { error = ENOENT; } return error; } static int hammer2_vop_nmkdir(struct vop_nmkdir_args *ap) { hammer2_inode_t *dip; hammer2_inode_t *nip; struct namecache *ncp; const char *name; size_t name_len; hammer2_tid_t inum; int error; dip = VTOI(ap->a_dvp); if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) return (ENOSPC); ncp = ap->a_nch->ncp; name = ncp->nc_name; name_len = ncp->nc_nlen; hammer2_trans_init(dip->pmp, 0); inum = hammer2_trans_newinum(dip->pmp); /* * Create the directory as an inode and then create the directory * entry. * * dip must be locked before nip to avoid deadlock. */ hammer2_inode_lock(dip, 0); nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, inum, &error); if (error) { error = hammer2_error_to_errno(error); } else { error = hammer2_dirent_create(dip, name, name_len, nip->meta.inum, nip->meta.type); /* returns UNIX error code */ } if (error) { if (nip) { hammer2_inode_unlink_finisher(nip, NULL); hammer2_inode_unlock(nip); nip = NULL; } *ap->a_vpp = NULL; } else { /* * inode_depend() must occur before the igetv() because * the igetv() can temporarily release the inode lock. */ hammer2_inode_depend(dip, nip); /* before igetv */ *ap->a_vpp = hammer2_igetv(nip, &error); hammer2_inode_unlock(nip); } if (error == 0) hammer2_update_dir_mtime(dip); hammer2_inode_unlock(dip); hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); if (error == 0) { cache_setunresolved(ap->a_nch); cache_setvp(ap->a_nch, *ap->a_vpp); hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); } return error; } static int hammer2_vop_open(struct vop_open_args *ap) { return vop_stdopen(ap); } /* * hammer2_vop_advlock { vp, id, op, fl, flags } */ static int hammer2_vop_advlock(struct vop_advlock_args *ap) { hammer2_inode_t *ip = VTOI(ap->a_vp); hammer2_off_t size; size = ip->meta.size; return (lf_advlock(ap, &ip->advlock, size)); } static int hammer2_vop_close(struct vop_close_args *ap) { return vop_stdclose(ap); } /* * hammer2_vop_nlink { nch, dvp, vp, cred } * * Create a hardlink from (vp) to {dvp, nch}. */ static int hammer2_vop_nlink(struct vop_nlink_args *ap) { hammer2_inode_t *tdip; /* target directory to create link in */ hammer2_inode_t *ip; /* inode we are hardlinking to */ struct namecache *ncp; const char *name; size_t name_len; int error; uint64_t cmtime; if (ap->a_dvp->v_mount != ap->a_vp->v_mount) return(EXDEV); tdip = VTOI(ap->a_dvp); if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1) return (ENOSPC); ncp = ap->a_nch->ncp; name = ncp->nc_name; name_len = ncp->nc_nlen; /* * ip represents the file being hardlinked. The file could be a * normal file or a hardlink target if it has already been hardlinked. * (with the new semantics, it will almost always be a hardlink * target). * * Bump nlinks and potentially also create or move the hardlink * target in the parent directory common to (ip) and (tdip). The * consolidation code can modify ip->cluster. The returned cluster * is locked. */ ip = VTOI(ap->a_vp); KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp)); hammer2_trans_init(ip->pmp, 0); /* * Target should be an indexed inode or there's no way we will ever * be able to find it! */ KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0); hammer2_inode_lock4(tdip, ip, NULL, NULL); hammer2_update_time(&cmtime); /* * Create the directory entry and bump nlinks. * Also update ip's ctime. */ error = hammer2_dirent_create(tdip, name, name_len, ip->meta.inum, ip->meta.type); hammer2_inode_modify(ip); ++ip->meta.nlinks; ip->meta.ctime = cmtime; if (error == 0) { /* * Update dip's [cm]time */ hammer2_inode_modify(tdip); tdip->meta.mtime = cmtime; tdip->meta.ctime = cmtime; cache_setunresolved(ap->a_nch); cache_setvp(ap->a_nch, ap->a_vp); } hammer2_inode_unlock(ip); hammer2_inode_unlock(tdip); hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); hammer2_knote(ap->a_vp, NOTE_LINK); hammer2_knote(ap->a_dvp, NOTE_WRITE); return error; } /* * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap } * * The operating system has already ensured that the directory entry * does not exist and done all appropriate namespace locking. */ static int hammer2_vop_ncreate(struct vop_ncreate_args *ap) { hammer2_inode_t *dip; hammer2_inode_t *nip; struct namecache *ncp; const char *name; size_t name_len; hammer2_tid_t inum; int error; dip = VTOI(ap->a_dvp); if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) return (ENOSPC); ncp = ap->a_nch->ncp; name = ncp->nc_name; name_len = ncp->nc_nlen; hammer2_trans_init(dip->pmp, 0); inum = hammer2_trans_newinum(dip->pmp); /* * Create the regular file as an inode and then create the directory * entry. * * dip must be locked before nip to avoid deadlock. */ hammer2_inode_lock(dip, 0); nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, inum, &error); if (error) { error = hammer2_error_to_errno(error); } else { error = hammer2_dirent_create(dip, name, name_len, nip->meta.inum, nip->meta.type); } if (error) { if (nip) { hammer2_inode_unlink_finisher(nip, NULL); hammer2_inode_unlock(nip); nip = NULL; } *ap->a_vpp = NULL; } else { hammer2_inode_depend(dip, nip); /* before igetv */ *ap->a_vpp = hammer2_igetv(nip, &error); hammer2_inode_unlock(nip); } if (error == 0) hammer2_update_dir_mtime(dip); hammer2_inode_unlock(dip); hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); if (error == 0) { cache_setunresolved(ap->a_nch); cache_setvp(ap->a_nch, *ap->a_vpp); hammer2_knote(ap->a_dvp, NOTE_WRITE); } return error; } /* * Make a device node (typically a fifo) */ static int hammer2_vop_nmknod(struct vop_nmknod_args *ap) { hammer2_inode_t *dip; hammer2_inode_t *nip; struct namecache *ncp; const char *name; size_t name_len; hammer2_tid_t inum; int error; dip = VTOI(ap->a_dvp); if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) return (ENOSPC); ncp = ap->a_nch->ncp; name = ncp->nc_name; name_len = ncp->nc_nlen; hammer2_trans_init(dip->pmp, 0); /* * Create the device inode and then create the directory entry. * * dip must be locked before nip to avoid deadlock. */ inum = hammer2_trans_newinum(dip->pmp); hammer2_inode_lock(dip, 0); nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, inum, &error); if (error) { error = hammer2_error_to_errno(error); } else { error = hammer2_dirent_create(dip, name, name_len, nip->meta.inum, nip->meta.type); } if (error) { if (nip) { hammer2_inode_unlink_finisher(nip, NULL); hammer2_inode_unlock(nip); nip = NULL; } *ap->a_vpp = NULL; } else { hammer2_inode_depend(dip, nip); /* before igetv */ *ap->a_vpp = hammer2_igetv(nip, &error); hammer2_inode_unlock(nip); } if (error == 0) hammer2_update_dir_mtime(dip); hammer2_inode_unlock(dip); hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); if (error == 0) { cache_setunresolved(ap->a_nch); cache_setvp(ap->a_nch, *ap->a_vpp); hammer2_knote(ap->a_dvp, NOTE_WRITE); } return error; } /* * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target } */ static int hammer2_vop_nsymlink(struct vop_nsymlink_args *ap) { hammer2_inode_t *dip; hammer2_inode_t *nip; struct namecache *ncp; const char *name; size_t name_len; hammer2_tid_t inum; int error; dip = VTOI(ap->a_dvp); if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) return (ENOSPC); ncp = ap->a_nch->ncp; name = ncp->nc_name; name_len = ncp->nc_nlen; hammer2_trans_init(dip->pmp, 0); ap->a_vap->va_type = VLNK; /* enforce type */ /* * Create the softlink as an inode and then create the directory * entry. * * dip must be locked before nip to avoid deadlock. */ inum = hammer2_trans_newinum(dip->pmp); hammer2_inode_lock(dip, 0); nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred, inum, &error); if (error) { error = hammer2_error_to_errno(error); } else { error = hammer2_dirent_create(dip, name, name_len, nip->meta.inum, nip->meta.type); } if (error) { if (nip) { hammer2_inode_unlink_finisher(nip, NULL); hammer2_inode_unlock(nip); nip = NULL; } *ap->a_vpp = NULL; hammer2_inode_unlock(dip); hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); return error; } hammer2_inode_depend(dip, nip); /* before igetv */ *ap->a_vpp = hammer2_igetv(nip, &error); /* * Build the softlink (~like file data) and finalize the namecache. */ if (error == 0) { size_t bytes; struct uio auio; struct iovec aiov; bytes = strlen(ap->a_target); hammer2_inode_unlock(nip); bzero(&auio, sizeof(auio)); bzero(&aiov, sizeof(aiov)); auio.uio_iov = &aiov; auio.uio_segflg = UIO_SYSSPACE; auio.uio_rw = UIO_WRITE; auio.uio_resid = bytes; auio.uio_iovcnt = 1; auio.uio_td = curthread; aiov.iov_base = ap->a_target; aiov.iov_len = bytes; error = hammer2_write_file(nip, &auio, IO_APPEND, 0); /* XXX handle error */ error = 0; } else { hammer2_inode_unlock(nip); } if (error == 0) hammer2_update_dir_mtime(dip); hammer2_inode_unlock(dip); hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); /* * Finalize namecache */ if (error == 0) { cache_setunresolved(ap->a_nch); cache_setvp(ap->a_nch, *ap->a_vpp); hammer2_knote(ap->a_dvp, NOTE_WRITE); } return error; } /* * hammer2_vop_nremove { nch, dvp, cred } */ static int hammer2_vop_nremove(struct vop_nremove_args *ap) { hammer2_xop_unlink_t *xop; hammer2_inode_t *dip; hammer2_inode_t *ip; struct vnode *vprecycle; struct namecache *ncp; int error; dip = VTOI(ap->a_dvp); if (dip->pmp->ronly) return (EROFS); #if 0 /* allow removals, except user to also bulkfree */ if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) return (ENOSPC); #endif ncp = ap->a_nch->ncp; if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) { kprintf("hammer2: attempt to delete inside debug inode: %s\n", ncp->nc_name); while (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) { tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5); } } hammer2_trans_init(dip->pmp, 0); hammer2_inode_lock(dip, 0); xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); xop->isdir = 0; xop->dopermanent = 0; hammer2_xop_start(&xop->head, &hammer2_unlink_desc); error = hammer2_xop_collect(&xop->head, 0); error = hammer2_error_to_errno(error); vprecycle = NULL; if (error == 0) { ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); if (ip) { if (hammer2_debug_inode && ip->meta.inum == hammer2_debug_inode) { kprintf("hammer2: attempt to delete debug " "inode!\n"); while (hammer2_debug_inode && ip->meta.inum == hammer2_debug_inode) { tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5); } } hammer2_inode_unlink_finisher(ip, &vprecycle); hammer2_inode_depend(dip, ip); /* after modified */ hammer2_inode_unlock(ip); } } else { hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); } if (error == 0) hammer2_update_dir_mtime(dip); hammer2_inode_unlock(dip); hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); if (error == 0) { cache_unlink(ap->a_nch); hammer2_knote(ap->a_dvp, NOTE_WRITE); } if (vprecycle) hammer2_inode_vprecycle(vprecycle); return (error); } /* * hammer2_vop_nrmdir { nch, dvp, cred } */ static int hammer2_vop_nrmdir(struct vop_nrmdir_args *ap) { hammer2_xop_unlink_t *xop; hammer2_inode_t *dip; hammer2_inode_t *ip; struct namecache *ncp; struct vnode *vprecycle; int error; dip = VTOI(ap->a_dvp); if (dip->pmp->ronly) return (EROFS); #if 0 /* allow removals, except user to also bulkfree */ if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1) return (ENOSPC); #endif hammer2_trans_init(dip->pmp, 0); hammer2_inode_lock(dip, 0); xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); ncp = ap->a_nch->ncp; hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen); xop->isdir = 1; xop->dopermanent = 0; hammer2_xop_start(&xop->head, &hammer2_unlink_desc); error = hammer2_xop_collect(&xop->head, 0); error = hammer2_error_to_errno(error); vprecycle = NULL; if (error == 0) { ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); if (ip) { hammer2_inode_unlink_finisher(ip, &vprecycle); hammer2_inode_depend(dip, ip); /* after modified */ hammer2_inode_unlock(ip); } } else { hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); } if (error == 0) hammer2_update_dir_mtime(dip); hammer2_inode_unlock(dip); hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ); if (error == 0) { cache_unlink(ap->a_nch); hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); } if (vprecycle) hammer2_inode_vprecycle(vprecycle); return (error); } /* * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred } */ static int hammer2_vop_nrename(struct vop_nrename_args *ap) { struct namecache *fncp; struct namecache *tncp; hammer2_inode_t *fdip; /* source directory */ hammer2_inode_t *tdip; /* target directory */ hammer2_inode_t *ip; /* file being renamed */ hammer2_inode_t *tip; /* replaced target during rename or NULL */ struct vnode *vprecycle; const char *fname; size_t fname_len; const char *tname; size_t tname_len; int error; int update_tdip; int update_fdip; hammer2_key_t tlhc; if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount) return(EXDEV); if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount) return(EXDEV); fdip = VTOI(ap->a_fdvp); /* source directory */ tdip = VTOI(ap->a_tdvp); /* target directory */ if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1) return (ENOSPC); fncp = ap->a_fnch->ncp; /* entry name in source */ fname = fncp->nc_name; fname_len = fncp->nc_nlen; tncp = ap->a_tnch->ncp; /* entry name in target */ tname = tncp->nc_name; tname_len = tncp->nc_nlen; hammer2_trans_init(tdip->pmp, 0); update_tdip = 0; update_fdip = 0; ip = VTOI(fncp->nc_vp); hammer2_inode_ref(ip); /* extra ref */ /* * Lookup the target name to determine if a directory entry * is being overwritten. We only hold related inode locks * temporarily, the operating system is expected to protect * against rename races. */ tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL; if (tip) hammer2_inode_ref(tip); /* extra ref */ /* * For now try to avoid deadlocks with a simple pointer address * test. (tip) can be NULL. */ error = 0; { hammer2_inode_t *ip1 = fdip; hammer2_inode_t *ip2 = tdip; hammer2_inode_t *ip3 = ip; hammer2_inode_t *ip4 = tip; /* may be NULL */ if (fdip > tdip) { ip1 = tdip; ip2 = fdip; } if (tip && ip > tip) { ip3 = tip; ip4 = ip; } hammer2_inode_lock4(ip1, ip2, ip3, ip4); } /* * Resolve the collision space for (tdip, tname, tname_len) * * tdip must be held exclusively locked to prevent races since * multiple filenames can end up in the same collision space. */ { hammer2_xop_scanlhc_t *sxop; hammer2_key_t lhcbase; tlhc = hammer2_dirhash(tname, tname_len); lhcbase = tlhc; sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING); sxop->lhc = tlhc; hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc); while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) { if (tlhc != sxop->head.cluster.focus->bref.key) break; ++tlhc; } error = hammer2_error_to_errno(error); hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP); if (error) { if (error != ENOENT) goto done2; ++tlhc; error = 0; } if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) { error = ENOSPC; goto done2; } } /* * Ready to go, issue the rename to the backend. Note that meta-data * updates to the related inodes occur separately from the rename * operation. * * NOTE: While it is not necessary to update ip->meta.name*, doing * so aids catastrophic recovery and debugging. */ if (error == 0) { hammer2_xop_nrename_t *xop4; xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING); xop4->lhc = tlhc; xop4->ip_key = ip->meta.name_key; hammer2_xop_setip2(&xop4->head, ip); hammer2_xop_setip3(&xop4->head, tdip); if (tip && tip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) hammer2_xop_setip4(&xop4->head, tip); hammer2_xop_setname(&xop4->head, fname, fname_len); hammer2_xop_setname2(&xop4->head, tname, tname_len); hammer2_xop_start(&xop4->head, &hammer2_nrename_desc); error = hammer2_xop_collect(&xop4->head, 0); error = hammer2_error_to_errno(error); hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP); if (error == ENOENT) error = 0; /* * Update inode meta-data. * * WARNING! The in-memory inode (ip) structure does not * maintain a copy of the inode's filename buffer. */ if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) { hammer2_inode_modify(ip); ip->meta.name_len = tname_len; ip->meta.name_key = tlhc; } if (error == 0) { hammer2_inode_modify(ip); ip->meta.iparent = tdip->meta.inum; } update_fdip = 1; update_tdip = 1; } done2: /* * If no error, the backend has replaced the target directory entry. * We must adjust nlinks on the original replace target if it exists. */ vprecycle = NULL; if (error == 0 && tip) { hammer2_inode_unlink_finisher(tip, &vprecycle); } /* * Update directory mtimes to represent the something changed. */ if (update_fdip || update_tdip) { uint64_t mtime; hammer2_update_time(&mtime); if (update_fdip) { hammer2_inode_modify(fdip); fdip->meta.mtime = mtime; } if (update_tdip) { hammer2_inode_modify(tdip); tdip->meta.mtime = mtime; } } if (tip) { hammer2_inode_unlock(tip); hammer2_inode_drop(tip); } hammer2_inode_unlock(ip); hammer2_inode_unlock(tdip); hammer2_inode_unlock(fdip); hammer2_inode_drop(ip); hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ); /* * Issue the namecache update after unlocking all the internal * hammer2 structures, otherwise we might deadlock. * * WARNING! The target namespace must be updated atomically, * and we depend on cache_rename() to handle that for * us. Do not do a separate cache_unlink() because * that leaves a small window of opportunity for other * threads to allocate the target namespace before we * manage to complete our rename. * * WARNING! cache_rename() (and cache_unlink()) will properly * set VREF_FINALIZE on any attached vnode. Do not * call cache_setunresolved() manually before-hand as * this will prevent the flag from being set later via * cache_rename(). If VREF_FINALIZE is not properly set * and the inode is no longer in the topology, related * chains can remain dirty indefinitely. */ if (error == 0 && tip) { /*cache_unlink(ap->a_tnch); see above */ /*cache_setunresolved(ap->a_tnch); see above */ } if (error == 0) { cache_rename(ap->a_fnch, ap->a_tnch); hammer2_knote(ap->a_fdvp, NOTE_WRITE); hammer2_knote(ap->a_tdvp, NOTE_WRITE); hammer2_knote(fncp->nc_vp, NOTE_RENAME); } if (vprecycle) hammer2_inode_vprecycle(vprecycle); return (error); } /* * hammer2_vop_ioctl { vp, command, data, fflag, cred } */ static int hammer2_vop_ioctl(struct vop_ioctl_args *ap) { hammer2_inode_t *ip; int error; ip = VTOI(ap->a_vp); error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data, ap->a_fflag, ap->a_cred); return (error); } static int hammer2_vop_mountctl(struct vop_mountctl_args *ap) { struct mount *mp; hammer2_pfs_t *pmp; int rc; switch (ap->a_op) { case (MOUNTCTL_SET_EXPORT): mp = ap->a_head.a_ops->head.vv_mount; pmp = MPTOPMP(mp); if (ap->a_ctllen != sizeof(struct export_args)) rc = (EINVAL); else rc = vfs_export(mp, &pmp->export, (const struct export_args *)ap->a_ctl); break; default: rc = vop_stdmountctl(ap); break; } return (rc); } /* * KQFILTER */ static void filt_hammer2detach(struct knote *kn); static int filt_hammer2read(struct knote *kn, long hint); static int filt_hammer2write(struct knote *kn, long hint); static int filt_hammer2vnode(struct knote *kn, long hint); static struct filterops hammer2read_filtops = { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, filt_hammer2detach, filt_hammer2read }; static struct filterops hammer2write_filtops = { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, filt_hammer2detach, filt_hammer2write }; static struct filterops hammer2vnode_filtops = { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, filt_hammer2detach, filt_hammer2vnode }; static int hammer2_vop_kqfilter(struct vop_kqfilter_args *ap) { struct vnode *vp = ap->a_vp; struct knote *kn = ap->a_kn; switch (kn->kn_filter) { case EVFILT_READ: kn->kn_fop = &hammer2read_filtops; break; case EVFILT_WRITE: kn->kn_fop = &hammer2write_filtops; break; case EVFILT_VNODE: kn->kn_fop = &hammer2vnode_filtops; break; default: return (EOPNOTSUPP); } kn->kn_hook = (caddr_t)vp; knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); return(0); } static void filt_hammer2detach(struct knote *kn) { struct vnode *vp = (void *)kn->kn_hook; knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); } static int filt_hammer2read(struct knote *kn, long hint) { struct vnode *vp = (void *)kn->kn_hook; hammer2_inode_t *ip = VTOI(vp); off_t off; if (hint == NOTE_REVOKE) { kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); return(1); } off = ip->meta.size - kn->kn_fp->f_offset; kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX; if (kn->kn_sfflags & NOTE_OLDAPI) return(1); return (kn->kn_data != 0); } static int filt_hammer2write(struct knote *kn, long hint) { if (hint == NOTE_REVOKE) kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); kn->kn_data = 0; return (1); } static int filt_hammer2vnode(struct knote *kn, long hint) { if (kn->kn_sfflags & hint) kn->kn_fflags |= hint; if (hint == NOTE_REVOKE) { kn->kn_flags |= (EV_EOF | EV_NODATA); return (1); } return (kn->kn_fflags != 0); } /* * FIFO VOPS */ static int hammer2_vop_markatime(struct vop_markatime_args *ap) { hammer2_inode_t *ip; struct vnode *vp; vp = ap->a_vp; ip = VTOI(vp); if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG)) return (EROFS); return(0); } static int hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap) { int error; error = VOCALL(&fifo_vnode_vops, &ap->a_head); if (error) error = hammer2_vop_kqfilter(ap); return(error); } /* * VOPS vector */ struct vop_ops hammer2_vnode_vops = { .vop_default = vop_defaultop, .vop_fsync = hammer2_vop_fsync, .vop_getpages = vop_stdgetpages, .vop_putpages = vop_stdputpages, .vop_access = hammer2_vop_access, .vop_advlock = hammer2_vop_advlock, .vop_close = hammer2_vop_close, .vop_nlink = hammer2_vop_nlink, .vop_ncreate = hammer2_vop_ncreate, .vop_nsymlink = hammer2_vop_nsymlink, .vop_nremove = hammer2_vop_nremove, .vop_nrmdir = hammer2_vop_nrmdir, .vop_nrename = hammer2_vop_nrename, .vop_getattr = hammer2_vop_getattr, .vop_getattr_lite = hammer2_vop_getattr_lite, .vop_setattr = hammer2_vop_setattr, .vop_readdir = hammer2_vop_readdir, .vop_readlink = hammer2_vop_readlink, .vop_read = hammer2_vop_read, .vop_write = hammer2_vop_write, .vop_open = hammer2_vop_open, .vop_inactive = hammer2_vop_inactive, .vop_reclaim = hammer2_vop_reclaim, .vop_nresolve = hammer2_vop_nresolve, .vop_nlookupdotdot = hammer2_vop_nlookupdotdot, .vop_nmkdir = hammer2_vop_nmkdir, .vop_nmknod = hammer2_vop_nmknod, .vop_ioctl = hammer2_vop_ioctl, .vop_mountctl = hammer2_vop_mountctl, .vop_bmap = hammer2_vop_bmap, .vop_strategy = hammer2_vop_strategy, .vop_kqfilter = hammer2_vop_kqfilter }; struct vop_ops hammer2_spec_vops = { .vop_default = vop_defaultop, .vop_fsync = hammer2_vop_fsync, .vop_read = vop_stdnoread, .vop_write = vop_stdnowrite, .vop_access = hammer2_vop_access, .vop_close = hammer2_vop_close, .vop_markatime = hammer2_vop_markatime, .vop_getattr = hammer2_vop_getattr, .vop_inactive = hammer2_vop_inactive, .vop_reclaim = hammer2_vop_reclaim, .vop_setattr = hammer2_vop_setattr }; struct vop_ops hammer2_fifo_vops = { .vop_default = fifo_vnoperate, .vop_fsync = hammer2_vop_fsync, #if 0 .vop_read = hammer2_vop_fiforead, .vop_write = hammer2_vop_fifowrite, #endif .vop_access = hammer2_vop_access, #if 0 .vop_close = hammer2_vop_fifoclose, #endif .vop_markatime = hammer2_vop_markatime, .vop_getattr = hammer2_vop_getattr, .vop_inactive = hammer2_vop_inactive, .vop_reclaim = hammer2_vop_reclaim, .vop_setattr = hammer2_vop_setattr, .vop_kqfilter = hammer2_vop_fifokqfilter }; |