2 * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3 * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
14 * this list of conditions and the following disclaimer in the
16 * and/or other materials provided with the distribution.
17 * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18 * nor the names of their contributors may be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission from Kernel Drivers, LLC and Your File System, Inc.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 // File: AFSLockControl.cpp
39 #include "AFSCommon.h"
42 AFSLockControl( IN PDEVICE_OBJECT LibDeviceObject,
46 NTSTATUS ntStatus = STATUS_SUCCESS;
47 ULONG ulRequestType = 0;
48 IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
51 BOOLEAN bCompleteRequest = TRUE;
52 AFSByteRangeLockRequestCB stLockRequestCB;
53 AFSByteRangeLockResultCB stLockResultCB;
54 AFSByteRangeUnlockRequestCB stUnlockRequestCB;
55 AFSByteRangeUnlockResultCB stUnlockResultCB;
56 ULONG ulResultLen = 0;
57 BOOLEAN bReleaseResource = FALSE;
58 IO_STATUS_BLOCK stIoStatus;
63 pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
65 pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
70 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
71 AFS_TRACE_LEVEL_ERROR,
72 "AFSLockControl Attempted access (%08lX) when pFcb == NULL\n",
75 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
79 // Acquire the main shared for adding the lock control to the list
82 AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
83 AFS_TRACE_LEVEL_VERBOSE,
84 "AFSLockControl Acquiring Fcb lock %08lX SHARED %08lX\n",
85 &pFcb->NPFcb->Resource,
86 PsGetCurrentThread());
88 AFSAcquireShared( &pFcb->NPFcb->Resource,
91 bReleaseResource = TRUE;
93 if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
96 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
97 AFS_TRACE_LEVEL_ERROR,
98 "AFSLockControl Failing request against PIOCtl Fcb\n");
100 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
102 else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
105 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
106 AFS_TRACE_LEVEL_ERROR,
107 "AFSLockControl Failing request against SpecialShare Fcb\n");
109 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
111 else if( pFcb->Header.NodeTypeCode == AFS_INVALID_FCB)
114 AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
115 AFS_TRACE_LEVEL_ERROR,
116 "AFSLockControl Failing request against Invalid Fcb\n");
118 try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
122 // Post the request to the service for checks
125 switch( pIrpSp->MinorFunction)
131 stLockRequestCB.Count = 1;
133 stLockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
135 stLockRequestCB.Request[ 0].LockType = AFS_BYTE_RANGE_LOCK_TYPE_EXCL;
137 stLockRequestCB.Request[ 0].Offset = pIrpSp->Parameters.LockControl.ByteOffset;
139 stLockRequestCB.Request[ 0].Length.QuadPart = pIrpSp->Parameters.LockControl.Length->QuadPart;
141 ulResultLen = sizeof( AFSByteRangeLockResultCB);
143 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_LOCK,
144 AFS_REQUEST_FLAG_SYNCHRONOUS,
146 &pCcb->DirectoryCB->NameInformation.FileName,
147 &pFcb->ObjectInformation->FileId,
149 sizeof( AFSByteRangeLockRequestCB),
153 if( !NT_SUCCESS( ntStatus))
156 try_return( ntStatus);
162 case IRP_MN_UNLOCK_ALL:
163 case IRP_MN_UNLOCK_ALL_BY_KEY:
166 RtlZeroMemory( &stUnlockRequestCB,
167 sizeof( AFSByteRangeUnlockRequestCB));
169 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
171 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK_ALL,
172 AFS_REQUEST_FLAG_SYNCHRONOUS,
174 &pCcb->DirectoryCB->NameInformation.FileName,
175 &pFcb->ObjectInformation->FileId,
176 (void *)&stUnlockRequestCB,
177 sizeof( AFSByteRangeUnlockRequestCB),
181 if( NT_SUCCESS( ntStatus))
183 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
188 if( !NT_SUCCESS( stIoStatus.Status))
191 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
192 AFS_TRACE_LEVEL_ERROR,
193 "AFSLockControl CcFlushCache [1] failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
194 pFcb->ObjectInformation->FileId.Cell,
195 pFcb->ObjectInformation->FileId.Volume,
196 pFcb->ObjectInformation->FileId.Vnode,
197 pFcb->ObjectInformation->FileId.Unique,
199 stIoStatus.Information);
201 ntStatus = stIoStatus.Status;
206 // Even on a failure we need to notify the rtl package of the unlock
212 case IRP_MN_UNLOCK_SINGLE:
215 stUnlockRequestCB.Count = 1;
217 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
219 stUnlockRequestCB.Request[ 0].LockType = AFS_BYTE_RANGE_LOCK_TYPE_EXCL;
221 stUnlockRequestCB.Request[ 0].Offset = pIrpSp->Parameters.LockControl.ByteOffset;
223 stUnlockRequestCB.Request[ 0].Length.QuadPart = pIrpSp->Parameters.LockControl.Length->QuadPart;
225 ulResultLen = sizeof( AFSByteRangeUnlockResultCB);
227 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK,
228 AFS_REQUEST_FLAG_SYNCHRONOUS,
230 &pCcb->DirectoryCB->NameInformation.FileName,
231 &pFcb->ObjectInformation->FileId,
232 (void *)&stUnlockRequestCB,
233 sizeof( AFSByteRangeUnlockRequestCB),
234 (void *)&stUnlockResultCB,
237 if( NT_SUCCESS( ntStatus))
239 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
240 &pIrpSp->Parameters.LockControl.ByteOffset,
241 pIrpSp->Parameters.LockControl.Length->LowPart,
244 if( !NT_SUCCESS( stIoStatus.Status))
247 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
248 AFS_TRACE_LEVEL_ERROR,
249 "AFSLockControl CcFlushCache [2] failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
250 pFcb->ObjectInformation->FileId.Cell,
251 pFcb->ObjectInformation->FileId.Volume,
252 pFcb->ObjectInformation->FileId.Vnode,
253 pFcb->ObjectInformation->FileId.Unique,
255 stIoStatus.Information);
257 ntStatus = stIoStatus.Status;
270 // Below here we won't complete the request, it is handled by the lock package
273 bCompleteRequest = FALSE;
276 // Now call the system package for actually processing the lock request
279 ntStatus = FsRtlProcessFileLock( &pFcb->Specific.File.FileLock,
285 if( bReleaseResource)
292 AFSReleaseResource( &pFcb->NPFcb->Resource);
295 if( bCompleteRequest)
298 AFSCompleteRequest( Irp,
302 __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()))
307 "EXCEPTION - AFSLockControl\n");
310 // Again, there is little point in failing this request but pass back some type of failure status
313 ntStatus = STATUS_UNSUCCESSFUL;
315 AFSCompleteRequest( Irp,