Skip to content

Commit

Permalink
implement shareaccess @marinkobabic
Browse files Browse the repository at this point in the history
  • Loading branch information
Liryna committed Aug 19, 2015
1 parent e6e9964 commit ac016e9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
5 changes: 5 additions & 0 deletions sys/cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ DokanCompleteCleanup(
FsRtlNotifyCleanup(vcb->NotifySync, &vcb->DirNotifyList, ccb);
}

InterlockedDecrement(&fcb->OpenHandleCount);
IoRemoveShareAccess(irpSp->FileObject, &fcb->ShareAccess);

DDbgPrint(" OpenHandleCount:%d ReferenceCount:%d\n", fcb->OpenHandleCount, fcb->ReferenceCount);

DokanCompleteIrpRequest(irp, status, 0);

DDbgPrint("<== DokanCompleteCleanup\n");
Expand Down
2 changes: 1 addition & 1 deletion sys/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Return Value:
}

eventContext->Context = ccb->UserContext;
DDbgPrint(" UserContext:%X\n", (ULONG)ccb->UserContext);
DDbgPrint(" UserContext:%X RefereceCount:%d OpenHandleCount:%d\n", (ULONG)ccb->UserContext, fcb->ReferenceCount, fcb->OpenHandleCount);

// copy the file name to be closed
eventContext->Operation.Close.FileNameLength = fcb->FileName.Length;
Expand Down
53 changes: 42 additions & 11 deletions sys/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ DokanGetFCB(
ExFreePool(FileName);
}

InterlockedIncrement(&fcb->FileCount);
InterlockedIncrement(&fcb->ReferenceCount);

ExReleaseResourceLite(&Vcb->Resource);
KeLeaveCriticalRegion();
Expand All @@ -174,9 +174,9 @@ DokanFreeFCB(
ExAcquireResourceExclusiveLite(&vcb->Resource, TRUE);
ExAcquireResourceExclusiveLite(&Fcb->Resource, TRUE);

InterlockedDecrement(&Fcb->FileCount);
InterlockedDecrement(&Fcb->ReferenceCount);

if (Fcb->FileCount == 0) {
if (Fcb->ReferenceCount == 0) {

RemoveEntryList(&Fcb->NextFCB);

Expand Down Expand Up @@ -502,14 +502,6 @@ Return Value:
fcb->AdvancedFCBHeader.Flags2 &= ~FSRTL_FLAG2_SUPPORTS_FILTER_CONTEXTS;
}

ccb = DokanAllocateCCB(dcb, fcb);
if (ccb == NULL) {
DDbgPrint(" Was not able to allocate CCB\n");
DokanFreeFCB(fcb); // FileName is freed here
status = STATUS_INSUFFICIENT_RESOURCES;
__leave;
}

//remember FILE_DELETE_ON_CLOSE so than the file can be deleted in close for windows 8
if (irpSp->Parameters.Create.Options & FILE_DELETE_ON_CLOSE) {
fcb->Flags |= DOKAN_DELETE_ON_CLOSE;
Expand All @@ -518,6 +510,34 @@ Return Value:
fcb->Flags &= ~DOKAN_DELETE_ON_CLOSE;
}

if (fcb->OpenHandleCount > 0) {
/* check the share access conflicts */
status = IoCheckShareAccess(irpSp->Parameters.Create.SecurityContext->DesiredAccess,
irpSp->Parameters.Create.ShareAccess,
irpSp->FileObject,
&(fcb->ShareAccess),
TRUE);
if (!NT_SUCCESS(status)) {
DokanFreeFCB(fcb);
__leave;
}
}
else {
/* set share access rights */
IoSetShareAccess(irpSp->Parameters.Create.SecurityContext->DesiredAccess,
irpSp->Parameters.Create.ShareAccess,
irpSp->FileObject,
&(fcb->ShareAccess));
}

ccb = DokanAllocateCCB(dcb, fcb);
if (ccb == NULL) {
DDbgPrint(" Was not able to allocate CCB");
DokanFreeFCB(fcb); // FileName is freed here
status = STATUS_INSUFFICIENT_RESOURCES;
__leave;
}

fileObject->FsContext = &fcb->AdvancedFCBHeader;
fileObject->FsContext2 = ccb;
fileObject->PrivateCacheMap = NULL;
Expand All @@ -529,10 +549,19 @@ Return Value:

if (eventContext == NULL) {
DDbgPrint(" Was not able to allocate eventContext\n");
IoRemoveShareAccess(irpSp->FileObject, &fcb->ShareAccess);
fileObject->FsContext = NULL;
fileObject->FsContext2 = NULL;
fileObject->PrivateCacheMap = NULL;
fileObject->SectionObjectPointer = NULL;
DokanFreeFCB(fcb);
DokanFreeCCB(ccb);
status = STATUS_INSUFFICIENT_RESOURCES;
__leave;
}

InterlockedIncrement(&fcb->OpenHandleCount);

eventContext->Context = 0;
eventContext->FileFlags |= fcb->Flags;

Expand Down Expand Up @@ -650,6 +679,8 @@ DokanCompleteCreate(
}
} else {
DDbgPrint(" IRP_MJ_CREATE failed. Free CCB:%X\n", ccb);
InterlockedDecrement(&fcb->OpenHandleCount);
IoRemoveShareAccess(irpSp->FileObject, &fcb->ShareAccess);
DokanFreeCCB(ccb);
DokanFreeFCB(fcb);
}
Expand Down
27 changes: 20 additions & 7 deletions sys/dokan.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ typedef struct _DokanVolumeControlBlock {
} DokanVCB, *PDokanVCB;


//
// File Control Block
//
// Data that represents an open file
// There is a single instance of the FCB for every open file
//
typedef struct _DokanFileControlBlock
{
FSD_IDENTIFIER Identifier;
Expand All @@ -249,18 +255,27 @@ typedef struct _DokanFileControlBlock
ERESOURCE Resource;
LIST_ENTRY NextCCB;

LONG FileCount;

ULONG Flags;

UNICODE_STRING FileName;

//uint32 ReferenceCount;
//uint32 OpenHandleCount;
} DokanFCB, *PDokanFCB;
// Share Access for the file object
SHARE_ACCESS ShareAccess;

// Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLEANUP
LONG OpenHandleCount;

// Incremented on IRP_MJ_CREATE, decremented on IRP_MJ_CLOSE
LONG ReferenceCount;
} DokanFCB, *PDokanFCB;


//
// Context Control Block
//
// Data that represents one instance of an open file
// There is one instance of the CCB for every instance of an open file
//
typedef struct _DokanContextControlBlock
{
FSD_IDENTIFIER Identifier;
Expand All @@ -274,8 +289,6 @@ typedef struct _DokanContextControlBlock
ULONG SearchPatternLength;

ULONG Flags;

int FileCount;
ULONG MountId;
} DokanCCB, *PDokanCCB;

Expand Down

0 comments on commit ac016e9

Please sign in to comment.