Skip to content

Commit

Permalink
wip: Dokan file lock/unlock
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Chivu <[email protected]>
  • Loading branch information
stefan-chivu committed Dec 19, 2022
1 parent 7471527 commit 9b2cbed
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/dokan/ceph_dokan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,121 @@ static NTSTATUS WinCephUnmount(
return 0;
}

static NTSTATUS WinCephLockFile(
LPCWSTR FileName,
LONGLONG ByteOffset,
LONGLONG Length,
PDOKAN_FILE_INFO DokanFileInfo) {
if (!Length) {
return 0;
}

std::string path = get_path(FileName);

if (ByteOffset < 0) {
dout(2) << __func__ << " " << path
<< ": Invalid offset: " << ByteOffset << dendl;
return STATUS_INVALID_PARAMETER;
}
if (ByteOffset > CEPH_DOKAN_MAX_FILE_SZ ||
Length > CEPH_DOKAN_MAX_IO_SZ) {
dout(2) << "Lock size exceeds max file length: " << path
<< ". ByteOffset: " << ByteOffset
<< ". Lock length: " << Length << dendl;
return STATUS_FILE_TOO_LARGE;
}

HANDLE hFile = CreateFile(
path.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

if (hFile == INVALID_HANDLE_VALUE) {
return STATUS_OPEN_FAILED;
}

BOOL ret = LockFile(
hFile,
ByteOffset & 0xffffffff, // LOW part of 64 bit offset
ByteOffset >> 32, // HIGH part of 64 bit offset
Length & 0xffffffff, // LOW part of the 64 lock length
Length >> 32 // HIGH part of the 64 lock length
);

if (!ret) {
dout(2) << __func__ << " " << path
<< ": LockFile failed. Error: " << ret
<< ". ByteOffset: " << ByteOffset
<< ". Lock length: " << Length << dendl;
CloseHandle(hFile);
return cephfs_errno_to_ntstatus_map(ret);
}
CloseHandle(hFile);
return 0;
}

static NTSTATUS WinCephUnlockFile(
LPCWSTR FileName,
LONGLONG ByteOffset,
LONGLONG Length,
PDOKAN_FILE_INFO DokanFileInfo) {

if (!Length) {
return 0;
}

std::string path = get_path(FileName);

if (ByteOffset < 0) {
dout(2) << __func__ << " " << path
<< ": Invalid offset: " << ByteOffset << dendl;
return STATUS_INVALID_PARAMETER;
}
if (ByteOffset > CEPH_DOKAN_MAX_FILE_SZ ||
Length > CEPH_DOKAN_MAX_IO_SZ) {
dout(2) << "Unlock size exceeds max file length: " << path
<< ". ByteOffset: " << ByteOffset
<< ". Unlock length: " << Length << dendl;
return STATUS_FILE_TOO_LARGE;
}

HANDLE hFile = CreateFile(
path.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

if (hFile == INVALID_HANDLE_VALUE) {
return STATUS_OPEN_FAILED;
}

BOOL ret = UnlockFile(
hFile,
ByteOffset & 0xffffffff, // LOW part of 64 bit offset
ByteOffset >> 32, // HIGH part of 64 bit offset
Length & 0xffffffff, // LOW part of the 64 lock length
Length >> 32 // HIGH part of the 64 lock length
);

if (!ret) {
dout(2) << __func__ << " " << path
<< ": UnlockFile failed. Error: " << ret
<< ". ByteOffset: " << ByteOffset
<< ". Unlock length: " << Length << dendl;
CloseHandle(hFile);
return cephfs_errno_to_ntstatus_map(ret);
}
CloseHandle(hFile);
return 0;
}

BOOL WINAPI ConsoleHandler(DWORD dwType)
{
switch(dwType) {
Expand Down Expand Up @@ -926,6 +1041,8 @@ int do_map() {
dokan_operations->GetDiskFreeSpace = WinCephGetDiskFreeSpace;
dokan_operations->GetVolumeInformation = WinCephGetVolumeInformation;
dokan_operations->Unmounted = WinCephUnmount;
dokan_operations->LockFile = WinCephLockFile;
dokan_operations->UnlockFile = WinCephUnlockFile;

ceph_create_with_context(&cmount, g_ceph_context);

Expand Down

0 comments on commit 9b2cbed

Please sign in to comment.