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 // Flush data and then release the locks on the file server
169 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
174 if( !NT_SUCCESS( stIoStatus.Status))
177 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
178 AFS_TRACE_LEVEL_ERROR,
179 "AFSLockControl CcFlushCache [1] failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
180 pFcb->ObjectInformation->FileId.Cell,
181 pFcb->ObjectInformation->FileId.Volume,
182 pFcb->ObjectInformation->FileId.Vnode,
183 pFcb->ObjectInformation->FileId.Unique,
185 stIoStatus.Information);
187 ntStatus = stIoStatus.Status;
190 RtlZeroMemory( &stUnlockRequestCB,
191 sizeof( AFSByteRangeUnlockRequestCB));
193 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
195 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK_ALL,
196 AFS_REQUEST_FLAG_SYNCHRONOUS,
198 &pCcb->DirectoryCB->NameInformation.FileName,
199 &pFcb->ObjectInformation->FileId,
200 (void *)&stUnlockRequestCB,
201 sizeof( AFSByteRangeUnlockRequestCB),
206 // Even on a failure we need to notify the rtl package of the unlock
212 case IRP_MN_UNLOCK_SINGLE:
215 // Flush the data and then release the file server locks
218 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
219 &pIrpSp->Parameters.LockControl.ByteOffset,
220 pIrpSp->Parameters.LockControl.Length->LowPart,
223 if( !NT_SUCCESS( stIoStatus.Status))
226 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
227 AFS_TRACE_LEVEL_ERROR,
228 "AFSLockControl CcFlushCache [2] failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
229 pFcb->ObjectInformation->FileId.Cell,
230 pFcb->ObjectInformation->FileId.Volume,
231 pFcb->ObjectInformation->FileId.Vnode,
232 pFcb->ObjectInformation->FileId.Unique,
234 stIoStatus.Information);
236 ntStatus = stIoStatus.Status;
239 stUnlockRequestCB.Count = 1;
241 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
243 stUnlockRequestCB.Request[ 0].LockType = AFS_BYTE_RANGE_LOCK_TYPE_EXCL;
245 stUnlockRequestCB.Request[ 0].Offset = pIrpSp->Parameters.LockControl.ByteOffset;
247 stUnlockRequestCB.Request[ 0].Length.QuadPart = pIrpSp->Parameters.LockControl.Length->QuadPart;
249 ulResultLen = sizeof( AFSByteRangeUnlockResultCB);
251 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK,
252 AFS_REQUEST_FLAG_SYNCHRONOUS,
254 &pCcb->DirectoryCB->NameInformation.FileName,
255 &pFcb->ObjectInformation->FileId,
256 (void *)&stUnlockRequestCB,
257 sizeof( AFSByteRangeUnlockRequestCB),
258 (void *)&stUnlockResultCB,
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( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()))
307 "EXCEPTION - AFSLockControl\n");
309 AFSDumpTraceFilesFnc();
312 // Again, there is little point in failing this request but pass back some type of failure status
315 ntStatus = STATUS_UNSUCCESSFUL;
317 AFSCompleteRequest( Irp,