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
|
// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package gofer provides a filesystem implementation that is backed by a 9p
// server, interchangably referred to as "gofers" throughout this package.
//
// Lock order:
//
// regularFileFD/directoryFD.mu
// filesystem.renameMu
// dentry.cachingMu
// dentryCache.mu
// dentry.dirMu
// filesystem.syncMu
// dentry.metadataMu
// *** "memmap.Mappable locks" below this point
// dentry.mapsMu
// *** "memmap.Mappable locks taken by Translate" below this point
// dentry.handleMu
// dentry.dataMu
// filesystem.inoMu
// specialFileFD.mu
// specialFileFD.bufMu
//
// Locking dentry.dirMu and dentry.metadataMu in multiple dentries requires that
// either ancestor dentries are locked before descendant dentries, or that
// filesystem.renameMu is locked for writing.
package gofer
import (
"fmt"
"path"
"strconv"
"strings"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/atomicbitops"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/hostarch"
"gvisor.dev/gvisor/pkg/lisafs"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/refs"
fslock "gvisor.dev/gvisor/pkg/sentry/fsimpl/lock"
"gvisor.dev/gvisor/pkg/sentry/fsutil"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/kernel/pipe"
ktime "gvisor.dev/gvisor/pkg/sentry/kernel/time"
"gvisor.dev/gvisor/pkg/sentry/memmap"
"gvisor.dev/gvisor/pkg/sentry/pgalloc"
"gvisor.dev/gvisor/pkg/sentry/socket/unix/transport"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/unet"
)
// Name is the default filesystem name.
const Name = "9p"
// Mount option names for goferfs.
const (
moptTransport = "trans"
moptReadFD = "rfdno"
moptWriteFD = "wfdno"
moptAname = "aname"
moptDfltUID = "dfltuid"
moptDfltGID = "dfltgid"
moptCache = "cache"
moptForcePageCache = "force_page_cache"
moptLimitHostFDTranslation = "limit_host_fd_translation"
moptOverlayfsStaleRead = "overlayfs_stale_read"
)
// Valid values for the "cache" mount option.
const (
cacheNone = "none"
cacheFSCache = "fscache"
cacheFSCacheWritethrough = "fscache_writethrough"
cacheRemoteRevalidating = "remote_revalidating"
)
const (
defaultMaxCachedDentries = 1000
maxCachedNegativeChildren = 1000
)
// stringFixedCache is a fixed sized cache, once initialized,
// its size never changes.
//
// +stateify savable
type stringFixedCache struct {
// namesList stores negative names with fifo list.
// name stored in namesList only means it used to be negative
// at the moment you pushed it to the list.
namesList stringList
size uint64
}
func (cache *stringFixedCache) isInited() bool {
return cache.size != 0
}
func (cache *stringFixedCache) init(size uint64) {
elements := make([]stringListElem, size)
for i := uint64(0); i < size; i++ {
cache.namesList.PushFront(&elements[i])
}
cache.size = size
}
// Update will push name to the front of the list,
// and pop the tail value.
func (cache *stringFixedCache) add(name string) string {
tail := cache.namesList.Back()
victimName := tail.str
tail.str = name
cache.namesList.Remove(tail)
cache.namesList.PushFront(tail)
return victimName
}
// +stateify savable
type dentryCache struct {
// mu protects the below fields.
mu sync.Mutex `state:"nosave"`
// dentries contains all dentries with 0 references. Due to race conditions,
// it may also contain dentries with non-zero references.
dentries dentryList
// dentriesLen is the number of dentries in dentries.
dentriesLen uint64
// maxCachedDentries is the maximum number of cachable dentries.
maxCachedDentries uint64
}
// SetDentryCacheSize sets the size of the global gofer dentry cache.
func SetDentryCacheSize(size int) {
if size < 0 {
return
}
if globalDentryCache != nil {
log.Warningf("Global dentry cache has already been initialized. Ignoring subsequent attempt.")
return
}
globalDentryCache = &dentryCache{maxCachedDentries: uint64(size)}
}
// globalDentryCache is a global cache of dentries across all gofers.
var globalDentryCache *dentryCache
// Valid values for "trans" mount option.
const transportModeFD = "fd"
// FilesystemType implements vfs.FilesystemType.
//
// +stateify savable
type FilesystemType struct{}
// filesystem implements vfs.FilesystemImpl.
//
// +stateify savable
type filesystem struct {
vfsfs vfs.Filesystem
// mfp is used to allocate memory that caches regular file contents. mfp is
// immutable.
mfp pgalloc.MemoryFileProvider
// Immutable options.
opts filesystemOptions
iopts InternalFilesystemOptions
// client is the LISAFS client used for communicating with the server. client
// is immutable.
client *lisafs.Client `state:"nosave"`
// clock is a realtime clock used to set timestamps in file operations.
clock ktime.Clock
// devMinor is the filesystem's minor device number. devMinor is immutable.
devMinor uint32
// root is the root dentry. root is immutable.
root *dentry
// renameMu serves two purposes:
//
// - It synchronizes path resolution with renaming initiated by this
// client.
//
// - It is held by path resolution to ensure that reachable dentries remain
// valid. A dentry is reachable by path resolution if it has a non-zero
// reference count (such that it is usable as vfs.ResolvingPath.Start() or
// is reachable from its children), or if it is a child dentry (such that
// it is reachable from its parent).
renameMu sync.RWMutex `state:"nosave"`
dentryCache *dentryCache
// syncableDentries contains all non-synthetic dentries. specialFileFDs
// contains all open specialFileFDs. These fields are protected by syncMu.
syncMu sync.Mutex `state:"nosave"`
syncableDentries dentryList
specialFileFDs specialFDList
// inoByKey maps previously-observed device ID and host inode numbers to
// internal inode numbers assigned to those files. inoByKey is not preserved
// across checkpoint/restore because inode numbers may be reused between
// different gofer processes, so inode numbers may be repeated for different
// files across checkpoint/restore. inoByKey is protected by inoMu.
inoMu sync.Mutex `state:"nosave"`
inoByKey map[inoKey]uint64 `state:"nosave"`
// lastIno is the last inode number assigned to a file. lastIno is accessed
// using atomic memory operations.
lastIno atomicbitops.Uint64
// savedDentryRW records open read/write handles during save/restore.
savedDentryRW map[*dentry]savedDentryRW
// released is nonzero once filesystem.Release has been called.
released atomicbitops.Int32
}
// +stateify savable
type filesystemOptions struct {
fd int
aname string
interop InteropMode // derived from the "cache" mount option
dfltuid auth.KUID
dfltgid auth.KGID
// If forcePageCache is true, host FDs may not be used for application
// memory mappings even if available; instead, the client must perform its
// own caching of regular file pages. This is primarily useful for testing.
forcePageCache bool
// If limitHostFDTranslation is true, apply maxFillRange() constraints to
// host FD mappings returned by dentry.(memmap.Mappable).Translate(). This
// makes memory accounting behavior more consistent between cases where
// host FDs are / are not available, but may increase the frequency of
// sentry-handled page faults on files for which a host FD is available.
limitHostFDTranslation bool
// If overlayfsStaleRead is true, O_RDONLY host FDs provided by the remote
// filesystem may not be coherent with writable host FDs opened later, so
// all uses of the former must be replaced by uses of the latter. This is
// usually only the case when the remote filesystem is a Linux overlayfs
// mount. (Prior to Linux 4.18, patch series centered on commit
// d1d04ef8572b "ovl: stack file ops", both I/O and memory mappings were
// incoherent between pre-copy-up and post-copy-up FDs; after that patch
// series, only memory mappings are incoherent.)
overlayfsStaleRead bool
// If regularFilesUseSpecialFileFD is true, application FDs representing
// regular files will use distinct file handles for each FD, in the same
// way that application FDs representing "special files" such as sockets
// do. Note that this disables client caching for regular files. This option
// may regress performance due to excessive Open RPCs. This option is not
// supported with overlayfsStaleRead for now.
regularFilesUseSpecialFileFD bool
}
// InteropMode controls the client's interaction with other remote filesystem
// users.
//
// +stateify savable
type InteropMode uint32
const (
// InteropModeExclusive is appropriate when the filesystem client is the
// only user of the remote filesystem.
//
// - The client may cache arbitrary filesystem state (file data, metadata,
// filesystem structure, etc.).
//
// - Client changes to filesystem state may be sent to the remote
// filesystem asynchronously, except when server permission checks are
// necessary.
//
// - File timestamps are based on client clocks. This ensures that users of
// the client observe timestamps that are coherent with their own clocks
// and consistent with Linux's semantics (in particular, it is not always
// possible for clients to set arbitrary atimes and mtimes depending on the
// remote filesystem implementation, and never possible for clients to set
// arbitrary ctimes.)
InteropModeExclusive InteropMode = iota
// InteropModeWritethrough is appropriate when there are read-only users of
// the remote filesystem that expect to observe changes made by the
// filesystem client.
//
// - The client may cache arbitrary filesystem state.
//
// - Client changes to filesystem state must be sent to the remote
// filesystem synchronously.
//
// - File timestamps are based on client clocks. As a corollary, access
// timestamp changes from other remote filesystem users will not be visible
// to the client.
InteropModeWritethrough
// InteropModeShared is appropriate when there are users of the remote
// filesystem that may mutate its state other than the client.
//
// - The client must verify ("revalidate") cached filesystem state before
// using it.
//
// - Client changes to filesystem state must be sent to the remote
// filesystem synchronously.
//
// - File timestamps are based on server clocks. This is necessary to
// ensure that timestamp changes are synchronized between remote filesystem
// users.
//
// Note that the correctness of InteropModeShared depends on the server
// correctly implementing 9P fids (i.e. each fid immutably represents a
// single filesystem object), even in the presence of remote filesystem
// mutations from other users. If this is violated, the behavior of the
// client is undefined.
InteropModeShared
)
// InternalFilesystemOptions may be passed as
// vfs.GetFilesystemOptions.InternalData to FilesystemType.GetFilesystem.
//
// +stateify savable
type InternalFilesystemOptions struct {
// If UniqueID is non-empty, it is an opaque string used to reassociate the
// filesystem with a new server FD during restoration from checkpoint.
UniqueID string
// If LeakConnection is true, do not close the connection to the server
// when the Filesystem is released. This is necessary for deployments in
// which servers can handle only a single client and report failure if that
// client disconnects.
LeakConnection bool
// If OpenSocketsByConnecting is true, silently translate attempts to open
// files identifying as sockets to connect RPCs.
OpenSocketsByConnecting bool
}
// _V9FS_DEFUID and _V9FS_DEFGID (from Linux's fs/9p/v9fs.h) are the default
// UIDs and GIDs used for files that do not provide a specific owner or group
// respectively.
const (
// uint32(-2) doesn't work in Go.
_V9FS_DEFUID = auth.KUID(4294967294)
_V9FS_DEFGID = auth.KGID(4294967294)
)
// Name implements vfs.FilesystemType.Name.
func (FilesystemType) Name() string {
return Name
}
// Release implements vfs.FilesystemType.Release.
func (FilesystemType) Release(ctx context.Context) {}
// GetFilesystem implements vfs.FilesystemType.GetFilesystem.
func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.VirtualFilesystem, creds *auth.Credentials, source string, opts vfs.GetFilesystemOptions) (*vfs.Filesystem, *vfs.Dentry, error) {
mfp := pgalloc.MemoryFileProviderFromContext(ctx)
if mfp == nil {
ctx.Warningf("gofer.FilesystemType.GetFilesystem: context does not provide a pgalloc.MemoryFileProvider")
return nil, nil, linuxerr.EINVAL
}
mopts := vfs.GenericParseMountOptions(opts.Data)
var fsopts filesystemOptions
fd, err := getFDFromMountOptionsMap(ctx, mopts)
if err != nil {
return nil, nil, err
}
fsopts.fd = fd
// Get the attach name.
fsopts.aname = "/"
if aname, ok := mopts[moptAname]; ok {
delete(mopts, moptAname)
if !path.IsAbs(aname) {
ctx.Warningf("gofer.FilesystemType.GetFilesystem: aname is not absolute: %s=%s", moptAname, aname)
return nil, nil, linuxerr.EINVAL
}
fsopts.aname = path.Clean(aname)
}
// Parse the cache policy. For historical reasons, this defaults to the
// least generally-applicable option, InteropModeExclusive.
fsopts.interop = InteropModeExclusive
if cache, ok := mopts[moptCache]; ok {
delete(mopts, moptCache)
switch cache {
case cacheFSCache:
fsopts.interop = InteropModeExclusive
case cacheFSCacheWritethrough:
fsopts.interop = InteropModeWritethrough
case cacheNone:
fsopts.regularFilesUseSpecialFileFD = true
fallthrough
case cacheRemoteRevalidating:
fsopts.interop = InteropModeShared
default:
ctx.Warningf("gofer.FilesystemType.GetFilesystem: invalid cache policy: %s=%s", moptCache, cache)
return nil, nil, linuxerr.EINVAL
}
}
// Parse the default UID and GID.
fsopts.dfltuid = _V9FS_DEFUID
if dfltuidstr, ok := mopts[moptDfltUID]; ok {
delete(mopts, moptDfltUID)
dfltuid, err := strconv.ParseUint(dfltuidstr, 10, 32)
if err != nil {
ctx.Warningf("gofer.FilesystemType.GetFilesystem: invalid default UID: %s=%s", moptDfltUID, dfltuidstr)
return nil, nil, linuxerr.EINVAL
}
// In Linux, dfltuid is interpreted as a UID and is converted to a KUID
// in the caller's user namespace, but goferfs isn't
// application-mountable.
fsopts.dfltuid = auth.KUID(dfltuid)
}
fsopts.dfltgid = _V9FS_DEFGID
if dfltgidstr, ok := mopts[moptDfltGID]; ok {
delete(mopts, moptDfltGID)
dfltgid, err := strconv.ParseUint(dfltgidstr, 10, 32)
if err != nil {
ctx.Warningf("gofer.FilesystemType.GetFilesystem: invalid default UID: %s=%s", moptDfltGID, dfltgidstr)
return nil, nil, linuxerr.EINVAL
}
fsopts.dfltgid = auth.KGID(dfltgid)
}
// Handle simple flags.
if _, ok := mopts[moptForcePageCache]; ok {
delete(mopts, moptForcePageCache)
fsopts.forcePageCache = true
}
if _, ok := mopts[moptLimitHostFDTranslation]; ok {
delete(mopts, moptLimitHostFDTranslation)
fsopts.limitHostFDTranslation = true
}
if _, ok := mopts[moptOverlayfsStaleRead]; ok {
delete(mopts, moptOverlayfsStaleRead)
fsopts.overlayfsStaleRead = true
}
// fsopts.regularFilesUseSpecialFileFD can only be enabled by specifying
// "cache=none".
// Check for unparsed options.
if len(mopts) != 0 {
ctx.Warningf("gofer.FilesystemType.GetFilesystem: unknown options: %v", mopts)
return nil, nil, linuxerr.EINVAL
}
// Validation.
if fsopts.regularFilesUseSpecialFileFD && fsopts.overlayfsStaleRead {
// These options are not supported together. To support this, when a dentry
// is opened writably for the first time, we need to iterate over all the
// specialFileFDs of that dentry that represent a regular file and call
// fd.hostFileMapper.RegenerateMappings(writable_fd).
ctx.Warningf("gofer.FilesystemType.GetFilesystem: regularFilesUseSpecialFileFD and overlayfsStaleRead options are not supported together.")
return nil, nil, linuxerr.EINVAL
}
// Handle internal options.
iopts, ok := opts.InternalData.(InternalFilesystemOptions)
if opts.InternalData != nil && !ok {
ctx.Warningf("gofer.FilesystemType.GetFilesystem: GetFilesystemOptions.InternalData has type %T, wanted gofer.InternalFilesystemOptions", opts.InternalData)
return nil, nil, linuxerr.EINVAL
}
// If !ok, iopts being the zero value is correct.
// Construct the filesystem object.
devMinor, err := vfsObj.GetAnonBlockDevMinor()
if err != nil {
return nil, nil, err
}
fs := &filesystem{
mfp: mfp,
opts: fsopts,
iopts: iopts,
clock: ktime.RealtimeClockFromContext(ctx),
devMinor: devMinor,
inoByKey: make(map[inoKey]uint64),
}
// Did the user configure a global dentry cache?
if globalDentryCache != nil {
fs.dentryCache = globalDentryCache
} else {
fs.dentryCache = &dentryCache{maxCachedDentries: defaultMaxCachedDentries}
}
fs.vfsfs.Init(vfsObj, &fstype, fs)
if err := fs.initClientAndRoot(ctx); err != nil {
fs.vfsfs.DecRef(ctx)
return nil, nil, err
}
return &fs.vfsfs, &fs.root.vfsd, nil
}
func (fs *filesystem) initClientAndRoot(ctx context.Context) error {
rootInode, err := fs.initClient(ctx)
if err != nil {
return err
}
fs.root, err = fs.newDentry(ctx, &rootInode)
if err != nil {
fs.client.CloseFD(ctx, rootInode.ControlFD, false /* flush */)
return err
}
// Set the root's reference count to 2. One reference is returned to the
// caller, and the other is held by fs to prevent the root from being "cached"
// and subsequently evicted.
fs.root.refs = atomicbitops.FromInt64(2)
return nil
}
func (fs *filesystem) initClient(ctx context.Context) (lisafs.Inode, error) {
sock, err := unet.NewSocket(fs.opts.fd)
if err != nil {
return lisafs.Inode{}, err
}
var rootInode lisafs.Inode
ctx.UninterruptibleSleepStart(false)
fs.client, rootInode, err = lisafs.NewClient(sock)
ctx.UninterruptibleSleepFinish(false)
if err != nil {
return lisafs.Inode{}, err
}
if fs.opts.aname == "/" {
return rootInode, nil
}
// Walk to the attach point from root inode. aname is always absolute.
rootFD := fs.client.NewFD(rootInode.ControlFD)
status, inodes, err := rootFD.WalkMultiple(ctx, strings.Split(fs.opts.aname, "/")[1:])
rootFD.Close(ctx, false /* flush */)
if err != nil {
return lisafs.Inode{}, err
}
// Close all intermediate FDs to the attach point.
numInodes := len(inodes)
for i := 0; i < numInodes-1; i++ {
curFD := fs.client.NewFD(inodes[i].ControlFD)
curFD.Close(ctx, false /* flush */)
}
switch status {
case lisafs.WalkSuccess:
return inodes[numInodes-1], nil
default:
if numInodes > 0 {
last := fs.client.NewFD(inodes[numInodes-1].ControlFD)
last.Close(ctx, false /* flush */)
}
log.Warningf("initClient failed because walk to attach point %q failed: lisafs.WalkStatus = %v", fs.opts.aname, status)
return lisafs.Inode{}, unix.ENOENT
}
}
func getFDFromMountOptionsMap(ctx context.Context, mopts map[string]string) (int, error) {
// Check that the transport is "fd".
trans, ok := mopts[moptTransport]
if !ok || trans != transportModeFD {
ctx.Warningf("gofer.getFDFromMountOptionsMap: transport must be specified as '%s=%s'", moptTransport, transportModeFD)
return -1, linuxerr.EINVAL
}
delete(mopts, moptTransport)
// Check that read and write FDs are provided and identical.
rfdstr, ok := mopts[moptReadFD]
if !ok {
ctx.Warningf("gofer.getFDFromMountOptionsMap: read FD must be specified as '%s=<file descriptor>'", moptReadFD)
return -1, linuxerr.EINVAL
}
delete(mopts, moptReadFD)
rfd, err := strconv.Atoi(rfdstr)
if err != nil {
ctx.Warningf("gofer.getFDFromMountOptionsMap: invalid read FD: %s=%s", moptReadFD, rfdstr)
return -1, linuxerr.EINVAL
}
wfdstr, ok := mopts[moptWriteFD]
if !ok {
ctx.Warningf("gofer.getFDFromMountOptionsMap: write FD must be specified as '%s=<file descriptor>'", moptWriteFD)
return -1, linuxerr.EINVAL
}
delete(mopts, moptWriteFD)
wfd, err := strconv.Atoi(wfdstr)
if err != nil {
ctx.Warningf("gofer.getFDFromMountOptionsMap: invalid write FD: %s=%s", moptWriteFD, wfdstr)
return -1, linuxerr.EINVAL
}
if rfd != wfd {
ctx.Warningf("gofer.getFDFromMountOptionsMap: read FD (%d) and write FD (%d) must be equal", rfd, wfd)
return -1, linuxerr.EINVAL
}
return rfd, nil
}
// Release implements vfs.FilesystemImpl.Release.
func (fs *filesystem) Release(ctx context.Context) {
fs.released.Store(1)
mf := fs.mfp.MemoryFile()
fs.syncMu.Lock()
for elem := fs.syncableDentries.Front(); elem != nil; elem = elem.Next() {
d := elem.d
d.handleMu.Lock()
d.dataMu.Lock()
if h := d.writeHandleLocked(); h.isOpen() {
// Write dirty cached data to the remote file.
if err := fsutil.SyncDirtyAll(ctx, &d.cache, &d.dirty, d.size.Load(), mf, h.writeFromBlocksAt); err != nil {
log.Warningf("gofer.filesystem.Release: failed to flush dentry: %v", err)
}
// TODO(jamieliu): Do we need to flushf/fsync d?
}
// Discard cached pages.
d.cache.DropAll(mf)
d.dirty.RemoveAll()
d.dataMu.Unlock()
// Close host FDs if they exist. We can use RacyLoad() because d.handleMu
// is locked.
if d.readFD.RacyLoad() >= 0 {
_ = unix.Close(int(d.readFD.RacyLoad()))
}
if d.writeFD.RacyLoad() >= 0 && d.readFD.RacyLoad() != d.writeFD.RacyLoad() {
_ = unix.Close(int(d.writeFD.RacyLoad()))
}
d.readFD = atomicbitops.FromInt32(-1)
d.writeFD = atomicbitops.FromInt32(-1)
d.mmapFD = atomicbitops.FromInt32(-1)
d.handleMu.Unlock()
}
// There can't be any specialFileFDs still using fs, since each such
// FileDescription would hold a reference on a Mount holding a reference on
// fs.
fs.syncMu.Unlock()
// If leak checking is enabled, release all outstanding references in the
// filesystem. We deliberately avoid doing this outside of leak checking; we
// have released all external resources above rather than relying on dentry
// destructors. fs.root may be nil if creating the client or initializing the
// root dentry failed in GetFilesystem.
if refs.GetLeakMode() != refs.NoLeakChecking && fs.root != nil {
fs.renameMu.Lock()
fs.root.releaseSyntheticRecursiveLocked(ctx)
fs.evictAllCachedDentriesLocked(ctx)
fs.renameMu.Unlock()
// An extra reference was held by the filesystem on the root to prevent it from
// being cached/evicted.
fs.root.DecRef(ctx)
}
if !fs.iopts.LeakConnection {
// Close the connection to the server. This implicitly closes all FDs.
if fs.client != nil {
fs.client.Close()
}
}
fs.vfsfs.VirtualFilesystem().PutAnonBlockDevMinor(fs.devMinor)
}
// releaseSyntheticRecursiveLocked traverses the tree with root d and decrements
// the reference count on every synthetic dentry. Synthetic dentries have one
// reference for existence that should be dropped during filesystem.Release.
//
// Precondition: d.fs.renameMu is locked for writing.
func (d *dentry) releaseSyntheticRecursiveLocked(ctx context.Context) {
if d.isSynthetic() {
d.decRefNoCaching()
d.checkCachingLocked(ctx, true /* renameMuWriteLocked */)
}
if d.isDir() {
var children []*dentry
d.dirMu.Lock()
for _, child := range d.children {
children = append(children, child)
}
d.dirMu.Unlock()
for _, child := range children {
if child != nil {
child.releaseSyntheticRecursiveLocked(ctx)
}
}
}
}
// inoKey is the key used to identify the inode backed by this dentry.
//
// +stateify savable
type inoKey struct {
ino uint64
devMinor uint32
devMajor uint32
}
func inoKeyFromStat(stat *linux.Statx) inoKey {
return inoKey{
ino: stat.Ino,
devMinor: stat.DevMinor,
devMajor: stat.DevMajor,
}
}
// dentry implements vfs.DentryImpl.
//
// +stateify savable
type dentry struct {
vfsd vfs.Dentry
// refs is the reference count. Each dentry holds a reference on its
// parent, even if disowned. An additional reference is held on all
// synthetic dentries until they are unlinked or invalidated. When refs
// reaches 0, the dentry may be added to the cache or destroyed. If refs ==
// -1, the dentry has already been destroyed. refs is accessed using atomic
// memory operations.
refs atomicbitops.Int64
// fs is the owning filesystem. fs is immutable.
fs *filesystem
// parent is this dentry's parent directory. Each dentry holds a reference
// on its parent. If this dentry is a filesystem root, parent is nil.
// parent is protected by filesystem.renameMu.
parent *dentry
// name is the name of this dentry in its parent. If this dentry is a
// filesystem root, name is the empty string. name is protected by
// filesystem.renameMu.
name string
// inoKey is used to identify this dentry's inode.
inoKey inoKey
// controlFDLisa is used by lisafs to perform path based operations on this
// dentry.
//
// if !controlFD.Ok(), this dentry represents a synthetic file, i.e. a
// file that does not exist on the remote filesystem. As of this writing, the
// only files that can be synthetic are sockets, pipes, and directories.
controlFDLisa lisafs.ClientFD `state:"nosave"`
// If deleted is non-zero, the file represented by this dentry has been
// deleted is accessed using atomic memory operations.
deleted atomicbitops.Uint32
// cachingMu is used to synchronize concurrent dentry caching attempts on
// this dentry.
cachingMu sync.Mutex `state:"nosave"`
// If cached is true, this dentry is part of filesystem.dentryCache. cached
// is protected by cachingMu.
cached bool
// cacheEntry links dentry into filesystem.dentryCache.dentries. It is
// protected by filesystem.dentryCache.mu.
cacheEntry dentryListElem
// syncableListEntry links dentry into filesystem.syncableDentries. It is
// protected by filesystem.syncMu.
syncableListEntry dentryListElem
dirMu sync.Mutex `state:"nosave"`
// If this dentry represents a directory, children contains:
//
// - Mappings of child filenames to dentries representing those children.
//
// - Mappings of child filenames that are known not to exist to nil
// dentries (only if InteropModeShared is not in effect and the directory
// is not synthetic).
//
// children is protected by dirMu.
children map[string]*dentry
// If this dentry represents a directory, negativeChildrenCache cache
// names of negative children, negativeChildrenCache is protected by dirMu.
negativeChildrenCache stringFixedCache
// If this dentry represents a directory, negativeChildren is the number
// of negative children cached in dentry.children. negativeChildren is
// protected by dirMu.
negativeChildren int
// If this dentry represents a directory, syntheticChildren is the number
// of child dentries for which dentry.isSynthetic() == true.
// syntheticChildren is protected by dirMu.
syntheticChildren int
// If this dentry represents a directory,
// dentry.cachedMetadataAuthoritative() == true, and dirents is not nil, it
// is a cache of all entries in the directory, in the order they were
// returned by the server. childrenSet just stores the `Name` field of all
// dirents in a set for fast query. dirents and childrenSet are protected by
// dirMu and share the same lifecycle.
dirents []vfs.Dirent
childrenSet map[string]struct{}
// Cached metadata; protected by metadataMu.
// To access:
// - In situations where consistency is not required (like stat), these
// can be accessed using atomic operations only (without locking).
// - Lock metadataMu and can access without atomic operations.
// To mutate:
// - Lock metadataMu and use atomic operations to update because we might
// have atomic readers that don't hold the lock.
metadataMu sync.Mutex `state:"nosave"`
ino uint64 // immutable
mode atomicbitops.Uint32 // type is immutable, perms are mutable
uid atomicbitops.Uint32 // auth.KUID, but stored as raw uint32 for sync/atomic
gid atomicbitops.Uint32 // auth.KGID, but ...
blockSize atomicbitops.Uint32 // 0 if unknown
// Timestamps, all nsecs from the Unix epoch.
atime atomicbitops.Int64
mtime atomicbitops.Int64
ctime atomicbitops.Int64
btime atomicbitops.Int64
// File size, which differs from other metadata in two ways:
//
// - We make a best-effort attempt to keep it up to date even if
// !dentry.cachedMetadataAuthoritative() for the sake of O_APPEND writes.
//
// - size is protected by both metadataMu and dataMu (i.e. both must be
// locked to mutate it; locking either is sufficient to access it).
size atomicbitops.Uint64
// If this dentry does not represent a synthetic file, deleted is 0, and
// atimeDirty/mtimeDirty are non-zero, atime/mtime may have diverged from the
// remote file's timestamps, which should be updated when this dentry is
// evicted.
atimeDirty atomicbitops.Uint32
mtimeDirty atomicbitops.Uint32
// nlink counts the number of hard links to this dentry. It's updated and
// accessed using atomic operations. It's not protected by metadataMu like the
// other metadata fields.
nlink atomicbitops.Uint32
mapsMu sync.Mutex `state:"nosave"`
// If this dentry represents a regular file, mappings tracks mappings of
// the file into memmap.MappingSpaces. mappings is protected by mapsMu.
mappings memmap.MappingSet
// - If this dentry represents a regular file or directory, readFDLisa is
// a LISAFS FD used for reads by all regularFileFDs/directoryFDs
// representing this dentry, and readFD (if not -1) is a host FD
// equivalent to readFDLisa used as a faster alternative.
//
// - If this dentry represents a regular file, writeFDLisa is the LISAFS FD
// used for writes by all regularFileFDs representing this dentry, and
// writeFD (if not -1) is a host FD equivalent to writeFDLisa used as a
// faster alternative.
//
// - If this dentry represents a regular file, mmapFD is the host FD used
// for memory mappings. If mmapFD is -1, no such FD is available, and the
// internal page cache implementation is used for memory mappings instead.
//
// These fields are protected by handleMu. readFD, writeFD, and mmapFD are
// additionally written using atomic memory operations, allowing them to be
// read (albeit racily) with atomic.LoadInt32() without locking handleMu.
//
// readFDLisa and writeFDLisa may or may not represent the same LISAFS FD.
// Once either transitions from closed (Ok() == false) to open
// (Ok() == true), it may be mutated with handleMu locked, but cannot
// be closed until the dentry is destroyed.
//
// readFD and writeFD may or may not be the same file descriptor. mmapFD is
// always either -1 or equal to readFD; if writeFDLisa.Ok() (the file has
// been opened for writing), it is additionally either -1 or equal to
// writeFD.
handleMu sync.RWMutex `state:"nosave"`
readFDLisa lisafs.ClientFD `state:"nosave"`
writeFDLisa lisafs.ClientFD `state:"nosave"`
readFD atomicbitops.Int32 `state:"nosave"`
writeFD atomicbitops.Int32 `state:"nosave"`
mmapFD atomicbitops.Int32 `state:"nosave"`
dataMu sync.RWMutex `state:"nosave"`
// If this dentry represents a regular file that is client-cached, cache
// maps offsets into the cached file to offsets into
// filesystem.mfp.MemoryFile() that store the file's data. cache is
// protected by dataMu.
cache fsutil.FileRangeSet
// If this dentry represents a regular file that is client-cached, dirty
// tracks dirty segments in cache. dirty is protected by dataMu.
dirty fsutil.DirtySet
// pf implements platform.File for mappings of hostFD.
pf dentryPlatformFile
// If this dentry represents a symbolic link, InteropModeShared is not in
// effect, and haveTarget is true, target is the symlink target. haveTarget
// and target are protected by dataMu.
haveTarget bool
target string
// If this dentry represents a synthetic socket file, endpoint is the
// transport endpoint bound to this file.
endpoint transport.BoundEndpoint
// If this dentry represents a synthetic named pipe, pipe is the pipe
// endpoint bound to this file.
pipe *pipe.VFSPipe
locks vfs.FileLocks
// Inotify watches for this dentry.
//
// Note that inotify may behave unexpectedly in the presence of hard links,
// because dentries corresponding to the same file have separate inotify
// watches when they should share the same set. This is the case because it is
// impossible for us to know for sure whether two dentries correspond to the
// same underlying file (see the gofer filesystem section fo vfs/inotify.md for
// a more in-depth discussion on this matter).
watches vfs.Watches
}
// +stateify savable
type stringListElem struct {
// str is the string that this elem represents.
str string
stringEntry
}
// +stateify savable
type dentryListElem struct {
// d is the dentry that this elem represents.
d *dentry
dentryEntry
}
func (fs *filesystem) newDentry(ctx context.Context, ino *lisafs.Inode) (*dentry, error) {
if ino.Stat.Mask&linux.STATX_TYPE == 0 {
ctx.Warningf("can't create gofer.dentry without file type")
return nil, linuxerr.EIO
}
if ino.Stat.Mode&linux.FileTypeMask == linux.ModeRegular && ino.Stat.Mask&linux.STATX_SIZE == 0 {
ctx.Warningf("can't create regular file gofer.dentry without file size")
return nil, linuxerr.EIO
}
inoKey := inoKeyFromStat(&ino.Stat)
d := &dentry{
fs: fs,
inoKey: inoKey,
ino: fs.inoFromKey(inoKey),
mode: atomicbitops.FromUint32(uint32(ino.Stat.Mode)),
uid: atomicbitops.FromUint32(uint32(fs.opts.dfltuid)),
gid: atomicbitops.FromUint32(uint32(fs.opts.dfltgid)),
blockSize: atomicbitops.FromUint32(hostarch.PageSize),
readFD: atomicbitops.FromInt32(-1),
writeFD: atomicbitops.FromInt32(-1),
mmapFD: atomicbitops.FromInt32(-1),
controlFDLisa: fs.client.NewFD(ino.ControlFD),
}
d.pf.dentry = d
d.cacheEntry.d = d
d.syncableListEntry.d = d
if ino.Stat.Mask&linux.STATX_UID != 0 {
d.uid = atomicbitops.FromUint32(dentryUID(lisafs.UID(ino.Stat.UID)))
}
if ino.Stat.Mask&linux.STATX_GID != 0 {
d.gid = atomicbitops.FromUint32(dentryGID(lisafs.GID(ino.Stat.GID)))
}
if ino.Stat.Mask&linux.STATX_SIZE != 0 {
d.size = atomicbitops.FromUint64(ino.Stat.Size)
}
if ino.Stat.Blksize != 0 {
d.blockSize = atomicbitops.FromUint32(ino.Stat.Blksize)
}
if ino.Stat.Mask&linux.STATX_ATIME != 0 {
d.atime = atomicbitops.FromInt64(dentryTimestamp(ino.Stat.Atime))
} else {
d.atime = atomicbitops.FromInt64(fs.clock.Now().Nanoseconds())
}
if ino.Stat.Mask&linux.STATX_MTIME != 0 {
d.mtime = atomicbitops.FromInt64(dentryTimestamp(ino.Stat.Mtime))
} else {
d.mtime = atomicbitops.FromInt64(fs.clock.Now().Nanoseconds())
}
if ino.Stat.Mask&linux.STATX_CTIME != 0 {
d.ctime = atomicbitops.FromInt64(dentryTimestamp(ino.Stat.Ctime))
} else {
// Approximate ctime with mtime if ctime isn't available.
d.ctime = atomicbitops.FromInt64(d.mtime.Load())
}
if ino.Stat.Mask&linux.STATX_BTIME != 0 {
d.btime = atomicbitops.FromInt64(dentryTimestamp(ino.Stat.Btime))
}
if ino.Stat.Mask&linux.STATX_NLINK != 0 {
d.nlink = atomicbitops.FromUint32(ino.Stat.Nlink)
} else {
if ino.Stat.Mode&linux.FileTypeMask == linux.ModeDirectory {
d.nlink = atomicbitops.FromUint32(2)
} else {
d.nlink = atomicbitops.FromUint32(1)
}
}
d.vfsd.Init(d)
refs.Register(d)
fs.syncMu.Lock()
fs.syncableDentries.PushBack(&d.syncableListEntry)
fs.syncMu.Unlock()
return d, nil
}
func (fs *filesystem) inoFromKey(key inoKey) uint64 {
fs.inoMu.Lock()
defer fs.inoMu.Unlock()
if ino, ok := fs.inoByKey[key]; ok {
return ino
}
ino := fs.nextIno()
fs.inoByKey[key] = ino
return ino
}
func (fs *filesystem) nextIno() uint64 {
return fs.lastIno.Add(1)
}
func (d *dentry) isSynthetic() bool {
return !d.isControlFileOk()
}
func (d *dentry) cachedMetadataAuthoritative() bool {
return d.fs.opts.interop != InteropModeShared || d.isSynthetic()
}
// updateMetadataFromStatLocked is called to update d's metadata after an update
// from the remote filesystem.
// Precondition: d.metadataMu must be locked.
// +checklocks:d.metadataMu
func (d *dentry) updateMetadataFromStatLocked(stat *linux.Statx) {
if stat.Mask&linux.STATX_TYPE != 0 {
if got, want := stat.Mode&linux.FileTypeMask, d.fileType(); uint32(got) != want {
panic(fmt.Sprintf("gofer.dentry file type changed from %#o to %#o", want, got))
}
}
if stat.Mask&linux.STATX_MODE != 0 {
d.mode.Store(uint32(stat.Mode))
}
if stat.Mask&linux.STATX_UID != 0 {
d.uid.Store(dentryUID(lisafs.UID(stat.UID)))
}
if stat.Mask&linux.STATX_GID != 0 {
d.gid.Store(dentryGID(lisafs.GID(stat.GID)))
}
if stat.Blksize != 0 {
d.blockSize.Store(stat.Blksize)
}
// Don't override newer client-defined timestamps with old server-defined
// ones.
if stat.Mask&linux.STATX_ATIME != 0 && d.atimeDirty.Load() == 0 {
d.atime.Store(dentryTimestamp(stat.Atime))
}
if stat.Mask&linux.STATX_MTIME != 0 && d.mtimeDirty.Load() == 0 {
d.mtime.Store(dentryTimestamp(stat.Mtime))
}
if stat.Mask&linux.STATX_CTIME != 0 {
d.ctime.Store(dentryTimestamp(stat.Ctime))
}
if stat.Mask&linux.STATX_BTIME != 0 {
d.btime.Store(dentryTimestamp(stat.Btime))
}
if stat.Mask&linux.STATX_NLINK != 0 {
d.nlink.Store(stat.Nlink)
}
if stat.Mask&linux.STATX_SIZE != 0 {
d.updateSizeLocked(stat.Size)
}
}
// Preconditions: !d.isSynthetic().
// Preconditions: d.metadataMu is locked.
// +checklocks:d.metadataMu
func (d *dentry) refreshSizeLocked(ctx context.Context) error {
d.handleMu.RLock()
// Can use RacyLoad() because handleMu is locked.
if d.writeFD.RacyLoad() < 0 {
d.handleMu.RUnlock()
// Ask the gofer if we don't have a host FD.
return d.updateMetadataLocked(ctx, nil)
}
var stat unix.Statx_t
// Can use RacyLoad() because handleMu is locked.
err := unix.Statx(int(d.writeFD.RacyLoad()), "", unix.AT_EMPTY_PATH, unix.STATX_SIZE, &stat)
d.handleMu.RUnlock() // must be released before updateSizeLocked()
if err != nil {
return err
}
d.updateSizeLocked(stat.Size)
return nil
}
// Preconditions: !d.isSynthetic().
func (d *dentry) updateMetadata(ctx context.Context, fd *lisafs.ClientFD) error {
// d.metadataMu must be locked *before* we getAttr so that we do not end up
// updating stale attributes in d.updateFromP9AttrsLocked().
d.metadataMu.Lock()
defer d.metadataMu.Unlock()
return d.updateMetadataLocked(ctx, fd)
}
// Preconditions:
// - !d.isSynthetic().
// - d.metadataMu is locked.
//
// +checklocks:d.metadataMu
func (d *dentry) updateMetadataLocked(ctx context.Context, fd *lisafs.ClientFD) error {
handleMuRLocked := false
if fd == nil {
// Use open FDs in preferenece to the control FD. This may be significantly
// more efficient in some implementations. Prefer a writable FD over a
// readable one since some filesystem implementations may update a writable
// FD's metadata after writes, without making metadata updates immediately
// visible to read-only FDs representing the same file.
d.handleMu.RLock()
switch {
case d.writeFDLisa.Ok():
fd = &d.writeFDLisa
handleMuRLocked = true
case d.readFDLisa.Ok():
fd = &d.readFDLisa
handleMuRLocked = true
default:
fd = &d.controlFDLisa
d.handleMu.RUnlock()
}
}
var stat linux.Statx
err := fd.StatTo(ctx, &stat)
if handleMuRLocked {
// handleMu must be released before updateMetadataFromStatLocked().
d.handleMu.RUnlock() // +checklocksforce: complex case.
}
if err != nil {
return err
}
d.updateMetadataFromStatLocked(&stat)
return nil
}
func (d *dentry) fileType() uint32 {
return d.mode.Load() & linux.S_IFMT
}
func (d *dentry) statTo(stat *linux.Statx) {
stat.Mask = linux.STATX_TYPE | linux.STATX_MODE | linux.STATX_NLINK | linux.STATX_UID | linux.STATX_GID | linux.STATX_ATIME | linux.STATX_MTIME | linux.STATX_CTIME | linux.STATX_INO | linux.STATX_SIZE | linux.STATX_BLOCKS | linux.STATX_BTIME
stat.Blksize = d.blockSize.Load()
stat.Nlink = d.nlink.Load()
if stat.Nlink == 0 {
// The remote filesystem doesn't support link count; just make
// something up. This is consistent with Linux, where
// fs/inode.c:inode_init_always() initializes link count to 1, and
// fs/9p/vfs_inode_dotl.c:v9fs_stat2inode_dotl() doesn't touch it if
// it's not provided by the remote filesystem.
stat.Nlink = 1
}
stat.UID = d.uid.Load()
stat.GID = d.gid.Load()
stat.Mode = uint16(d.mode.Load())
stat.Ino = uint64(d.ino)
stat.Size = d.size.Load()
// This is consistent with regularFileFD.Seek(), which treats regular files
// as having no holes.
stat.Blocks = (stat.Size + 511) / 512
stat.Atime = linux.NsecToStatxTimestamp(d.atime.Load())
stat.Btime = linux.NsecToStatxTimestamp(d.btime.Load())
stat.Ctime = linux.NsecToStatxTimestamp(d.ctime.Load())
stat.Mtime = linux.NsecToStatxTimestamp(d.mtime.Load())
stat.DevMajor = linux.UNNAMED_MAJOR
stat.DevMinor = d.fs.devMinor
}
func (d *dentry) setStat(ctx context.Context, creds *auth.Credentials, opts *vfs.SetStatOptions, mnt *vfs.Mount) error {
stat := &opts.Stat
if stat.Mask == 0 {
return nil
}
if stat.Mask&^(linux.STATX_MODE|linux.STATX_UID|linux.STATX_GID|linux.STATX_ATIME|linux.STATX_MTIME|linux.STATX_SIZE) != 0 {
return linuxerr.EPERM
}
mode := linux.FileMode(d.mode.Load())
if err := vfs.CheckSetStat(ctx, creds, opts, mode, auth.KUID(d.uid.Load()), auth.KGID(d.gid.Load())); err != nil {
return err
}
if err := mnt.CheckBeginWrite(); err != nil {
return err
}
defer mnt.EndWrite()
if stat.Mask&linux.STATX_SIZE != 0 {
// Reject attempts to truncate files other than regular files, since
// filesystem implementations may return the wrong errno.
switch mode.FileType() {
case linux.S_IFREG:
// ok
case linux.S_IFDIR:
return linuxerr.EISDIR
default:
return linuxerr.EINVAL
}
}
var now int64
if d.cachedMetadataAuthoritative() {
// Truncate updates mtime.
if stat.Mask&(linux.STATX_SIZE|linux.STATX_MTIME) == linux.STATX_SIZE {
stat.Mask |= linux.STATX_MTIME
stat.Mtime = linux.StatxTimestamp{
Nsec: linux.UTIME_NOW,
}
}
// Use client clocks for timestamps.
now = d.fs.clock.Now().Nanoseconds()
if stat.Mask&linux.STATX_ATIME != 0 && stat.Atime.Nsec == linux.UTIME_NOW {
stat.Atime = linux.NsecToStatxTimestamp(now)
}
if stat.Mask&linux.STATX_MTIME != 0 && stat.Mtime.Nsec == linux.UTIME_NOW {
stat.Mtime = linux.NsecToStatxTimestamp(now)
}
}
d.metadataMu.Lock()
defer d.metadataMu.Unlock()
// As with Linux, if the UID, GID, or file size is changing, we have to
// clear permission bits. Note that when set, clearSGID may cause
// permissions to be updated.
clearSGID := (stat.Mask&linux.STATX_UID != 0 && stat.UID != d.uid.Load()) ||
(stat.Mask&linux.STATX_GID != 0 && stat.GID != d.gid.Load()) ||
stat.Mask&linux.STATX_SIZE != 0
if clearSGID {
if stat.Mask&linux.STATX_MODE != 0 {
stat.Mode = uint16(vfs.ClearSUIDAndSGID(uint32(stat.Mode)))
} else {
oldMode := d.mode.Load()
if updatedMode := vfs.ClearSUIDAndSGID(oldMode); updatedMode != oldMode {
stat.Mode = uint16(updatedMode)
stat.Mask |= linux.STATX_MODE
}
}
}
// failureMask indicates which attributes could not be set on the remote
// filesystem. p9 returns an error if any of the attributes could not be set
// but that leads to inconsistency as the server could have set a few
// attributes successfully but a later failure will cause the successful ones
// to not be updated in the dentry cache.
var failureMask uint32
var failureErr error
if !d.isSynthetic() {
if stat.Mask != 0 {
if stat.Mask&linux.STATX_SIZE != 0 {
// d.dataMu must be held around the update to both the remote
// file's size and d.size to serialize with writeback (which
// might otherwise write data back up to the old d.size after
// the remote file has been truncated).
d.dataMu.Lock()
}
var err error
failureMask, failureErr, err = d.controlFDLisa.SetStat(ctx, stat)
if err != nil {
if stat.Mask&linux.STATX_SIZE != 0 {
d.dataMu.Unlock() // +checklocksforce: locked conditionally above
}
return err
}
if stat.Mask&linux.STATX_SIZE != 0 {
if failureMask&linux.STATX_SIZE == 0 {
// d.size should be kept up to date, and privatized
// copy-on-write mappings of truncated pages need to be
// invalidated, even if InteropModeShared is in effect.
d.updateSizeAndUnlockDataMuLocked(stat.Size) // +checklocksforce: locked conditionally above
} else {
d.dataMu.Unlock() // +checklocksforce: locked conditionally above
}
}
}
if d.fs.opts.interop == InteropModeShared {
// There's no point to updating d's metadata in this case since
// it'll be overwritten by revalidation before the next time it's
// used anyway. (InteropModeShared inhibits client caching of
// regular file data, so there's no cache to truncate either.)
return nil
}
}
if stat.Mask&linux.STATX_MODE != 0 && failureMask&linux.STATX_MODE == 0 {
d.mode.Store(d.fileType() | uint32(stat.Mode))
}
if stat.Mask&linux.STATX_UID != 0 && failureMask&linux.STATX_UID == 0 {
d.uid.Store(stat.UID)
}
if stat.Mask&linux.STATX_GID != 0 && failureMask&linux.STATX_GID == 0 {
d.gid.Store(stat.GID)
}
// Note that stat.Atime.Nsec and stat.Mtime.Nsec can't be UTIME_NOW because
// if d.cachedMetadataAuthoritative() then we converted stat.Atime and
// stat.Mtime to client-local timestamps above, and if
// !d.cachedMetadataAuthoritative() then we returned after calling
// d.file.setAttr(). For the same reason, now must have been initialized.
if stat.Mask&linux.STATX_ATIME != 0 && failureMask&linux.STATX_ATIME == 0 {
d.atime.Store(stat.Atime.ToNsec())
d.atimeDirty.Store(0)
}
if stat.Mask&linux.STATX_MTIME != 0 && failureMask&linux.STATX_MTIME == 0 {
d.mtime.Store(stat.Mtime.ToNsec())
d.mtimeDirty.Store(0)
}
d.ctime.Store(now)
if failureMask != 0 {
// Setting some attribute failed on the remote filesystem.
return failureErr
}
return nil
}
// Preconditions:
// - filesystem.renameMu must be locked.
// - d.dirMu must be locked.
// - d.isDir().
func (d *dentry) mknodLocked(ctx context.Context, name string, creds *auth.Credentials, opts vfs.MknodOptions, ds **[]*dentry) error {
if _, ok := opts.Endpoint.(transport.HostBoundEndpoint); !ok {
childInode, err := d.controlFDLisa.MknodAt(ctx, name, opts.Mode, lisafs.UID(creds.EffectiveKUID), lisafs.GID(creds.EffectiveKGID), opts.DevMinor, opts.DevMajor)
if err != nil {
return err
}
return d.insertCreatedChildLocked(ctx, &childInode, name, nil, ds)
}
// This mknod(2) is coming from unix bind(2), as opts.Endpoint is set.
sockType := opts.Endpoint.(transport.Endpoint).Type()
childInode, boundSocketFD, err := d.controlFDLisa.BindAt(ctx, sockType, name, opts.Mode, lisafs.UID(creds.EffectiveKUID), lisafs.GID(creds.EffectiveKGID))
if err != nil {
return err
}
hbep := opts.Endpoint.(transport.HostBoundEndpoint)
if err := hbep.SetBoundSocketFD(boundSocketFD); err != nil {
boundSocketFD.Close(ctx)
if err := d.controlFDLisa.UnlinkAt(ctx, name, 0 /* flags */); err != nil {
log.Warningf("failed to clean up socket which was created by BindAt RPC: %v", err)
}
d.fs.client.CloseFD(ctx, childInode.ControlFD, false /* flush */)
return err
}
if err := d.insertCreatedChildLocked(ctx, &childInode, name, func(child *dentry) {
// Set the endpoint on the newly created child dentry.
child.endpoint = opts.Endpoint
}, ds); err != nil {
hbep.ResetBoundSocketFD(ctx)
return err
}
return nil
}
// doAllocate performs an allocate operation on d. Note that d.metadataMu will
// be held when allocate is called.
func (d *dentry) doAllocate(ctx context.Context, offset, length uint64, allocate func() error) error {
d.metadataMu.Lock()
defer d.metadataMu.Unlock()
// Allocating a smaller size is a noop.
size := offset + length
if d.cachedMetadataAuthoritative() && size <= d.size.RacyLoad() {
return nil
}
err := allocate()
if err != nil {
return err
}
d.updateSizeLocked(size)
if d.cachedMetadataAuthoritative() {
d.touchCMtimeLocked()
}
return nil
}
// Preconditions: d.metadataMu must be locked.
func (d *dentry) updateSizeLocked(newSize uint64) {
d.dataMu.Lock()
d.updateSizeAndUnlockDataMuLocked(newSize)
}
// Preconditions: d.metadataMu and d.dataMu must be locked.
//
// Postconditions: d.dataMu is unlocked.
// +checklocksrelease:d.dataMu
func (d *dentry) updateSizeAndUnlockDataMuLocked(newSize uint64) {
oldSize := d.size.RacyLoad()
d.size.Store(newSize)
// d.dataMu must be unlocked to lock d.mapsMu and invalidate mappings
// below. This allows concurrent calls to Read/Translate/etc. These
// functions synchronize with truncation by refusing to use cache
// contents beyond the new d.size. (We are still holding d.metadataMu,
// so we can't race with Write or another truncate.)
d.dataMu.Unlock()
if newSize < oldSize {
oldpgend, _ := hostarch.PageRoundUp(oldSize)
newpgend, _ := hostarch.PageRoundUp(newSize)
if oldpgend != newpgend {
d.mapsMu.Lock()
d.mappings.Invalidate(memmap.MappableRange{newpgend, oldpgend}, memmap.InvalidateOpts{
// Compare Linux's mm/truncate.c:truncate_setsize() =>
// truncate_pagecache() =>
// mm/memory.c:unmap_mapping_range(evencows=1).
InvalidatePrivate: true,
})
d.mapsMu.Unlock()
}
// We are now guaranteed that there are no translations of
// truncated pages, and can remove them from the cache. Since
// truncated pages have been removed from the remote file, they
// should be dropped without being written back.
d.dataMu.Lock()
d.cache.Truncate(newSize, d.fs.mfp.MemoryFile())
d.dirty.KeepClean(memmap.MappableRange{newSize, oldpgend})
d.dataMu.Unlock()
}
}
func (d *dentry) checkPermissions(creds *auth.Credentials, ats vfs.AccessTypes) error {
return vfs.GenericCheckPermissions(creds, ats, linux.FileMode(d.mode.Load()), auth.KUID(d.uid.Load()), auth.KGID(d.gid.Load()))
}
func (d *dentry) checkXattrPermissions(creds *auth.Credentials, name string, ats vfs.AccessTypes) error {
// Deny access to the "security" and "system" namespaces since applications
// may expect these to affect kernel behavior in unimplemented ways
// (b/148380782). Allow all other extended attributes to be passed through
// to the remote filesystem. This is inconsistent with Linux's 9p client,
// but consistent with other filesystems (e.g. FUSE).
//
// NOTE(b/202533394): Also disallow "trusted" namespace for now. This is
// consistent with the VFS1 gofer client.
if strings.HasPrefix(name, linux.XATTR_SECURITY_PREFIX) || strings.HasPrefix(name, linux.XATTR_SYSTEM_PREFIX) || strings.HasPrefix(name, linux.XATTR_TRUSTED_PREFIX) {
return linuxerr.EOPNOTSUPP
}
mode := linux.FileMode(d.mode.Load())
kuid := auth.KUID(d.uid.Load())
kgid := auth.KGID(d.gid.Load())
if err := vfs.GenericCheckPermissions(creds, ats, mode, kuid, kgid); err != nil {
return err
}
return vfs.CheckXattrPermissions(creds, ats, mode, kuid, name)
}
func (d *dentry) mayDelete(creds *auth.Credentials, child *dentry) error {
return vfs.CheckDeleteSticky(
creds,
linux.FileMode(d.mode.Load()),
auth.KUID(d.uid.Load()),
auth.KUID(child.uid.Load()),
auth.KGID(child.gid.Load()),
)
}
func dentryUID(uid lisafs.UID) uint32 {
if !uid.Ok() {
return uint32(auth.OverflowUID)
}
return uint32(uid)
}
func dentryGID(gid lisafs.GID) uint32 {
if !gid.Ok() {
return uint32(auth.OverflowGID)
}
return uint32(gid)
}
// IncRef implements vfs.DentryImpl.IncRef.
func (d *dentry) IncRef() {
// d.refs may be 0 if d.fs.renameMu is locked, which serializes against
// d.checkCachingLocked().
r := d.refs.Add(1)
if d.LogRefs() {
refs.LogIncRef(d, r)
}
}
// TryIncRef implements vfs.DentryImpl.TryIncRef.
func (d *dentry) TryIncRef() bool {
for {
r := d.refs.Load()
if r <= 0 {
return false
}
if d.refs.CompareAndSwap(r, r+1) {
if d.LogRefs() {
refs.LogTryIncRef(d, r+1)
}
return true
}
}
}
// DecRef implements vfs.DentryImpl.DecRef.
func (d *dentry) DecRef(ctx context.Context) {
if d.decRefNoCaching() == 0 {
d.checkCachingLocked(ctx, false /* renameMuWriteLocked */)
}
}
// decRefNoCaching decrements d's reference count without calling
// d.checkCachingLocked, even if d's reference count reaches 0; callers are
// responsible for ensuring that d.checkCachingLocked will be called later.
func (d *dentry) decRefNoCaching() int64 {
r := d.refs.Add(-1)
if d.LogRefs() {
refs.LogDecRef(d, r)
}
if r < 0 {
panic("gofer.dentry.decRefNoCaching() called without holding a reference")
}
return r
}
// RefType implements refs.CheckedObject.Type.
func (d *dentry) RefType() string {
return "gofer.dentry"
}
// LeakMessage implements refs.CheckedObject.LeakMessage.
func (d *dentry) LeakMessage() string {
return fmt.Sprintf("[gofer.dentry %p] reference count of %d instead of -1", d, d.refs.Load())
}
// LogRefs implements refs.CheckedObject.LogRefs.
//
// This should only be set to true for debugging purposes, as it can generate an
// extremely large amount of output and drastically degrade performance.
func (d *dentry) LogRefs() bool {
return false
}
// InotifyWithParent implements vfs.DentryImpl.InotifyWithParent.
func (d *dentry) InotifyWithParent(ctx context.Context, events, cookie uint32, et vfs.EventType) {
if d.isDir() {
events |= linux.IN_ISDIR
}
d.fs.renameMu.RLock()
// The ordering below is important, Linux always notifies the parent first.
if d.parent != nil {
d.parent.watches.Notify(ctx, d.name, events, cookie, et, d.isDeleted())
}
d.watches.Notify(ctx, "", events, cookie, et, d.isDeleted())
d.fs.renameMu.RUnlock()
}
// Watches implements vfs.DentryImpl.Watches.
func (d *dentry) Watches() *vfs.Watches {
return &d.watches
}
// OnZeroWatches implements vfs.DentryImpl.OnZeroWatches.
//
// If no watches are left on this dentry and it has no references, cache it.
func (d *dentry) OnZeroWatches(ctx context.Context) {
d.checkCachingLocked(ctx, false /* renameMuWriteLocked */)
}
// checkCachingLocked should be called after d's reference count becomes 0 or
// it becomes disowned.
//
// For performance, checkCachingLocked can also be called after d's reference
// count becomes non-zero, so that d can be removed from the LRU cache. This
// may help in reducing the size of the cache and hence reduce evictions. Note
// that this is not necessary for correctness.
//
// It may be called on a destroyed dentry. For example,
// renameMu[R]UnlockAndCheckCaching may call checkCachingLocked multiple times
// for the same dentry when the dentry is visited more than once in the same
// operation. One of the calls may destroy the dentry, so subsequent calls will
// do nothing.
//
// Preconditions: d.fs.renameMu must be locked for writing if
// renameMuWriteLocked is true; it may be temporarily unlocked.
func (d *dentry) checkCachingLocked(ctx context.Context, renameMuWriteLocked bool) {
d.cachingMu.Lock()
refs := d.refs.Load()
if refs == -1 {
// Dentry has already been destroyed.
d.cachingMu.Unlock()
return
}
if refs > 0 {
// fs.dentryCache.dentries is permitted to contain dentries with non-zero
// refs, which are skipped by fs.evictCachedDentryLocked() upon reaching
// the end of the LRU. But it is still beneficial to remove d from the
// cache as we are already holding d.cachingMu. Keeping a cleaner cache
// also reduces the number of evictions (which is expensive as it acquires
// fs.renameMu).
d.removeFromCacheLocked()
d.cachingMu.Unlock()
return
}
// Deleted and invalidated dentries with zero references are no longer
// reachable by path resolution and should be dropped immediately.
if d.vfsd.IsDead() {
d.removeFromCacheLocked()
d.cachingMu.Unlock()
if !renameMuWriteLocked {
// Need to lock d.fs.renameMu for writing as needed by d.destroyLocked().
d.fs.renameMu.Lock()
defer d.fs.renameMu.Unlock()
// Now that renameMu is locked for writing, no more refs can be taken on
// d because path resolution requires renameMu for reading at least.
if d.refs.Load() != 0 {
// Destroy d only if its ref is still 0. If not, either someone took a
// ref on it or it got destroyed before fs.renameMu could be acquired.
return
}
}
if d.isDeleted() {
d.watches.HandleDeletion(ctx)
}
d.destroyLocked(ctx) // +checklocksforce: renameMu must be acquired at this point.
return
}
if d.vfsd.IsEvictable() {
d.cachingMu.Unlock()
// Attempt to evict.
if renameMuWriteLocked {
d.evictLocked(ctx) // +checklocksforce: renameMu is locked in this case.
return
}
d.evict(ctx)
return
}
// If d still has inotify watches and it is not deleted or invalidated, it
// can't be evicted. Otherwise, we will lose its watches, even if a new
// dentry is created for the same file in the future. Note that the size of
// d.watches cannot concurrently transition from zero to non-zero, because
// adding a watch requires holding a reference on d.
if d.watches.Size() > 0 {
// As in the refs > 0 case, removing d is beneficial.
d.removeFromCacheLocked()
d.cachingMu.Unlock()
return
}
if d.fs.released.Load() != 0 {
d.cachingMu.Unlock()
if !renameMuWriteLocked {
// Need to lock d.fs.renameMu to access d.parent. Lock it for writing as
// needed by d.destroyLocked() later.
d.fs.renameMu.Lock()
defer d.fs.renameMu.Unlock()
}
if d.parent != nil {
d.parent.dirMu.Lock()
delete(d.parent.children, d.name)
d.parent.dirMu.Unlock()
}
d.destroyLocked(ctx) // +checklocksforce: see above.
return
}
d.fs.dentryCache.mu.Lock()
// If d is already cached, just move it to the front of the LRU.
if d.cached {
d.fs.dentryCache.dentries.Remove(&d.cacheEntry)
d.fs.dentryCache.dentries.PushFront(&d.cacheEntry)
d.fs.dentryCache.mu.Unlock()
d.cachingMu.Unlock()
return
}
// Cache the dentry, then evict the least recently used cached dentry if
// the cache becomes over-full.
d.fs.dentryCache.dentries.PushFront(&d.cacheEntry)
d.fs.dentryCache.dentriesLen++
d.cached = true
shouldEvict := d.fs.dentryCache.dentriesLen > d.fs.dentryCache.maxCachedDentries
d.fs.dentryCache.mu.Unlock()
d.cachingMu.Unlock()
if shouldEvict {
if !renameMuWriteLocked {
// Need to lock d.fs.renameMu for writing as needed by
// d.evictCachedDentryLocked().
d.fs.renameMu.Lock()
defer d.fs.renameMu.Unlock()
}
d.fs.evictCachedDentryLocked(ctx) // +checklocksforce: see above.
}
}
// Preconditions: d.cachingMu must be locked.
func (d *dentry) removeFromCacheLocked() {
if d.cached {
d.fs.dentryCache.mu.Lock()
d.fs.dentryCache.dentries.Remove(&d.cacheEntry)
d.fs.dentryCache.dentriesLen--
d.fs.dentryCache.mu.Unlock()
d.cached = false
}
}
// Precondition: fs.renameMu must be locked for writing; it may be temporarily
// unlocked.
// +checklocks:fs.renameMu
func (fs *filesystem) evictAllCachedDentriesLocked(ctx context.Context) {
for fs.dentryCache.dentriesLen != 0 {
fs.evictCachedDentryLocked(ctx)
}
}
// Preconditions:
// - fs.renameMu must be locked for writing; it may be temporarily unlocked.
//
// +checklocks:fs.renameMu
func (fs *filesystem) evictCachedDentryLocked(ctx context.Context) {
fs.dentryCache.mu.Lock()
victim := fs.dentryCache.dentries.Back()
fs.dentryCache.mu.Unlock()
if victim == nil {
// fs.dentryCache.dentries may have become empty between when it was
// checked and when we locked fs.dentryCache.mu.
return
}
if victim.d.fs == fs {
victim.d.evictLocked(ctx) // +checklocksforce: owned as precondition, victim.fs == fs
return
}
// The dentry cache is shared between all gofer filesystems and the victim is
// from another filesystem. Have that filesystem do the work. We unlock
// fs.renameMu to prevent deadlock: two filesystems could otherwise wait on
// each others' renameMu.
fs.renameMu.Unlock()
defer fs.renameMu.Lock()
victim.d.evict(ctx)
}
// Preconditions:
// - d.fs.renameMu must not be locked for writing.
func (d *dentry) evict(ctx context.Context) {
d.fs.renameMu.Lock()
defer d.fs.renameMu.Unlock()
d.evictLocked(ctx)
}
// Preconditions:
// - d.fs.renameMu must be locked for writing; it may be temporarily unlocked.
//
// +checklocks:d.fs.renameMu
func (d *dentry) evictLocked(ctx context.Context) {
d.cachingMu.Lock()
d.removeFromCacheLocked()
// d.refs or d.watches.Size() may have become non-zero from an earlier path
// resolution since it was inserted into fs.dentryCache.dentries.
if d.refs.Load() != 0 || d.watches.Size() != 0 {
d.cachingMu.Unlock()
return
}
if d.parent != nil {
d.parent.dirMu.Lock()
if !d.vfsd.IsDead() {
// Note that d can't be a mount point (in any mount namespace), since VFS
// holds references on mount points.
d.fs.vfsfs.VirtualFilesystem().InvalidateDentry(ctx, &d.vfsd)
delete(d.parent.children, d.name)
// We're only deleting the dentry, not the file it
// represents, so we don't need to update
// victim parent.dirents etc.
}
d.parent.dirMu.Unlock()
}
// Safe to unlock cachingMu now that d.vfsd.IsDead(). Henceforth any
// concurrent caching attempts on d will attempt to destroy it and so will
// try to acquire fs.renameMu (which we have already acquiredd). Hence,
// fs.renameMu will synchronize the destroy attempts.
d.cachingMu.Unlock()
d.destroyLocked(ctx) // +checklocksforce: owned as precondition.
}
// destroyLocked destroys the dentry.
//
// Preconditions:
// - d.fs.renameMu must be locked for writing; it may be temporarily unlocked.
// - d.refs == 0.
// - d.parent.children[d.name] != d, i.e. d is not reachable by path traversal
// from its former parent dentry.
//
// +checklocks:d.fs.renameMu
func (d *dentry) destroyLocked(ctx context.Context) {
switch d.refs.Load() {
case 0:
// Mark the dentry destroyed.
d.refs.Store(-1)
case -1:
panic("dentry.destroyLocked() called on already destroyed dentry")
default:
panic("dentry.destroyLocked() called with references on the dentry")
}
// Allow the following to proceed without renameMu locked to improve
// scalability.
d.fs.renameMu.Unlock()
mf := d.fs.mfp.MemoryFile()
d.handleMu.Lock()
d.dataMu.Lock()
if h := d.writeHandleLocked(); h.isOpen() {
// Write dirty pages back to the remote filesystem.
if err := fsutil.SyncDirtyAll(ctx, &d.cache, &d.dirty, d.size.Load(), mf, h.writeFromBlocksAt); err != nil {
log.Warningf("gofer.dentry.destroyLocked: failed to write dirty data back: %v", err)
}
}
// Discard cached data.
if !d.cache.IsEmpty() {
mf.MarkAllUnevictable(d)
d.cache.DropAll(mf)
d.dirty.RemoveAll()
}
d.dataMu.Unlock()
if d.readFDLisa.Ok() && d.readFDLisa.ID() != d.writeFDLisa.ID() {
d.readFDLisa.Close(ctx, false /* flush */)
}
if d.writeFDLisa.Ok() {
d.writeFDLisa.Close(ctx, false /* flush */)
}
// Can use RacyLoad() because handleMu is locked.
if d.readFD.RacyLoad() >= 0 {
_ = unix.Close(int(d.readFD.RacyLoad()))
}
if d.writeFD.RacyLoad() >= 0 && d.readFD.RacyLoad() != d.writeFD.RacyLoad() {
_ = unix.Close(int(d.writeFD.RacyLoad()))
}
d.readFD = atomicbitops.FromInt32(-1)
d.writeFD = atomicbitops.FromInt32(-1)
d.mmapFD = atomicbitops.FromInt32(-1)
d.handleMu.Unlock()
if !d.isSynthetic() {
// Note that it's possible that d.atimeDirty or d.mtimeDirty are true,
// i.e. client and server timestamps may differ (because e.g. a client
// write was serviced by the page cache, and only written back to the
// remote file later). Ideally, we'd write client timestamps back to
// the remote filesystem so that timestamps for a new dentry
// instantiated for the same file would remain coherent. Unfortunately,
// this turns out to be too expensive in many cases, so for now we
// don't do this.
// Close the control FD. Propagate the Close RPCs immediately to the server
// if the dentry being destroyed is a deleted regular file. This is to
// release the disk space on remote immediately.
flushClose := d.isDeleted() && d.isRegularFile()
d.controlFDLisa.Close(ctx, flushClose)
// Remove d from the set of syncable dentries.
d.fs.syncMu.Lock()
d.fs.syncableDentries.Remove(&d.syncableListEntry)
d.fs.syncMu.Unlock()
}
d.fs.renameMu.Lock()
// Drop the reference held by d on its parent without recursively locking
// d.fs.renameMu.
if d.parent != nil && d.parent.decRefNoCaching() == 0 {
d.parent.checkCachingLocked(ctx, true /* renameMuWriteLocked */)
}
refs.Unregister(d)
}
func (d *dentry) isDeleted() bool {
return d.deleted.Load() != 0
}
func (d *dentry) setDeleted() {
d.deleted.Store(1)
}
func (d *dentry) isControlFileOk() bool {
return d.controlFDLisa.Ok()
}
func (d *dentry) isReadFileOk() bool {
return d.readFDLisa.Ok()
}
func (d *dentry) listXattr(ctx context.Context, size uint64) ([]string, error) {
if !d.isControlFileOk() {
return nil, nil
}
return d.controlFDLisa.ListXattr(ctx, size)
}
func (d *dentry) getXattr(ctx context.Context, creds *auth.Credentials, opts *vfs.GetXattrOptions) (string, error) {
if !d.isControlFileOk() {
return "", linuxerr.ENODATA
}
if err := d.checkXattrPermissions(creds, opts.Name, vfs.MayRead); err != nil {
return "", err
}
return d.controlFDLisa.GetXattr(ctx, opts.Name, opts.Size)
}
func (d *dentry) setXattr(ctx context.Context, creds *auth.Credentials, opts *vfs.SetXattrOptions) error {
if !d.isControlFileOk() {
return linuxerr.EPERM
}
if err := d.checkXattrPermissions(creds, opts.Name, vfs.MayWrite); err != nil {
return err
}
return d.controlFDLisa.SetXattr(ctx, opts.Name, opts.Value, opts.Flags)
}
func (d *dentry) removeXattr(ctx context.Context, creds *auth.Credentials, name string) error {
if !d.isControlFileOk() {
return linuxerr.EPERM
}
if err := d.checkXattrPermissions(creds, name, vfs.MayWrite); err != nil {
return err
}
return d.controlFDLisa.RemoveXattr(ctx, name)
}
// Preconditions:
// - !d.isSynthetic().
// - d.isRegularFile() || d.isDir().
func (d *dentry) ensureSharedHandle(ctx context.Context, read, write, trunc bool) error {
// O_TRUNC unconditionally requires us to obtain a new handle (opened with
// O_TRUNC).
if !trunc {
d.handleMu.RLock()
canReuseCurHandle := (!read || d.readFDLisa.Ok()) && (!write || d.writeFDLisa.Ok())
d.handleMu.RUnlock()
if canReuseCurHandle {
// Current handles are sufficient.
return nil
}
}
var fdsToCloseArr [2]int32
fdsToClose := fdsToCloseArr[:0]
invalidateTranslations := false
d.handleMu.Lock()
if (read && !d.readFDLisa.Ok()) || (write && !d.writeFDLisa.Ok()) || trunc {
// Get a new handle. If this file has been opened for both reading and
// writing, try to get a single handle that is usable for both:
//
// - Writable memory mappings of a host FD require that the host FD is
// opened for both reading and writing.
//
// - NOTE(b/141991141): Some filesystems may not ensure coherence
// between multiple handles for the same file.
openReadable := d.readFDLisa.Ok() || read
openWritable := d.writeFDLisa.Ok() || write
h, err := openHandle(ctx, d.controlFDLisa, openReadable, openWritable, trunc)
if linuxerr.Equals(linuxerr.EACCES, err) && (openReadable != read || openWritable != write) {
// It may not be possible to use a single handle for both
// reading and writing, since permissions on the file may have
// changed to e.g. disallow reading after previously being
// opened for reading. In this case, we have no choice but to
// use separate handles for reading and writing.
ctx.Debugf("gofer.dentry.ensureSharedHandle: bifurcating read/write handles for dentry %p", d)
openReadable = read
openWritable = write
h, err = openHandle(ctx, d.controlFDLisa, openReadable, openWritable, trunc)
}
if err != nil {
d.handleMu.Unlock()
return err
}
// Update d.readFD and d.writeFD
if h.fd >= 0 {
if openReadable && openWritable && (d.readFD.RacyLoad() < 0 || d.writeFD.RacyLoad() < 0 || d.readFD.RacyLoad() != d.writeFD.RacyLoad()) {
// Replace existing FDs with this one.
if d.readFD.RacyLoad() >= 0 {
// We already have a readable FD that may be in use by
// concurrent callers of d.pf.FD().
if d.fs.opts.overlayfsStaleRead {
// If overlayfsStaleRead is in effect, then the new FD
// may not be coherent with the existing one, so we
// have no choice but to switch to mappings of the new
// FD in both the application and sentry.
if err := d.pf.hostFileMapper.RegenerateMappings(int(h.fd)); err != nil {
d.handleMu.Unlock()
ctx.Warningf("gofer.dentry.ensureSharedHandle: failed to replace sentry mappings of old FD with mappings of new FD: %v", err)
h.close(ctx)
return err
}
fdsToClose = append(fdsToClose, d.readFD.RacyLoad())
invalidateTranslations = true
d.readFD.Store(h.fd)
} else {
// Otherwise, we want to avoid invalidating existing
// memmap.Translations (which is expensive); instead, use
// dup3 to make the old file descriptor refer to the new
// file description, then close the new file descriptor
// (which is no longer needed). Racing callers of d.pf.FD()
// may use the old or new file description, but this
// doesn't matter since they refer to the same file, and
// any racing mappings must be read-only.
if err := unix.Dup3(int(h.fd), int(d.readFD.RacyLoad()), unix.O_CLOEXEC); err != nil {
oldFD := d.readFD.RacyLoad()
d.handleMu.Unlock()
ctx.Warningf("gofer.dentry.ensureSharedHandle: failed to dup fd %d to fd %d: %v", h.fd, oldFD, err)
h.close(ctx)
return err
}
fdsToClose = append(fdsToClose, h.fd)
h.fd = d.readFD.RacyLoad()
}
} else {
d.readFD.Store(h.fd)
}
if d.writeFD.RacyLoad() != h.fd && d.writeFD.RacyLoad() >= 0 {
fdsToClose = append(fdsToClose, d.writeFD.RacyLoad())
}
d.writeFD.Store(h.fd)
d.mmapFD.Store(h.fd)
} else if openReadable && d.readFD.RacyLoad() < 0 {
d.readFD.Store(h.fd)
// If the file has not been opened for writing, the new FD may
// be used for read-only memory mappings. If the file was
// previously opened for reading (without an FD), then existing
// translations of the file may use the internal page cache;
// invalidate those mappings.
if !d.writeFDLisa.Ok() {
invalidateTranslations = d.readFDLisa.Ok()
d.mmapFD.Store(h.fd)
}
} else if openWritable && d.writeFD.RacyLoad() < 0 {
d.writeFD.Store(h.fd)
if d.readFD.RacyLoad() >= 0 {
// We have an existing read-only FD, but the file has just
// been opened for writing, so we need to start supporting
// writable memory mappings. However, the new FD is not
// readable, so we have no FD that can be used to create
// writable memory mappings. Switch to using the internal
// page cache.
invalidateTranslations = true
d.mmapFD.Store(-1)
}
} else {
// The new FD is not useful.
fdsToClose = append(fdsToClose, h.fd)
}
} else if openWritable && d.writeFD.RacyLoad() < 0 && d.mmapFD.RacyLoad() >= 0 {
// We have an existing read-only FD, but the file has just been
// opened for writing, so we need to start supporting writable
// memory mappings. However, we have no writable host FD. Switch to
// using the internal page cache.
invalidateTranslations = true
d.mmapFD.Store(-1)
}
// Switch to new fids/FDs.
oldReadFD := lisafs.InvalidFDID
if openReadable {
oldReadFD = d.readFDLisa.ID()
d.readFDLisa = h.fdLisa
}
oldWriteFD := lisafs.InvalidFDID
if openWritable {
oldWriteFD = d.writeFDLisa.ID()
d.writeFDLisa = h.fdLisa
}
// NOTE(b/141991141): Close old FDs before making new fids visible (by
// unlocking d.handleMu).
if oldReadFD.Ok() {
d.fs.client.CloseFD(ctx, oldReadFD, false /* flush */)
}
if oldWriteFD.Ok() && oldReadFD != oldWriteFD {
d.fs.client.CloseFD(ctx, oldWriteFD, false /* flush */)
}
}
d.handleMu.Unlock()
if invalidateTranslations {
// Invalidate application mappings that may be using an old FD; they
// will be replaced with mappings using the new FD after future calls
// to d.Translate(). This requires holding d.mapsMu, which precedes
// d.handleMu in the lock order.
d.mapsMu.Lock()
d.mappings.InvalidateAll(memmap.InvalidateOpts{})
d.mapsMu.Unlock()
}
for _, fd := range fdsToClose {
unix.Close(int(fd))
}
return nil
}
// Preconditions: d.handleMu must be locked.
func (d *dentry) readHandleLocked() handle {
return handle{
fdLisa: d.readFDLisa,
fd: d.readFD.RacyLoad(),
}
}
// Preconditions: d.handleMu must be locked.
func (d *dentry) writeHandleLocked() handle {
return handle{
fdLisa: d.writeFDLisa,
fd: d.writeFD.RacyLoad(),
}
}
func (d *dentry) syncRemoteFile(ctx context.Context) error {
d.handleMu.RLock()
defer d.handleMu.RUnlock()
return d.syncRemoteFileLocked(ctx)
}
// Preconditions: d.handleMu must be locked.
func (d *dentry) syncRemoteFileLocked(ctx context.Context) error {
// If we have a host FD, fsyncing it is likely to be faster than an fsync
// RPC. Prefer syncing write handles over read handles, since some remote
// filesystem implementations may not sync changes made through write
// handles otherwise.
if d.writeFD.RacyLoad() >= 0 {
ctx.UninterruptibleSleepStart(false)
err := unix.Fsync(int(d.writeFD.RacyLoad()))
ctx.UninterruptibleSleepFinish(false)
return err
}
if d.writeFDLisa.Ok() {
return d.writeFDLisa.Sync(ctx)
}
if d.readFD.RacyLoad() >= 0 {
ctx.UninterruptibleSleepStart(false)
err := unix.Fsync(int(d.readFD.RacyLoad()))
ctx.UninterruptibleSleepFinish(false)
return err
}
if d.readFDLisa.Ok() {
return d.readFDLisa.Sync(ctx)
}
return nil
}
func (d *dentry) syncCachedFile(ctx context.Context, forFilesystemSync bool) error {
d.handleMu.RLock()
defer d.handleMu.RUnlock()
h := d.writeHandleLocked()
if h.isOpen() {
// Write back dirty pages to the remote file.
d.dataMu.Lock()
err := fsutil.SyncDirtyAll(ctx, &d.cache, &d.dirty, d.size.Load(), d.fs.mfp.MemoryFile(), h.writeFromBlocksAt)
d.dataMu.Unlock()
if err != nil {
return err
}
}
if err := d.syncRemoteFileLocked(ctx); err != nil {
if !forFilesystemSync {
return err
}
// Only return err if we can reasonably have expected sync to succeed
// (d is a regular file and was opened for writing).
if d.isRegularFile() && h.isOpen() {
return err
}
ctx.Debugf("gofer.dentry.syncCachedFile: syncing non-writable or non-regular-file dentry failed: %v", err)
}
return nil
}
// incLinks increments link count.
func (d *dentry) incLinks() {
if d.nlink.Load() == 0 {
// The remote filesystem doesn't support link count.
return
}
d.nlink.Add(1)
}
// decLinks decrements link count.
func (d *dentry) decLinks() {
if d.nlink.Load() == 0 {
// The remote filesystem doesn't support link count.
return
}
d.nlink.Add(^uint32(0))
}
// fileDescription is embedded by gofer implementations of
// vfs.FileDescriptionImpl.
//
// +stateify savable
type fileDescription struct {
vfsfd vfs.FileDescription
vfs.FileDescriptionDefaultImpl
vfs.LockFD
lockLogging sync.Once `state:"nosave"`
}
func (fd *fileDescription) filesystem() *filesystem {
return fd.vfsfd.Mount().Filesystem().Impl().(*filesystem)
}
func (fd *fileDescription) dentry() *dentry {
return fd.vfsfd.Dentry().Impl().(*dentry)
}
// Stat implements vfs.FileDescriptionImpl.Stat.
func (fd *fileDescription) Stat(ctx context.Context, opts vfs.StatOptions) (linux.Statx, error) {
d := fd.dentry()
const validMask = uint32(linux.STATX_MODE | linux.STATX_UID | linux.STATX_GID | linux.STATX_ATIME | linux.STATX_MTIME | linux.STATX_CTIME | linux.STATX_SIZE | linux.STATX_BLOCKS | linux.STATX_BTIME)
if !d.cachedMetadataAuthoritative() && opts.Mask&validMask != 0 && opts.Sync != linux.AT_STATX_DONT_SYNC {
// Use specialFileFD.handle.fileLisa for the Stat if available, for the
// same reason that we try to use open FD in updateMetadataLocked().
var fdLisa *lisafs.ClientFD
if sffd, ok := fd.vfsfd.Impl().(*specialFileFD); ok {
fdLisa = &sffd.handle.fdLisa
}
err := d.updateMetadata(ctx, fdLisa)
if err != nil {
return linux.Statx{}, err
}
}
var stat linux.Statx
d.statTo(&stat)
return stat, nil
}
// SetStat implements vfs.FileDescriptionImpl.SetStat.
func (fd *fileDescription) SetStat(ctx context.Context, opts vfs.SetStatOptions) error {
return fd.dentry().setStat(ctx, auth.CredentialsFromContext(ctx), &opts, fd.vfsfd.Mount())
}
// ListXattr implements vfs.FileDescriptionImpl.ListXattr.
func (fd *fileDescription) ListXattr(ctx context.Context, size uint64) ([]string, error) {
return fd.dentry().listXattr(ctx, size)
}
// GetXattr implements vfs.FileDescriptionImpl.GetXattr.
func (fd *fileDescription) GetXattr(ctx context.Context, opts vfs.GetXattrOptions) (string, error) {
return fd.dentry().getXattr(ctx, auth.CredentialsFromContext(ctx), &opts)
}
// SetXattr implements vfs.FileDescriptionImpl.SetXattr.
func (fd *fileDescription) SetXattr(ctx context.Context, opts vfs.SetXattrOptions) error {
return fd.dentry().setXattr(ctx, auth.CredentialsFromContext(ctx), &opts)
}
// RemoveXattr implements vfs.FileDescriptionImpl.RemoveXattr.
func (fd *fileDescription) RemoveXattr(ctx context.Context, name string) error {
return fd.dentry().removeXattr(ctx, auth.CredentialsFromContext(ctx), name)
}
// LockBSD implements vfs.FileDescriptionImpl.LockBSD.
func (fd *fileDescription) LockBSD(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, block bool) error {
fd.lockLogging.Do(func() {
log.Infof("File lock using gofer file handled internally.")
})
return fd.LockFD.LockBSD(ctx, uid, ownerPID, t, block)
}
// LockPOSIX implements vfs.FileDescriptionImpl.LockPOSIX.
func (fd *fileDescription) LockPOSIX(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, r fslock.LockRange, block bool) error {
fd.lockLogging.Do(func() {
log.Infof("Range lock using gofer file handled internally.")
})
return fd.Locks().LockPOSIX(ctx, uid, ownerPID, t, r, block)
}
// UnlockPOSIX implements vfs.FileDescriptionImpl.UnlockPOSIX.
func (fd *fileDescription) UnlockPOSIX(ctx context.Context, uid fslock.UniqueID, r fslock.LockRange) error {
return fd.Locks().UnlockPOSIX(ctx, uid, r)
}
|