Windows: AFSPerformObjectInvalidate hold ExtentsResource shared
[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                 RtlZeroMemory( &stUnlockRequestCB,
167                                sizeof( AFSByteRangeUnlockRequestCB));
168
169                 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
170
171                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK_ALL,
172                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
173                                               &pCcb->AuthGroup,
174                                               &pCcb->DirectoryCB->NameInformation.FileName,
175                                               &pFcb->ObjectInformation->FileId,
176                                               (void *)&stUnlockRequestCB,
177                                               sizeof( AFSByteRangeUnlockRequestCB),
178                                               NULL,
179                                               NULL);
180
181                 if( NT_SUCCESS( ntStatus))
182                 {
183                     CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
184                                   NULL,
185                                   0,
186                                   &stIoStatus);
187
188                     if( !NT_SUCCESS( stIoStatus.Status))
189                     {
190
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,
198                                       stIoStatus.Status,
199                                       stIoStatus.Information);
200
201                         ntStatus = stIoStatus.Status;
202                     }
203                 }
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                 stUnlockRequestCB.Count = 1;
216
217                 stUnlockRequestCB.ProcessId = (ULONGLONG)PsGetCurrentProcessId();
218
219                 stUnlockRequestCB.Request[ 0].LockType = AFS_BYTE_RANGE_LOCK_TYPE_EXCL;
220
221                 stUnlockRequestCB.Request[ 0].Offset = pIrpSp->Parameters.LockControl.ByteOffset;
222
223                 stUnlockRequestCB.Request[ 0].Length.QuadPart = pIrpSp->Parameters.LockControl.Length->QuadPart;
224
225                 ulResultLen = sizeof( AFSByteRangeUnlockResultCB);
226
227                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK,
228                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
229                                               &pCcb->AuthGroup,
230                                               &pCcb->DirectoryCB->NameInformation.FileName,
231                                               &pFcb->ObjectInformation->FileId,
232                                               (void *)&stUnlockRequestCB,
233                                               sizeof( AFSByteRangeUnlockRequestCB),
234                                               (void *)&stUnlockResultCB,
235                                               &ulResultLen);
236
237                 if( NT_SUCCESS( ntStatus))
238                 {
239                     CcFlushCache( &pFcb->NPFcb->SectionObjectPointers,
240                                   &pIrpSp->Parameters.LockControl.ByteOffset,
241                                   pIrpSp->Parameters.LockControl.Length->LowPart,
242                                   &stIoStatus);
243
244                     if( !NT_SUCCESS( stIoStatus.Status))
245                     {
246
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,
254                                       stIoStatus.Status,
255                                       stIoStatus.Information);
256
257                         ntStatus = stIoStatus.Status;
258                     }
259                 }
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 }