b2298cf252faa8ad83dc7b38c16e0485e4211e40
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSLockControl.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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
13  *   notice,
14  *   this list of conditions and the following disclaimer in the
15  *   documentation
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.
21  *
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.
33  */
34
35 //
36 // File: AFSLockControl.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 NTSTATUS
42 AFSLockControl( IN PDEVICE_OBJECT LibDeviceObject,
43                   IN PIRP Irp)
44 {
45
46     UNREFERENCED_PARAMETER(LibDeviceObject);
47     NTSTATUS ntStatus = STATUS_SUCCESS;
48     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
49     AFSFcb *pFcb = NULL;
50     AFSCcb *pCcb = NULL;
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;
59
60     __try
61     {
62
63         pFcb = (AFSFcb *)pIrpSp->FileObject->FsContext;
64
65         pCcb = (AFSCcb *)pIrpSp->FileObject->FsContext2;
66
67         if( pFcb == NULL)
68         {
69
70             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
71                           AFS_TRACE_LEVEL_ERROR,
72                           "AFSLockControl Attempted access (%p) when pFcb == NULL\n",
73                           Irp));
74
75             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
76         }
77
78         //
79         // Acquire the main shared for adding the lock control to the list
80         //
81
82         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
83                       AFS_TRACE_LEVEL_VERBOSE,
84                       "AFSLockControl Acquiring Fcb lock %p SHARED %08lX\n",
85                       &pFcb->NPFcb->Resource,
86                       PsGetCurrentThread()));
87
88         AFSAcquireShared( &pFcb->NPFcb->Resource,
89                           TRUE);
90
91         bReleaseResource = TRUE;
92
93         if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
94         {
95
96             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
97                           AFS_TRACE_LEVEL_ERROR,
98                           "AFSLockControl Failing request against PIOCtl Fcb\n"));
99
100             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
101         }
102         else if( pFcb->Header.NodeTypeCode == AFS_SPECIAL_SHARE_FCB)
103         {
104
105             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
106                           AFS_TRACE_LEVEL_ERROR,
107                           "AFSLockControl Failing request against SpecialShare Fcb\n"));
108
109             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
110         }
111         else if( pFcb->Header.NodeTypeCode == AFS_INVALID_FCB)
112         {
113
114             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
115                           AFS_TRACE_LEVEL_ERROR,
116                           "AFSLockControl Failing request against Invalid Fcb\n"));
117
118             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
119         }
120
121         //
122         // Post the request to the service for checks
123         //
124
125         switch( pIrpSp->MinorFunction)
126         {
127
128             case IRP_MN_LOCK:
129             {
130
131                 stLockRequestCB.Count = 1;
132
133                 stLockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
134
135                 stLockRequestCB.Request[ 0].LockType = AFS_BYTE_RANGE_LOCK_TYPE_EXCL;
136
137                 stLockRequestCB.Request[ 0].Offset = pIrpSp->Parameters.LockControl.ByteOffset;
138
139                 stLockRequestCB.Request[ 0].Length.QuadPart = pIrpSp->Parameters.LockControl.Length->QuadPart;
140
141                 ulResultLen = sizeof( AFSByteRangeLockResultCB);
142
143                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_LOCK,
144                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
145                                               &pCcb->AuthGroup,
146                                               &pCcb->DirectoryCB->NameInformation.FileName,
147                                               &pFcb->ObjectInformation->FileId,
148                                               pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
149                                               pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
150                                               &stLockRequestCB,
151                                               sizeof( AFSByteRangeLockRequestCB),
152                                               &stLockResultCB,
153                                               &ulResultLen);
154
155                 if( !NT_SUCCESS( ntStatus))
156                 {
157
158                     try_return( ntStatus);
159                 }
160
161                 break;
162             }
163
164             case IRP_MN_UNLOCK_ALL:
165             case IRP_MN_UNLOCK_ALL_BY_KEY:
166             {
167                 //
168                 // Flush data and then release the locks on the file server
169                 //
170
171                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
172                               AFS_TRACE_LEVEL_VERBOSE,
173                               "AFSLockControl Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
174                               &pFcb->NPFcb->SectionObjectResource,
175                               PsGetCurrentThread()));
176
177                 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
178                                   TRUE);
179
180                 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
181                               NULL,
182                               0,
183                               &stIoStatus);
184
185                 if( !NT_SUCCESS( stIoStatus.Status))
186                 {
187
188                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
189                                   AFS_TRACE_LEVEL_ERROR,
190                                   "AFSLockControl CcFlushCache [1] failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
191                                   pFcb->ObjectInformation->FileId.Cell,
192                                   pFcb->ObjectInformation->FileId.Volume,
193                                   pFcb->ObjectInformation->FileId.Vnode,
194                                   pFcb->ObjectInformation->FileId.Unique,
195                                   stIoStatus.Status,
196                                   stIoStatus.Information));
197
198                     ntStatus = stIoStatus.Status;
199                 }
200
201                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
202                               AFS_TRACE_LEVEL_VERBOSE,
203                               "AFSLockControl Releasing Fcb SectionObject lock %p SHARED %08lX\n",
204                               &pFcb->NPFcb->SectionObjectResource,
205                               PsGetCurrentThread()));
206
207                 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
208
209                 RtlZeroMemory( &stUnlockRequestCB,
210                                sizeof( AFSByteRangeUnlockRequestCB));
211
212                 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
213
214                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK_ALL,
215                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
216                                               &pCcb->AuthGroup,
217                                               &pCcb->DirectoryCB->NameInformation.FileName,
218                                               &pFcb->ObjectInformation->FileId,
219                                               pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
220                                               pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
221                                               (void *)&stUnlockRequestCB,
222                                               sizeof( AFSByteRangeUnlockRequestCB),
223                                               NULL,
224                                               NULL);
225
226                 //
227                 // Even on a failure we need to notify the rtl package of the unlock
228                 //
229
230                 break;
231             }
232
233             case IRP_MN_UNLOCK_SINGLE:
234             {
235                 //
236                 // Flush the data and then release the file server locks
237                 //
238
239                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
240                               AFS_TRACE_LEVEL_VERBOSE,
241                               "AFSLockControl Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
242                               &pFcb->NPFcb->SectionObjectResource,
243                               PsGetCurrentThread()));
244
245                 AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
246                                   TRUE);
247                 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
248                               &pIrpSp->Parameters.LockControl.ByteOffset,
249                               pIrpSp->Parameters.LockControl.Length->LowPart,
250                               &stIoStatus);
251
252                 AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING,
253                               AFS_TRACE_LEVEL_VERBOSE,
254                               "AFSLockControl Releasing Fcb SectionObject lock %p SHARED %08lX\n",
255                               &pFcb->NPFcb->SectionObjectResource,
256                               PsGetCurrentThread()));
257
258                 AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
259
260                 if( !NT_SUCCESS( stIoStatus.Status))
261                 {
262
263                     AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
264                                   AFS_TRACE_LEVEL_ERROR,
265                                   "AFSLockControl CcFlushCache [2] failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
266                                   pFcb->ObjectInformation->FileId.Cell,
267                                   pFcb->ObjectInformation->FileId.Volume,
268                                   pFcb->ObjectInformation->FileId.Vnode,
269                                   pFcb->ObjectInformation->FileId.Unique,
270                                   stIoStatus.Status,
271                                   stIoStatus.Information));
272
273                     ntStatus = stIoStatus.Status;
274                 }
275
276                 stUnlockRequestCB.Count = 1;
277
278                 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
279
280                 stUnlockRequestCB.Request[ 0].LockType = AFS_BYTE_RANGE_LOCK_TYPE_EXCL;
281
282                 stUnlockRequestCB.Request[ 0].Offset = pIrpSp->Parameters.LockControl.ByteOffset;
283
284                 stUnlockRequestCB.Request[ 0].Length.QuadPart = pIrpSp->Parameters.LockControl.Length->QuadPart;
285
286                 ulResultLen = sizeof( AFSByteRangeUnlockResultCB);
287
288                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK,
289                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
290                                               &pCcb->AuthGroup,
291                                               &pCcb->DirectoryCB->NameInformation.FileName,
292                                               &pFcb->ObjectInformation->FileId,
293                                               pFcb->ObjectInformation->VolumeCB->VolumeInformation.Cell,
294                                               pFcb->ObjectInformation->VolumeCB->VolumeInformation.CellLength,
295                                               (void *)&stUnlockRequestCB,
296                                               sizeof( AFSByteRangeUnlockRequestCB),
297                                               (void *)&stUnlockResultCB,
298                                               &ulResultLen);
299
300                 break;
301             }
302
303             default:
304
305                 break;
306         }
307
308         //
309         // Below here we won't complete the request, it is handled by the lock package
310         //
311
312         bCompleteRequest = FALSE;
313
314         //
315         //  Now call the system package for actually processing the lock request
316         //
317
318         ntStatus = FsRtlProcessFileLock( &pFcb->Specific.File.FileLock,
319                                          Irp,
320                                          NULL);
321
322 try_exit:
323
324         if( bReleaseResource)
325         {
326
327             //
328             // And drop it
329             //
330
331             AFSReleaseResource( &pFcb->NPFcb->Resource);
332         }
333
334         if( bCompleteRequest)
335         {
336
337             AFSCompleteRequest( Irp,
338                                 ntStatus);
339         }
340     }
341     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()))
342     {
343
344         AFSDbgTrace(( 0,
345                       0,
346                       "EXCEPTION - AFSLockControl\n"));
347
348         AFSDumpTraceFilesFnc();
349
350         //
351         // Again, there is little point in failing this request but pass back some type of failure status
352         //
353
354         ntStatus = STATUS_UNSUCCESSFUL;
355
356         AFSCompleteRequest( Irp,
357                             ntStatus);
358     }
359
360     return ntStatus;
361 }