Windows: Flush data and then drop locks
[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     NTSTATUS ntStatus = STATUS_SUCCESS;
47     ULONG ulRequestType = 0;
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             AFSDbgLogMsg( AFS_SUBSYSTEM_FILE_PROCESSING,
71                           AFS_TRACE_LEVEL_ERROR,
72                           "AFSLockControl Attempted access (%08lX) 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         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());
87
88         AFSAcquireShared( &pFcb->NPFcb->Resource,
89                           TRUE);
90
91         bReleaseResource = TRUE;
92
93         if( pFcb->Header.NodeTypeCode == AFS_IOCTL_FCB)
94         {
95
96             AFSDbgLogMsg( 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             AFSDbgLogMsg( 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             AFSDbgLogMsg( 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                                               &stLockRequestCB,
149                                               sizeof( AFSByteRangeLockRequestCB),
150                                               &stLockResultCB,
151                                               &ulResultLen);
152
153                 if( !NT_SUCCESS( ntStatus))
154                 {
155
156                     try_return( ntStatus);
157                 }
158
159                 break;
160             }
161
162             case IRP_MN_UNLOCK_ALL:
163             case IRP_MN_UNLOCK_ALL_BY_KEY:
164             {
165                 //
166                 // Flush data and then release the locks on the file server
167                 //
168
169                 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
170                               NULL,
171                               0,
172                               &stIoStatus);
173
174                 if( !NT_SUCCESS( stIoStatus.Status))
175                 {
176
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,
184                                   stIoStatus.Status,
185                                   stIoStatus.Information);
186
187                     ntStatus = stIoStatus.Status;
188                 }
189
190                 RtlZeroMemory( &stUnlockRequestCB,
191                                sizeof( AFSByteRangeUnlockRequestCB));
192
193                 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
194
195                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK_ALL,
196                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
197                                               &pCcb->AuthGroup,
198                                               &pCcb->DirectoryCB->NameInformation.FileName,
199                                               &pFcb->ObjectInformation->FileId,
200                                               (void *)&stUnlockRequestCB,
201                                               sizeof( AFSByteRangeUnlockRequestCB),
202                                               NULL,
203                                               NULL);
204
205                 //
206                 // Even on a failure we need to notify the rtl package of the unlock
207                 //
208
209                 break;
210             }
211
212             case IRP_MN_UNLOCK_SINGLE:
213             {
214                 //
215                 // Flush the data and then release the file server locks
216                 //
217
218                 CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
219                               &pIrpSp->Parameters.LockControl.ByteOffset,
220                               pIrpSp->Parameters.LockControl.Length->LowPart,
221                               &stIoStatus);
222
223                 if( !NT_SUCCESS( stIoStatus.Status))
224                 {
225
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,
233                                   stIoStatus.Status,
234                                   stIoStatus.Information);
235
236                     ntStatus = stIoStatus.Status;
237                 }
238
239                 stUnlockRequestCB.Count = 1;
240
241                 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
242
243                 stUnlockRequestCB.Request[ 0].LockType = AFS_BYTE_RANGE_LOCK_TYPE_EXCL;
244
245                 stUnlockRequestCB.Request[ 0].Offset = pIrpSp->Parameters.LockControl.ByteOffset;
246
247                 stUnlockRequestCB.Request[ 0].Length.QuadPart = pIrpSp->Parameters.LockControl.Length->QuadPart;
248
249                 ulResultLen = sizeof( AFSByteRangeUnlockResultCB);
250
251                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK,
252                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
253                                               &pCcb->AuthGroup,
254                                               &pCcb->DirectoryCB->NameInformation.FileName,
255                                               &pFcb->ObjectInformation->FileId,
256                                               (void *)&stUnlockRequestCB,
257                                               sizeof( AFSByteRangeUnlockRequestCB),
258                                               (void *)&stUnlockResultCB,
259                                               &ulResultLen);
260
261                 break;
262             }
263
264             default:
265
266                 break;
267         }
268
269         //
270         // Below here we won't complete the request, it is handled by the lock package
271         //
272
273         bCompleteRequest = FALSE;
274
275         //
276         //  Now call the system package for actually processing the lock request
277         //
278
279         ntStatus = FsRtlProcessFileLock( &pFcb->Specific.File.FileLock,
280                                          Irp,
281                                          NULL);
282
283 try_exit:
284
285         if( bReleaseResource)
286         {
287
288             //
289             // And drop it
290             //
291
292             AFSReleaseResource( &pFcb->NPFcb->Resource);
293         }
294
295         if( bCompleteRequest)
296         {
297
298             AFSCompleteRequest( Irp,
299                                 ntStatus);
300         }
301     }
302     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()))
303     {
304
305         AFSDbgLogMsg( 0,
306                       0,
307                       "EXCEPTION - AFSLockControl\n");
308
309         //
310         // Again, there is little point in failing this request but pass back some type of failure status
311         //
312
313         ntStatus = STATUS_UNSUCCESSFUL;
314
315         AFSCompleteRequest( Irp,
316                             ntStatus);
317     }
318
319     return ntStatus;
320 }