FM: Removes double syscall issue with GetEmulatedFDPath

In the case that the symlink following would immediately fail (Due to
the first query not existing or not being a symlink), then FEX would
immediately return the first query. Then the resulting syscall using
this result would try to run the syscall on that file, immediately
error, and try again on the file outside of the rootfs. This gives us
three syscalls for the price of one.

This adds an additional syscall cost to every syscall that that takes
the `FollowSymlink` code path, which is quite a few.

Instead now, check to see if the filepath exists at all, and if the
filepath doesn't even exist for the first fstatat, return a `NoEntry`
immediately. This will cause the resulting syscall waiting for the
result to skip the EmulatedFDPath check and run regular syscall.

If the filepath is symlink it will still loop to track through the
entire symlink tree to find the one that is in the rootfs.

If the filepath is just a file, then the `IsLink` step will fail,
returning the filepath to the file inside of the rootfs. (Getting this
step wrong will make wine/proton immediately break).

With this resolved, this converts a lot of three syscall operations down
to two.
This commit is contained in:
Ryan Houdek committed 2023-04-26 02:13:29 -07:00
1 parent 590422b295
commit e2db607769
1 file changed
+37 -17
@@ -420,27 +420,47 @@ std::pair<int, const char*> FileManager::GetEmulatedFDPath(int dirfd, const char
if (FollowSymlink) {
// Check if the combination of RootFS FD and subpath with the front '/' stripped off is a symlink.
while(FEX::HLE::IsSymlink(RootFSFD, &SubPath[1])) {
// Choose the current temporary working path.
auto CurrentTmp = TmpPaths[CurrentIndex];
bool HadAtLeastOne{};
struct stat Buffer{};
for(;;) {
// We need to check if the filepath exists and is a symlink.
// If the initial filepath doesn't exist then early exit.
// If it did exist at some state then trace it all all the way to the final link.
int Result = fstatat(RootFSFD, &SubPath[1], &Buffer, AT_SYMLINK_NOFOLLOW);
if (Result != 0 && errno == ENOENT && !HadAtLeastOne) {
// Initial file didn't exist at all
return NoEntry;
}
// Get the symlink of RootFS FD + stripped subpath.
auto SymlinkSize = FEX::HLE::GetSymlink(RootFSFD, &SubPath[1], CurrentTmp, PATH_MAX - 1);
const bool IsLink = Result == 0 && S_ISLNK(Buffer.st_mode);
if (SymlinkSize > 0 && CurrentTmp[0] == '/') {
// If the symlink is absolute:
// 1) Zero terminate it.
// 2) Set the path as our current subpath.
// 3) Switch to the next temporary index. (We don't want to overwrite the current one on the next loop iteration).
// 4) Run the loop again.
CurrentTmp[SymlinkSize] = 0;
SubPath = CurrentTmp;
CurrentIndex ^= 1;
HadAtLeastOne = true;
if (IsLink) {
// Choose the current temporary working path.
auto CurrentTmp = TmpPaths[CurrentIndex];
// Get the symlink of RootFS FD + stripped subpath.
auto SymlinkSize = FEX::HLE::GetSymlink(RootFSFD, &SubPath[1], CurrentTmp, PATH_MAX - 1);
if (SymlinkSize > 0 && CurrentTmp[0] == '/') {
// If the symlink is absolute:
// 1) Zero terminate it.
// 2) Set the path as our current subpath.
// 3) Switch to the next temporary index. (We don't want to overwrite the current one on the next loop iteration).
// 4) Run the loop again.
CurrentTmp[SymlinkSize] = 0;
SubPath = CurrentTmp;
CurrentIndex ^= 1;
}
else {
// If the path wasn't a symlink or wasn't absolute.
// 1) Break early, returning the previous found result.
// 2) If first iteration then we return `pathname`.
break;
}
}
else {
// If the path wasn't a symlink or wasn't absolute.
// 1) Break early, returning the previous found result.
// 2) If first iteration then we return `pathname`.
break;
}
}