Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use fstatfs to detect whether current file system supports shared locks for files opened for writing #55256

Merged
merged 16 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use fstatfs to detect whether current file system supports shared loc…
…ks for files opened for writing
  • Loading branch information
adamsitnik committed Jul 7, 2021
commit 71d131a4157c05d8d784e4140884c6db5d245cb8
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetFileSystemType")]
internal static extern long GetFileSystemType(SafeFileHandle fd);
}
}
2 changes: 2 additions & 0 deletions src/libraries/Native/Unix/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#cmakedefine01 HAVE_MMAP64
#cmakedefine01 HAVE_FTRUNCATE64
#cmakedefine01 HAVE_POSIX_FADVISE64
#cmakedefine01 HAVE_STATFS64
#cmakedefine01 HAVE_STATFS
#cmakedefine01 HAVE_FLOCK64
#cmakedefine01 HAVE_F_DUPFD_CLOEXEC
#cmakedefine01 HAVE_F_FULLFSYNC
Expand Down
1 change: 1 addition & 0 deletions src/libraries/Native/Unix/System.Native/entrypoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ static const Entry s_sysNative[] =
DllImportEntry(SystemNative_INotifyRemoveWatch)
DllImportEntry(SystemNative_RealPath)
DllImportEntry(SystemNative_GetPeerID)
DllImportEntry(SystemNative_GetFileSystemType)
DllImportEntry(SystemNative_LockFileRegion)
DllImportEntry(SystemNative_LChflags)
DllImportEntry(SystemNative_LChflagsCanSetHiddenFlag)
Expand Down
16 changes: 16 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#if HAVE_INOTIFY
#include <sys/inotify.h>
#endif
#if HAVE_STATFS64 || HAVE_STATFS
#include <sys/vfs.h>
#endif

#ifdef _AIX
#include <alloca.h>
Expand Down Expand Up @@ -1379,6 +1382,19 @@ static int16_t ConvertLockType(int16_t managedLockType)
}
}

int64_t SystemNative_GetFileSystemType(intptr_t fd)
{
#if HAVE_STATFS64
struct statfs64 statfsArgs;
return fstatfs64(ToFileDescriptor(fd), &statfsArgs) == -1 ? -1 : statfsArgs.f_type;
#elif HAVE_STATFS
struct statfs statfsArgs;
return fstatfs(ToFileDescriptor(fd), &statfsArgs) == -1 ? -1 : statfsArgs.f_type;
#else
return 0;
#endif
}

int32_t SystemNative_LockFileRegion(intptr_t fd, int64_t offset, int64_t length, int16_t lockType)
{
int16_t unixLockType = ConvertLockType(lockType);
Expand Down
5 changes: 5 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,11 @@ PALEXPORT char* SystemNative_RealPath(const char* path);
*/
PALEXPORT int32_t SystemNative_GetPeerID(intptr_t socket, uid_t* euid);

/**
* returns false for NFS and CIFS, otherwise true
*/
PALEXPORT int64_t SystemNative_GetFileSystemType(intptr_t fd);

/**
* Attempts to lock/unlock the region of the file "fd" specified by the offset and length. lockType
* can be set to F_UNLCK (2) for unlock or F_WRLCK (3) for lock.
Expand Down
22 changes: 22 additions & 0 deletions src/libraries/Native/Unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ check_c_source_compiles(

# /in_pktinfo

check_c_source_compiles(
"
#include <sys/vfs.h>
int main(void)
{
struct statfs64 s;
return 0;
}
"
HAVE_STATFS64)

check_c_source_compiles(
"
#include <sys/vfs.h>
int main(void)
{
struct statfs s;
return 0;
}
"
HAVE_STATFS)

check_c_source_compiles(
"
#include <fcntl.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private void Init(string path, FileMode mode, FileAccess access, FileShare share
// lock on the file and all other modes use a shared lock. While this is not as granular as Windows, not mandatory,
// and not atomic with file opening, it's better than nothing.
Interop.Sys.LockOperations lockOperation = (share == FileShare.None) ? Interop.Sys.LockOperations.LOCK_EX : Interop.Sys.LockOperations.LOCK_SH;
if (Interop.Sys.FLock(this, lockOperation | Interop.Sys.LockOperations.LOCK_NB) < 0)
if (CanLockTheFile(lockOperation, access) && Interop.Sys.FLock(this, lockOperation | Interop.Sys.LockOperations.LOCK_NB) < 0)
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
// The only error we care about is EWOULDBLOCK, which indicates that the file is currently locked by someone
// else and we would block trying to access it. Other errors, such as ENOTSUP (locking isn't supported) or
Expand Down Expand Up @@ -319,6 +319,30 @@ private void Init(string path, FileMode mode, FileAccess access, FileShare share
}
}

private bool CanLockTheFile(Interop.Sys.LockOperations lockOperation, FileAccess access)
{
Debug.Assert(lockOperation == Interop.Sys.LockOperations.LOCK_EX || lockOperation == Interop.Sys.LockOperations.LOCK_SH);

if (lockOperation == Interop.Sys.LockOperations.LOCK_EX)
{
return true; // LOCK_EX is always OK
}
else if ((access & FileAccess.Write) == 0)
{
return true; // LOCK_SH is always OK when reading
}

switch (Interop.Sys.GetFileSystemType(this))
{
case 0x6969: // NFS_SUPER_MAGIC
case 0xFF534D42: // CIFS_MAGIC_NUMBER
case 0x517B: // SMB_SUPER_MAGIC
return false; // LOCK_SH is not OK when writing to NFS, CIFS or SMB
default:
return true; // in all other situations it should be OK
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
}
}

private bool GetCanSeek()
{
Debug.Assert(!IsClosed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,9 @@
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ErrNo.cs">
<Link>Common\Interop\Unix\System.Native\Interop.ErrNo.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Fstatfs.cs">
<Link>Common\Interop\Unix\System.Native\Interop.Fstatfs.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.FLock.cs">
<Link>Common\Interop\Unix\System.Native\Interop.FLock.cs</Link>
</Compile>
Expand Down