Windows: RDR File System Framework driver
[openafs.git] / src / WINNT / afsrdr / kernel / fs / AFSCommSupport.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: AFSCommSupport.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 NTSTATUS
42 AFSReleaseFid( IN AFSFileID *FileId)
43 {
44
45     NTSTATUS ntStatus = STATUS_SUCCESS;
46
47     __Enter
48     {
49
50         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FID,
51                                       0,
52                                       NULL,
53                                       NULL,
54                                       FileId,
55                                       NULL,
56                                       0,
57                                       NULL,
58                                       NULL);
59     }
60
61     return ntStatus;
62 }
63
64 NTSTATUS
65 AFSProcessRequest( IN ULONG RequestType,
66                    IN ULONG RequestFlags,
67                    IN GUID *AuthGroup,
68                    IN PUNICODE_STRING FileName,
69                    IN AFSFileID *FileId,
70                    IN void  *Data,
71                    IN ULONG DataLength,
72                    IN OUT void *ResultBuffer,
73                    IN OUT PULONG ResultBufferLength)
74 {
75
76     NTSTATUS         ntStatus = STATUS_SUCCESS;
77     AFSPoolEntry     stPoolEntry, *pPoolEntry = NULL;
78     AFSCommSrvcCB   *pCommSrvc = NULL;
79     BOOLEAN          bReleasePool = FALSE;
80     AFSDeviceExt    *pControlDevExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
81     AFSDeviceExt    *pRDRDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
82     BOOLEAN          bWait = BooleanFlagOn( RequestFlags, AFS_REQUEST_FLAG_SYNCHRONOUS);
83     ULONG            ulPoolEntryLength = 0;
84     BOOLEAN          bDecrementCount = FALSE;
85
86     __try
87     {
88
89         if( BooleanFlagOn( pRDRDevExt->DeviceFlags, AFS_DEVICE_FLAG_REDIRECTOR_SHUTDOWN))
90         {
91             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
92         }
93
94         if( InterlockedIncrement( &pControlDevExt->Specific.Control.OutstandingServiceRequestCount) == 1)
95         {
96             KeClearEvent( &pControlDevExt->Specific.Control.OutstandingServiceRequestEvent);
97         }
98
99         bDecrementCount = TRUE;
100
101         pCommSrvc = &pControlDevExt->Specific.Control.CommServiceCB;
102
103         //
104         // Grab the pool resource and check the state
105         //
106
107         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
108                       AFS_TRACE_LEVEL_VERBOSE,
109                       "AFSProcessRequest Acquiring IrpPoolLock lock %08lX EXCL %08lX\n",
110                       &pCommSrvc->IrpPoolLock,
111                       PsGetCurrentThread());
112
113         AFSAcquireExcl( &pCommSrvc->IrpPoolLock,
114                         TRUE);
115
116         bReleasePool = TRUE;
117
118         if( pCommSrvc->IrpPoolControlFlag != POOL_ACTIVE)
119         {
120
121             //
122             // Pool not running so bail.
123             //
124
125             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
126         }
127
128         //
129         // If this is an async request we need to allocate a pool entry for the request
130         //
131
132         pPoolEntry = &stPoolEntry;
133
134         if( !bWait)
135         {
136
137             ASSERT( ResultBuffer == NULL);
138
139             ulPoolEntryLength = sizeof( AFSPoolEntry) + QuadAlign( DataLength);
140
141             if( FileName != NULL)
142             {
143
144                 ulPoolEntryLength += FileName->Length;
145             }
146
147             pPoolEntry = (AFSPoolEntry *)AFSExAllocatePoolWithTag( NonPagedPool,
148                                                                    ulPoolEntryLength,
149                                                                    AFS_POOL_ENTRY_TAG);
150
151             if( pPoolEntry == NULL)
152             {
153
154                 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
155             }
156
157             RtlZeroMemory( pPoolEntry,
158                            ulPoolEntryLength);
159
160             pPoolEntry->Data = (void *)((char *)pPoolEntry + sizeof( AFSPoolEntry));
161
162             pPoolEntry->FileName.Buffer = (WCHAR *)((char *)pPoolEntry->Data + DataLength);
163         }
164         else
165         {
166
167             RtlZeroMemory( pPoolEntry,
168                            sizeof( AFSPoolEntry));
169
170             KeInitializeEvent( &pPoolEntry->Event,
171                                NotificationEvent,
172                                FALSE);
173         }
174
175         pPoolEntry->RequestType = RequestType;
176
177         pPoolEntry->RequestIndex = pCommSrvc->IrpPoolRequestIndex++;
178
179         pPoolEntry->RequestFlags = RequestFlags;
180
181         pPoolEntry->ResultBufferLength = 0;
182
183         if( FileId != NULL)
184         {
185
186             pPoolEntry->FileId = *FileId;
187         }
188
189         pPoolEntry->FileName.Length = 0;
190
191         if( FileName != NULL)
192         {
193
194             if( bWait)
195             {
196
197                 pPoolEntry->FileName = *FileName;
198             }
199             else
200             {
201
202                 pPoolEntry->FileName.Length = FileName->Length;
203
204                 pPoolEntry->FileName.MaximumLength = pPoolEntry->FileName.Length;
205
206                 RtlCopyMemory( pPoolEntry->FileName.Buffer,
207                                FileName->Buffer,
208                                pPoolEntry->FileName.Length);
209             }
210         }
211
212         //
213         // Move in the data if there is some
214         //
215
216         pPoolEntry->DataLength = DataLength;
217
218         if( Data != NULL &&
219             DataLength > 0)
220         {
221
222             if( bWait)
223             {
224
225                 pPoolEntry->Data = Data;
226             }
227             else
228             {
229
230                 RtlCopyMemory( pPoolEntry->Data,
231                                Data,
232                                DataLength);
233             }
234         }
235
236         pPoolEntry->ResultBuffer = ResultBuffer;
237
238         pPoolEntry->ResultBufferLength = ResultBufferLength;
239
240         //
241         // Store off the auth group
242         //
243
244         if( AuthGroup == NULL)
245         {
246             AFSRetrieveAuthGroup( (ULONGLONG)PsGetCurrentProcessId(),
247                                   (ULONGLONG)PsGetCurrentThreadId(),
248                                   &pPoolEntry->AuthGroup);
249         }
250         else
251         {
252             RtlCopyMemory( &pPoolEntry->AuthGroup,
253                            AuthGroup,
254                            sizeof( GUID));
255         }
256
257         if( AFSIsLocalSystemAuthGroup( &pPoolEntry->AuthGroup))
258         {
259             SetFlag( pPoolEntry->RequestFlags, AFS_REQUEST_LOCAL_SYSTEM_PAG);
260         }
261
262         if( AFSIsNoPAGAuthGroup( &pPoolEntry->AuthGroup))
263         {
264             AFSDbgLogMsg( 0,
265                           0,
266                           "AFSProcessRequest NoPAG Auth Group %08lX\n",
267                           PsGetCurrentThread());
268         }
269
270         //
271         // Indicate the type of process
272         //
273
274 #ifdef AMD64
275
276         if( !AFSIs64BitProcess( (ULONGLONG)PsGetCurrentProcessId()))
277         {
278             SetFlag( pPoolEntry->RequestFlags, AFS_REQUEST_FLAG_WOW64);
279         }
280
281 #endif
282
283         //
284         // Insert the entry into the request pool
285         //
286
287         ntStatus = AFSInsertRequest( pCommSrvc,
288                                      pPoolEntry);
289
290         if( !NT_SUCCESS( ntStatus))
291         {
292
293             if( !bWait)
294             {
295
296                 ExFreePool( pPoolEntry);
297             }
298
299             try_return( ntStatus);
300         }
301
302         //
303         // Drop the lock on the pool prior to waiting
304         //
305
306         AFSReleaseResource( &pCommSrvc->IrpPoolLock);
307
308         bReleasePool = FALSE;
309
310         //
311         // Wait for the result if this is NOT an asynchronous request
312         //
313
314         if( bWait)
315         {
316
317             //
318             // Wait for the result of the request. We specify no timeout ...
319             //
320
321             ntStatus = KeWaitForSingleObject( &pPoolEntry->Event,
322                                               Executive,
323                                               KernelMode,
324                                               FALSE,
325                                               NULL);
326
327             //
328             // Process the result of the request
329             //
330
331             if( ntStatus == STATUS_SUCCESS)
332             {
333
334                 ntStatus = pPoolEntry->ResultStatus;
335             }
336             else
337             {
338
339                 ntStatus = STATUS_DEVICE_NOT_READY;
340             }
341         }
342
343 try_exit:
344
345         if( bReleasePool)
346         {
347
348             AFSReleaseResource( &pCommSrvc->IrpPoolLock);
349         }
350
351         if( bDecrementCount &&
352             InterlockedDecrement( &pControlDevExt->Specific.Control.OutstandingServiceRequestCount) == 0)
353         {
354             KeSetEvent( &pControlDevExt->Specific.Control.OutstandingServiceRequestEvent,
355                         0,
356                         FALSE);
357         }
358     }
359     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()))
360     {
361
362         if( bReleasePool)
363         {
364
365             AFSReleaseResource( &pCommSrvc->IrpPoolLock);
366         }
367
368         if( bDecrementCount &&
369             InterlockedDecrement( &pControlDevExt->Specific.Control.OutstandingServiceRequestCount) == 0)
370         {
371             KeSetEvent( &pControlDevExt->Specific.Control.OutstandingServiceRequestEvent,
372                         0,
373                         FALSE);
374         }
375
376         if ( ntStatus == STATUS_SUCCESS)
377         {
378
379             ntStatus = STATUS_UNSUCCESSFUL;
380         }
381     }
382
383     return ntStatus;
384 }
385
386 NTSTATUS
387 AFSProcessControlRequest( IN PIRP Irp)
388 {
389
390     NTSTATUS            ntStatus = STATUS_SUCCESS;
391     PIO_STACK_LOCATION  pIrpSp;
392     ULONG               ulIoControlCode;
393     BOOLEAN             bCompleteRequest = TRUE;
394     AFSDeviceExt       *pDevExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
395     ULONG               ulBytesProcessed = 0;
396
397     __try
398     {
399
400         pIrpSp = IoGetCurrentIrpStackLocation( Irp);
401
402         ulIoControlCode = pIrpSp->Parameters.DeviceIoControl.IoControlCode;
403
404         switch( ulIoControlCode)
405         {
406
407             case IOCTL_AFS_INITIALIZE_CONTROL_DEVICE:
408             {
409
410                 //
411                 // Go intialize the pool
412                 //
413
414                 ntStatus = AFSInitIrpPool();
415
416                 if( !NT_SUCCESS( ntStatus))
417                 {
418
419                     //
420                     // Don't initialize
421                     //
422
423                     break;
424                 }
425
426                 //
427                 // Tag this instance as the one to close the irp pool when it is closed
428                 //
429
430                 pIrpSp->FileObject->FsContext = (void *)((ULONG_PTR)pIrpSp->FileObject->FsContext | AFS_CONTROL_INSTANCE);
431
432                 break;
433             }
434
435             case IOCTL_AFS_INITIALIZE_REDIRECTOR_DEVICE:
436             {
437
438                 AFSRedirectorInitInfo *pRedirInitInfo = (AFSRedirectorInitInfo *)Irp->AssociatedIrp.SystemBuffer;
439
440                 //
441                 // Extract off the passed in information which contains the
442                 // cache file parameters
443                 //
444
445                 if( pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSRedirectorInitInfo) ||
446                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < (ULONG)FIELD_OFFSET( AFSRedirectorInitInfo, CacheFileName) +
447                                                                                                     pRedirInitInfo->CacheFileNameLength)
448                 {
449
450                     ntStatus = STATUS_INVALID_PARAMETER;
451
452                     break;
453                 }
454
455                 //
456                 // Initialize the Redirector device
457                 //
458
459                 ntStatus = AFSInitializeRedirector( pRedirInitInfo);
460
461                 if( !NT_SUCCESS( ntStatus))
462                 {
463
464                     break;
465                 }
466
467                 //
468                 // Stash away context so we know the instance used to initialize the redirector
469                 //
470
471                 pIrpSp->FileObject->FsContext = (void *)((ULONG_PTR)pIrpSp->FileObject->FsContext | AFS_REDIRECTOR_INSTANCE);
472
473                 break;
474             }
475
476             case IOCTL_AFS_PROCESS_IRP_REQUEST:
477             {
478
479                 ntStatus = AFSProcessIrpRequest( Irp);
480
481                 break;
482             }
483
484             case IOCTL_AFS_PROCESS_IRP_RESULT:
485             {
486
487                 ntStatus = AFSProcessIrpResult( Irp);
488
489                 break;
490             }
491
492             case IOCTL_AFS_SYSNAME_NOTIFICATION:
493             {
494
495                 AFSSysNameNotificationCB *pSysNameInfo = (AFSSysNameNotificationCB *)Irp->AssociatedIrp.SystemBuffer;
496
497                 if( pSysNameInfo == NULL ||
498                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSSysNameNotificationCB))
499                 {
500
501                     ntStatus = STATUS_INVALID_PARAMETER;
502
503                     break;
504                 }
505
506                 ntStatus = AFSSetSysNameInformation( pSysNameInfo,
507                                                      pIrpSp->Parameters.DeviceIoControl.InputBufferLength);
508
509                 break;
510             }
511
512             case IOCTL_AFS_CONFIGURE_DEBUG_TRACE:
513             {
514
515                 AFSTraceConfigCB *pTraceInfo = (AFSTraceConfigCB *)Irp->AssociatedIrp.SystemBuffer;
516
517                 if( pTraceInfo == NULL ||
518                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSTraceConfigCB))
519                 {
520
521                     ntStatus = STATUS_INVALID_PARAMETER;
522
523                     break;
524                 }
525
526                 ntStatus = AFSConfigureTrace( pTraceInfo);
527
528                 break;
529             }
530
531             case IOCTL_AFS_GET_TRACE_BUFFER:
532             {
533
534                 if( pIrpSp->Parameters.DeviceIoControl.OutputBufferLength == 0)
535                 {
536
537                     ntStatus = STATUS_INVALID_PARAMETER;
538
539                     break;
540                 }
541
542                 ntStatus = AFSGetTraceBuffer( pIrpSp->Parameters.DeviceIoControl.OutputBufferLength,
543                                               Irp->AssociatedIrp.SystemBuffer,
544                                               &Irp->IoStatus.Information);
545
546                 break;
547             }
548
549             case IOCTL_AFS_FORCE_CRASH:
550             {
551
552 #if DBG
553
554                 if( BooleanFlagOn( AFSDebugFlags, AFS_DBG_FLAG_ENABLE_FORCE_CRASH))
555                 {
556
557                     KeBugCheck( (ULONG)-1);
558                 }
559 #endif
560
561                 break;
562             }
563
564 #ifdef NOT_IMPLEMENTED
565             case IOCTL_AFS_LOAD_LIBRARY:
566             {
567
568                 AFSLoadLibraryCB *pLoadLib = (AFSLoadLibraryCB *)Irp->AssociatedIrp.SystemBuffer;
569                 UNICODE_STRING uniServicePath;
570
571                 if( pLoadLib == NULL ||
572                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSLoadLibraryCB) ||
573                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < (ULONG)FIELD_OFFSET( AFSLoadLibraryCB, LibraryServicePath) +
574                                                                                                     pLoadLib->LibraryServicePathLength)
575                 {
576
577                     ntStatus = STATUS_INVALID_PARAMETER;
578
579                     break;
580                 }
581
582                 uniServicePath.Length = pLoadLib->LibraryServicePathLength;
583                 uniServicePath.MaximumLength = uniServicePath.Length;
584
585                 uniServicePath.Buffer = pLoadLib->LibraryServicePath;
586
587                 if( uniServicePath.Length == 0)
588                 {
589
590                     ntStatus = STATUS_INVALID_PARAMETER;
591
592                     break;
593                 }
594
595                 ntStatus = AFSLoadLibrary( pLoadLib->Flags,
596                                            &uniServicePath);
597
598                 if( NT_SUCCESS( ntStatus))
599                 {
600
601                     //
602                     // Intialize the library
603                     //
604
605                     ntStatus = AFSInitializeLibrary( NULL,
606                                                      FALSE);
607                 }
608
609                 break;
610             }
611
612             case IOCTL_AFS_UNLOAD_LIBRARY:
613             {
614
615                 //
616                 // Try to unload the library we currently have in place
617                 //
618
619                 ntStatus = AFSUnloadLibrary( FALSE);
620
621                 break;
622             }
623 #endif
624
625             case IOCTL_AFS_SHUTDOWN:
626             {
627
628                 ntStatus = AFSShutdownRedirector();
629
630                 break;
631             }
632
633             case IOCTL_AFS_AUTHGROUP_CREATE_AND_SET:
634             {
635
636
637                 AFSAuthGroupRequestCB *pAuthGroupRequestCB = (AFSAuthGroupRequestCB *)Irp->AssociatedIrp.SystemBuffer;
638
639                 if( pAuthGroupRequestCB == NULL ||
640                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSAuthGroupRequestCB))
641                 {
642
643                     ntStatus = STATUS_INVALID_PARAMETER;
644
645                     break;
646                 }
647
648                 ntStatus = AFSCreateSetProcessAuthGroup( pAuthGroupRequestCB);
649
650                 break;
651             }
652
653             case IOCTL_AFS_AUTHGROUP_QUERY:
654             {
655
656                 ntStatus = AFSQueryProcessAuthGroupList( ( GUID *)Irp->AssociatedIrp.SystemBuffer,
657                                                          pIrpSp->Parameters.DeviceIoControl.OutputBufferLength,
658                                                          &Irp->IoStatus.Information);
659
660                 break;
661             }
662
663             case IOCTL_AFS_AUTHGROUP_SET:
664             {
665
666                 AFSAuthGroupRequestCB *pAuthGroupRequestCB = (AFSAuthGroupRequestCB *)Irp->AssociatedIrp.SystemBuffer;
667
668                 if( pAuthGroupRequestCB == NULL ||
669                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSAuthGroupRequestCB))
670                 {
671
672                     ntStatus = STATUS_INVALID_PARAMETER;
673
674                     break;
675                 }
676
677                 ntStatus = AFSSetActiveProcessAuthGroup( pAuthGroupRequestCB);
678
679                 break;
680             }
681
682             case IOCTL_AFS_AUTHGROUP_RESET:
683             {
684
685                 AFSAuthGroupRequestCB *pAuthGroupRequestCB = (AFSAuthGroupRequestCB *)Irp->AssociatedIrp.SystemBuffer;
686
687                 if( pAuthGroupRequestCB == NULL ||
688                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSAuthGroupRequestCB))
689                 {
690
691                     ntStatus = STATUS_INVALID_PARAMETER;
692
693                     break;
694                 }
695
696                 ntStatus = AFSResetActiveProcessAuthGroup( pAuthGroupRequestCB);
697
698                 break;
699             }
700
701             case IOCTL_AFS_AUTHGROUP_LOGON_CREATE:
702             case IOCTL_AFS_AUTHGROUP_SID_CREATE:
703             {
704
705                 AFSAuthGroupRequestCB *pAuthGroupRequestCB = (AFSAuthGroupRequestCB *)Irp->AssociatedIrp.SystemBuffer;
706
707                 if( pAuthGroupRequestCB != NULL &&
708                     pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSAuthGroupRequestCB))
709                 {
710
711                     ntStatus = STATUS_INVALID_PARAMETER;
712
713                     break;
714                 }
715
716                 ntStatus = AFSCreateAuthGroupForSIDorLogonSession( pAuthGroupRequestCB,
717                                                                    ulIoControlCode == IOCTL_AFS_AUTHGROUP_LOGON_CREATE);
718
719                 break;
720             }
721
722             case IOCTL_AFS_AUTHGROUP_SID_QUERY:
723             {
724
725                 AFSAuthGroupRequestCB *pAuthGroupRequestCB = NULL;
726
727                 if( pIrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof( GUID))
728                 {
729                     ntStatus = STATUS_INVALID_PARAMETER;
730                     break;
731                 }
732
733                 if( pIrpSp->Parameters.DeviceIoControl.InputBufferLength >= sizeof( AFSAuthGroupRequestCB))
734                 {
735                     pAuthGroupRequestCB = (AFSAuthGroupRequestCB *)Irp->AssociatedIrp.SystemBuffer;
736                 }
737
738                 ntStatus = AFSQueryAuthGroup( pAuthGroupRequestCB,
739                                               (GUID *)Irp->AssociatedIrp.SystemBuffer,
740                                               &Irp->IoStatus.Information);
741
742                 break;
743             }
744
745             default:
746             {
747
748                 //
749                 // Check the state of the library
750                 //
751
752                 ntStatus = AFSCheckLibraryState( Irp);
753
754                 if( !NT_SUCCESS( ntStatus) ||
755                     ntStatus == STATUS_PENDING)
756                 {
757
758                     if( ntStatus == STATUS_PENDING)
759                     {
760                         bCompleteRequest = FALSE;
761                     }
762
763                     break;
764                 }
765
766                 bCompleteRequest = FALSE;
767
768                 IoSkipCurrentIrpStackLocation( Irp);
769
770                 ntStatus = IoCallDriver( pDevExt->Specific.Control.LibraryDeviceObject,
771                                          Irp);
772
773                 //
774                 // Indicate the library is done with the request
775                 //
776
777                 AFSClearLibraryRequest();
778
779                 break;
780             }
781         }
782
783 //try_exit:
784
785     }
786     __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()))
787     {
788
789         ntStatus = STATUS_UNSUCCESSFUL;
790     }
791
792     if( bCompleteRequest)
793     {
794
795         Irp->IoStatus.Status = ntStatus;
796
797         AFSCompleteRequest( Irp,
798                               ntStatus);
799     }
800
801     return ntStatus;
802 }
803
804 NTSTATUS
805 AFSInitIrpPool()
806 {
807
808     NTSTATUS       ntStatus = STATUS_SUCCESS;
809     AFSCommSrvcCB *pCommSrvc = NULL;
810     BOOLEAN        bReleasePools = FALSE;
811     AFSDeviceExt  *pDevExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
812
813     __Enter
814     {
815
816         pCommSrvc = &pDevExt->Specific.Control.CommServiceCB;
817
818         //
819         // Whenever we change state we must grab both pool locks. On the checking of the state
820         // within the processing routines for these respective pools, we only grab one lock to
821         // minimize serialization. The ordering is always the Irp pool then the result pool
822         // locks. We also do this in the tear down of the pool
823         //
824
825         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
826                       AFS_TRACE_LEVEL_VERBOSE,
827                       "AFSInitIrpPool Acquiring IrpPoolLock lock %08lX EXCL %08lX\n",
828                       &pCommSrvc->IrpPoolLock,
829                       PsGetCurrentThread());
830
831         AFSAcquireExcl( &pCommSrvc->IrpPoolLock,
832                           TRUE);
833
834         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
835                       AFS_TRACE_LEVEL_VERBOSE,
836                       "AFSInitIrpPool Acquiring ResultPoolLock lock %08lX EXCL %08lX\n",
837                       &pCommSrvc->ResultPoolLock,
838                       PsGetCurrentThread());
839
840         AFSAcquireExcl( &pCommSrvc->ResultPoolLock,
841                           TRUE);
842
843         bReleasePools = TRUE;
844
845         //
846         // The pool can be either ACTIVE or INACTIVE. If the pool state is INACTIVE and we
847         // are receiving the INIT request, then activate it. If the pool is ACTIVE, then we
848         // shouldn't be getting this request ...
849         //
850
851         if( pCommSrvc->IrpPoolControlFlag == POOL_ACTIVE)
852         {
853
854             //
855             // We have already been activated so just fail this request
856             //
857
858             try_return( ntStatus = STATUS_INVALID_PARAMETER);
859         }
860         else if( pCommSrvc->IrpPoolControlFlag == POOL_INACTIVE)
861         {
862
863             //
864             // The pool is currently INACTIVE so start it up and ready it to
865             // receive irp requests
866             //
867
868             pCommSrvc->IrpPoolControlFlag = POOL_ACTIVE;
869
870             pDevExt->Specific.Control.ServiceProcess = (PKPROCESS)PsGetCurrentProcess();
871
872             try_return( ntStatus = STATUS_SUCCESS);
873         }
874         else
875         {
876
877             //
878             // The pool is in some mixed state, fail the request.
879             //
880
881             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
882         }
883
884 try_exit:
885
886         if( bReleasePools)
887         {
888
889             AFSReleaseResource( &pCommSrvc->IrpPoolLock);
890
891             AFSReleaseResource( &pCommSrvc->ResultPoolLock);
892         }
893     }
894
895     return ntStatus;
896 }
897
898 void
899 AFSCleanupIrpPool()
900 {
901
902     NTSTATUS        ntStatus = STATUS_SUCCESS;
903     AFSPoolEntry   *pEntry = NULL, *pNextEntry = NULL;
904     AFSDeviceExt   *pDevExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
905     AFSCommSrvcCB  *pCommSrvc = (AFSCommSrvcCB *)&pDevExt->Specific.Control.CommServiceCB;
906
907     __Enter
908     {
909
910         //
911         // When we change the state, grab both pool locks exclusive. The order is always the
912         // Irp pool then the result pool lock
913         //
914
915         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
916                       AFS_TRACE_LEVEL_VERBOSE,
917                       "AFSCleanupIrpPool Acquiring IrpPoolLock lock %08lX EXCL %08lX\n",
918                       &pCommSrvc->IrpPoolLock,
919                       PsGetCurrentThread());
920
921         AFSAcquireExcl( &pCommSrvc->IrpPoolLock,
922                         TRUE);
923
924         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
925                       AFS_TRACE_LEVEL_VERBOSE,
926                       "AFSCleanupIrpPool Acquiring ResultPoolLock lock %08lX EXCL %08lX\n",
927                       &pCommSrvc->ResultPoolLock,
928                       PsGetCurrentThread());
929
930         AFSAcquireExcl( &pCommSrvc->ResultPoolLock,
931                         TRUE);
932
933         //
934         // Indicate we are pending stop
935         //
936
937         pCommSrvc->IrpPoolControlFlag = POOL_INACTIVE;
938
939         //
940         // Set the event to release any waiting workers
941         //
942
943         KeSetEvent( &pCommSrvc->IrpPoolHasEntries,
944                     0,
945                     FALSE);
946
947         KeSetEvent( &pCommSrvc->IrpPoolHasReleaseEntries,
948                     0,
949                     FALSE);
950
951         //
952         // Go through the pool entries and free up the structures.
953         //
954
955         pEntry = pCommSrvc->RequestPoolHead;
956
957         while( pEntry != NULL)
958         {
959
960             pNextEntry = pEntry->fLink;
961
962             if( BooleanFlagOn( pEntry->RequestFlags, AFS_REQUEST_FLAG_SYNCHRONOUS))
963             {
964
965                 //
966                 // Here we need to complete the irp, cancelled, and delete the data block
967                 //
968
969                 pEntry->ResultStatus = STATUS_CANCELLED;
970
971                 KeSetEvent( &pEntry->Event,
972                             0,
973                             FALSE);
974             }
975             else
976             {
977
978                 ExFreePool( pEntry);
979             }
980
981             pEntry = pNextEntry;
982         }
983
984         //
985         // Cleanup the control structure for the request pool
986         //
987
988         pCommSrvc->RequestPoolHead = NULL;
989
990         pCommSrvc->RequestPoolTail = NULL;
991
992         pCommSrvc->IrpPoolRequestIndex = 1;
993
994         KeClearEvent( &pCommSrvc->IrpPoolHasEntries);
995
996         KeClearEvent( &pCommSrvc->IrpPoolHasReleaseEntries);
997
998         //
999         // Release the irp pool lock.
1000         //
1001
1002         AFSReleaseResource( &pCommSrvc->IrpPoolLock);
1003
1004         //
1005         // Go through the result pool entries and free up the structures.
1006         //
1007
1008         pEntry = pCommSrvc->ResultPoolHead;
1009
1010         while( pEntry != NULL)
1011         {
1012
1013             pNextEntry = pEntry->fLink;
1014
1015             pEntry->ResultStatus = STATUS_CANCELLED;
1016
1017             //
1018             // Here we will set the event of the requestor and let the blocked thread
1019             // free the data block
1020             //
1021
1022             KeSetEvent( &pEntry->Event,
1023                         0,
1024                         FALSE);
1025
1026             //
1027             // Go onto the next entry
1028             //
1029
1030             pEntry = pNextEntry;
1031         }
1032
1033         //
1034         // Cleanup the control structure for the result pool
1035         //
1036
1037         pCommSrvc->ResultPoolHead = NULL;
1038
1039         pCommSrvc->ResultPoolTail = NULL;
1040
1041         //
1042         // Release the result pool lock.
1043         //
1044
1045         AFSReleaseResource( &pCommSrvc->ResultPoolLock);
1046     }
1047
1048     return;
1049 }
1050
1051 NTSTATUS
1052 AFSInsertRequest( IN AFSCommSrvcCB *CommSrvc,
1053                   IN AFSPoolEntry *Entry)
1054 {
1055
1056     NTSTATUS ntStatus = STATUS_SUCCESS;
1057
1058     __Enter
1059     {
1060
1061         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1062                       AFS_TRACE_LEVEL_VERBOSE,
1063                       "AFSInsertRequest Acquiring IrpPoolLock lock %08lX EXCL %08lX\n",
1064                       &CommSrvc->IrpPoolLock,
1065                       PsGetCurrentThread());
1066
1067         AFSAcquireExcl( &CommSrvc->IrpPoolLock,
1068                         TRUE);
1069
1070         if( CommSrvc->IrpPoolControlFlag != POOL_ACTIVE)
1071         {
1072
1073             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
1074         }
1075
1076         if( CommSrvc->RequestPoolHead == NULL)
1077         {
1078
1079             CommSrvc->RequestPoolHead = Entry;
1080         }
1081         else
1082         {
1083
1084             CommSrvc->RequestPoolTail->fLink = Entry;
1085
1086             Entry->bLink = CommSrvc->RequestPoolTail;
1087         }
1088
1089         CommSrvc->RequestPoolTail = Entry;
1090
1091         if( Entry->RequestType == AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS)
1092         {
1093
1094             KeSetEvent( &CommSrvc->IrpPoolHasReleaseEntries,
1095                         0,
1096                         FALSE);
1097         }
1098         else
1099         {
1100
1101             KeSetEvent( &CommSrvc->IrpPoolHasEntries,
1102                         0,
1103                         FALSE);
1104         }
1105
1106         InterlockedIncrement( &CommSrvc->QueueCount);
1107
1108 try_exit:
1109
1110         AFSReleaseResource( &CommSrvc->IrpPoolLock);
1111     }
1112
1113     return ntStatus;
1114 }
1115
1116 NTSTATUS
1117 AFSProcessIrpRequest( IN PIRP Irp)
1118 {
1119
1120     NTSTATUS         ntStatus = STATUS_SUCCESS;
1121     AFSDeviceExt    *pDevExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
1122     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1123     AFSCommSrvcCB   *pCommSrvc = NULL;
1124     AFSPoolEntry    *pEntry = NULL, *pPrevEntry = NULL;
1125     AFSCommRequest  *pRequest = NULL;
1126     BOOLEAN          bReleaseRequestThread = FALSE;
1127
1128     __Enter
1129     {
1130
1131         pCommSrvc = &pDevExt->Specific.Control.CommServiceCB;
1132
1133         pRequest = (AFSCommRequest *)Irp->AssociatedIrp.SystemBuffer;
1134
1135         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1136                       AFS_TRACE_LEVEL_VERBOSE,
1137                       "AFSProcessIrpRequest Acquiring IrpPoolLock lock %08lX EXCL %08lX\n",
1138                       &pCommSrvc->IrpPoolLock,
1139                       PsGetCurrentThread());
1140
1141         AFSAcquireExcl( &pCommSrvc->IrpPoolLock,
1142                         TRUE);
1143
1144         if( pCommSrvc->IrpPoolControlFlag != POOL_ACTIVE)
1145         {
1146
1147             AFSReleaseResource( &pCommSrvc->IrpPoolLock);
1148
1149             try_return( ntStatus = STATUS_DEVICE_NOT_READY);
1150         }
1151
1152         AFSReleaseResource( &pCommSrvc->IrpPoolLock);
1153
1154         //
1155         // Is this a dedicated flush thread?
1156         //
1157
1158         if( BooleanFlagOn( pRequest->RequestFlags, AFS_REQUEST_RELEASE_THREAD))
1159         {
1160
1161             bReleaseRequestThread = TRUE;
1162         }
1163
1164         //
1165         // Wait on the 'have items' event until we can retrieve an item
1166         //
1167
1168         while( TRUE)
1169         {
1170
1171             if( bReleaseRequestThread)
1172             {
1173
1174                 ntStatus = KeWaitForSingleObject( &pCommSrvc->IrpPoolHasReleaseEntries,
1175                                                   UserRequest,
1176                                                   UserMode,
1177                                                   TRUE,
1178                                                   NULL);
1179             }
1180             else
1181             {
1182
1183                 ntStatus = KeWaitForSingleObject( &pCommSrvc->IrpPoolHasEntries,
1184                                                   UserRequest,
1185                                                   UserMode,
1186                                                   TRUE,
1187                                                   NULL);
1188             }
1189
1190             if( ntStatus != STATUS_SUCCESS)
1191             {
1192
1193                 ntStatus = STATUS_DEVICE_NOT_READY;
1194
1195                 break;
1196             }
1197
1198             //
1199             // Grab the lock on the request pool
1200             //
1201
1202             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1203                           AFS_TRACE_LEVEL_VERBOSE,
1204                           "AFSProcessIrpRequest Acquiring IrpPoolLock (WAIT) lock %08lX EXCL %08lX\n",
1205                           &pCommSrvc->IrpPoolLock,
1206                           PsGetCurrentThread());
1207
1208             AFSAcquireExcl( &pCommSrvc->IrpPoolLock,
1209                             TRUE);
1210
1211             if( pCommSrvc->IrpPoolControlFlag != POOL_ACTIVE)
1212             {
1213
1214                 AFSReleaseResource( &pCommSrvc->IrpPoolLock);
1215
1216                 try_return( ntStatus = STATUS_DEVICE_NOT_READY);
1217             }
1218
1219             //
1220             // If this is a dedicated flush thread only look for a flush request in the queue
1221             //
1222
1223             if( bReleaseRequestThread)
1224             {
1225
1226                 pEntry = pCommSrvc->RequestPoolHead;
1227
1228                 pPrevEntry = NULL;
1229
1230                 while( pEntry != NULL)
1231                 {
1232
1233                     if( pEntry->RequestType == AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS)
1234                     {
1235
1236                         if( pPrevEntry == NULL)
1237                         {
1238
1239                             pCommSrvc->RequestPoolHead = pEntry->fLink;
1240
1241                             if( pCommSrvc->RequestPoolHead == NULL)
1242                             {
1243
1244                                 pCommSrvc->RequestPoolTail = NULL;
1245                             }
1246                         }
1247                         else
1248                         {
1249
1250                             pPrevEntry->fLink = pEntry->fLink;
1251
1252                             if( pPrevEntry->fLink == NULL)
1253                             {
1254
1255                                 pCommSrvc->RequestPoolTail = pPrevEntry;
1256                             }
1257                         }
1258
1259                         break;
1260                     }
1261
1262                     pPrevEntry = pEntry;
1263
1264                     pEntry = pEntry->fLink;
1265                 }
1266
1267                 if( pCommSrvc->RequestPoolHead == NULL)
1268                 {
1269
1270                     KeClearEvent( &pCommSrvc->IrpPoolHasEntries);
1271                 }
1272
1273                 if( pEntry == NULL)
1274                 {
1275
1276                     KeClearEvent( &pCommSrvc->IrpPoolHasReleaseEntries);
1277                 }
1278
1279                 //
1280                 // And release the request pool lock
1281                 //
1282
1283                 AFSReleaseResource( &pCommSrvc->IrpPoolLock);
1284             }
1285             else
1286             {
1287
1288                 pEntry = pCommSrvc->RequestPoolHead;
1289
1290                 if( pEntry != NULL)
1291                 {
1292
1293                     pCommSrvc->RequestPoolHead = pEntry->fLink;
1294
1295                     pEntry->bLink = NULL;
1296
1297                     if( pCommSrvc->RequestPoolHead == NULL)
1298                     {
1299
1300                         pCommSrvc->RequestPoolTail = NULL;
1301                     }
1302                 }
1303                 else
1304                 {
1305
1306                     KeClearEvent( &pCommSrvc->IrpPoolHasEntries);
1307                 }
1308
1309                 //
1310                 // And release the request pool lock
1311                 //
1312
1313                 AFSReleaseResource( &pCommSrvc->IrpPoolLock);
1314             }
1315
1316             //
1317             // Insert the entry into the result pool, if we have one
1318             //
1319
1320             if( pEntry != NULL)
1321             {
1322
1323                 //
1324                 // Move the request data into the passed in buffer
1325                 //
1326
1327                 ASSERT( sizeof( AFSCommRequest) +
1328                                     pEntry->FileName.Length +
1329                                     pEntry->DataLength <= pIrpSp->Parameters.DeviceIoControl.OutputBufferLength);
1330
1331                 RtlCopyMemory( &pRequest->AuthGroup,
1332                                &pEntry->AuthGroup,
1333                                sizeof( GUID));
1334
1335                 pRequest->FileId = pEntry->FileId;
1336
1337                 pRequest->RequestType = pEntry->RequestType;
1338
1339                 pRequest->RequestIndex = pEntry->RequestIndex;
1340
1341                 pRequest->RequestFlags = pEntry->RequestFlags;
1342
1343                 pRequest->NameLength = pEntry->FileName.Length;
1344
1345                 pRequest->QueueCount = InterlockedDecrement( &pCommSrvc->QueueCount);
1346
1347                 if( pRequest->NameLength > 0)
1348                 {
1349
1350                     RtlCopyMemory( pRequest->Name,
1351                                    pEntry->FileName.Buffer,
1352                                    pRequest->NameLength);
1353                 }
1354
1355                 pRequest->DataOffset = 0;
1356
1357                 pRequest->DataLength = pEntry->DataLength;
1358
1359                 if( pRequest->DataLength > 0)
1360                 {
1361
1362                     pRequest->DataOffset = pEntry->FileName.Length;
1363
1364                     RtlCopyMemory( (void *)((char *)pRequest->Name + pRequest->DataOffset),
1365                                    pEntry->Data,
1366                                    pRequest->DataLength);
1367                 }
1368
1369                 pRequest->ResultBufferLength = 0;
1370
1371                 if( pEntry->ResultBufferLength != NULL)
1372                 {
1373
1374                     pRequest->ResultBufferLength = *(pEntry->ResultBufferLength);
1375                 }
1376
1377                 Irp->IoStatus.Information = sizeof( AFSCommRequest) +
1378                                                         pEntry->FileName.Length +
1379                                                         pEntry->DataLength;
1380
1381                 //
1382                 // If this is a synchronous request then move the request into the
1383                 // result pool
1384                 //
1385
1386                 if( BooleanFlagOn( pEntry->RequestFlags, AFS_REQUEST_FLAG_SYNCHRONOUS))
1387                 {
1388
1389                     pEntry->fLink = NULL;
1390                     pEntry->bLink = NULL;
1391
1392                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1393                                   AFS_TRACE_LEVEL_VERBOSE,
1394                                   "AFSProcessIrpRequest Acquiring ResultPoolLock lock %08lX EXCL %08lX\n",
1395                                   &pCommSrvc->ResultPoolLock,
1396                                   PsGetCurrentThread());
1397
1398                     AFSAcquireExcl( &pCommSrvc->ResultPoolLock,
1399                                     TRUE);
1400
1401                     if( pCommSrvc->ResultPoolHead == NULL)
1402                     {
1403
1404                         pCommSrvc->ResultPoolHead = pEntry;
1405                     }
1406                     else
1407                     {
1408
1409                         pCommSrvc->ResultPoolTail->fLink = pEntry;
1410
1411                         pEntry->bLink = pCommSrvc->ResultPoolTail;
1412                     }
1413
1414                     pCommSrvc->ResultPoolTail = pEntry;
1415
1416                     AFSReleaseResource( &pCommSrvc->ResultPoolLock);
1417                 }
1418                 else
1419                 {
1420
1421                     //
1422                     // Free up the pool entry
1423                     //
1424
1425                     ExFreePool( pEntry);
1426                 }
1427
1428                 break;
1429             }
1430         }
1431
1432 try_exit:
1433
1434         NOTHING;
1435     }
1436
1437     return ntStatus;
1438 }
1439
1440 NTSTATUS
1441 AFSProcessIrpResult( IN PIRP Irp)
1442 {
1443
1444     NTSTATUS            ntStatus = STATUS_SUCCESS;
1445     AFSDeviceExt       *pDevExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
1446     IO_STACK_LOCATION  *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
1447     AFSCommSrvcCB      *pCommSrvc = NULL;
1448     AFSPoolEntry       *pCurrentEntry = NULL;
1449     AFSCommResult      *pResult = NULL;
1450     ULONG               ulCopyLen = 0;
1451
1452     __Enter
1453     {
1454
1455         pCommSrvc = &pDevExt->Specific.Control.CommServiceCB;
1456
1457         //
1458         // Get the request for the incoming result
1459         //
1460
1461         pResult = (AFSCommResult *)Irp->AssociatedIrp.SystemBuffer;
1462
1463         if( pResult == NULL)
1464         {
1465
1466             try_return( ntStatus = STATUS_INVALID_PARAMETER);
1467         }
1468
1469         //
1470         // Go look for our entry
1471         //
1472
1473         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1474                       AFS_TRACE_LEVEL_VERBOSE,
1475                       "AFSProcessIrpResult Acquiring ResultPoolLock lock %08lX EXCL %08lX\n",
1476                       &pCommSrvc->ResultPoolLock,
1477                       PsGetCurrentThread());
1478
1479         AFSAcquireExcl( &pCommSrvc->ResultPoolLock,
1480                           TRUE);
1481
1482         pCurrentEntry = pCommSrvc->ResultPoolHead;
1483
1484         while( pCurrentEntry != NULL)
1485         {
1486
1487             if( pCurrentEntry->RequestIndex == pResult->RequestIndex)
1488             {
1489
1490                 //
1491                 // Found the entry so remove it from the queue
1492                 //
1493
1494                 if( pCurrentEntry->bLink == NULL)
1495                 {
1496
1497                     //
1498                     // At the head of the list
1499                     //
1500
1501                     pCommSrvc->ResultPoolHead = pCurrentEntry->fLink;
1502
1503                     if( pCommSrvc->ResultPoolHead != NULL)
1504                     {
1505
1506                         pCommSrvc->ResultPoolHead->bLink = NULL;
1507                     }
1508                 }
1509                 else
1510                 {
1511
1512                     pCurrentEntry->bLink->fLink = pCurrentEntry->fLink;
1513                 }
1514
1515                 if( pCurrentEntry->fLink == NULL)
1516                 {
1517
1518                     pCommSrvc->ResultPoolTail = pCurrentEntry->bLink;
1519
1520                     if( pCommSrvc->ResultPoolTail != NULL)
1521                     {
1522
1523                         pCommSrvc->ResultPoolTail->fLink = NULL;
1524                     }
1525                 }
1526                 else
1527                 {
1528
1529                     pCurrentEntry->fLink->bLink = pCurrentEntry->bLink;
1530                 }
1531
1532                 break;
1533             }
1534
1535             pCurrentEntry = pCurrentEntry->fLink;
1536         }
1537
1538         AFSReleaseResource( &pCommSrvc->ResultPoolLock);
1539
1540         if( pCurrentEntry == NULL)
1541         {
1542
1543             try_return( ntStatus = STATUS_INVALID_PARAMETER);
1544         }
1545
1546         //
1547         // OK, move in the result information
1548         //
1549
1550         pCurrentEntry->ResultStatus = pResult->ResultStatus;
1551
1552         if( ( pCurrentEntry->ResultStatus == STATUS_SUCCESS ||
1553               pCurrentEntry->ResultStatus == STATUS_BUFFER_OVERFLOW) &&
1554             pCurrentEntry->ResultBufferLength != NULL &&
1555             pCurrentEntry->ResultBuffer != NULL)
1556         {
1557
1558             ASSERT( pResult->ResultBufferLength <= *(pCurrentEntry->ResultBufferLength));
1559
1560             ulCopyLen = pResult->ResultBufferLength;
1561
1562             if( ulCopyLen > *(pCurrentEntry->ResultBufferLength))
1563             {
1564                 ulCopyLen = *(pCurrentEntry->ResultBufferLength);
1565             }
1566
1567             *(pCurrentEntry->ResultBufferLength) = ulCopyLen;
1568
1569             if( pResult->ResultBufferLength > 0)
1570             {
1571
1572                 RtlCopyMemory( pCurrentEntry->ResultBuffer,
1573                                pResult->ResultData,
1574                                ulCopyLen);
1575             }
1576         }
1577
1578         KeSetEvent( &pCurrentEntry->Event,
1579                     0,
1580                     FALSE);
1581
1582 try_exit:
1583
1584         if( !NT_SUCCESS( ntStatus))
1585         {
1586
1587         }
1588     }
1589
1590     return ntStatus;
1591 }