tests: Use real tempfile in pt_util-t
[openafs.git] / src / WINNT / afsrdr / user / RDRInit.cpp
1 /*
2  * Copyright (c) 2008 Secure Endpoints, Inc.
3  * Copyright (c) 2009-2013 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 are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Secure Endpoints Inc. nor the names of its contributors
15  *   may be used to endorse or promote products derived from this software without
16  *   specific prior written permission from Secure Endpoints, Inc. and
17  *   Your File System, Inc.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 extern "C" {
33 #include <afsconfig.h>
34 #include <afs/param.h>
35 #include <afs/stds.h>
36 }
37
38 #ifndef _WIN32_WINNT
39 #define _WIN32_WINNT 0x0500
40 #endif
41 #define _CRT_SECURE_NO_DEPRECATE
42 #define _CRT_NON_CONFORMING_SWPRINTFS
43 #define UNICODE 1
44 #define STRSAFE_NO_DEPRECATE
45
46 #include <ntstatus.h>
47 #define WIN32_NO_STATUS
48 #include <windows.h>
49 typedef LONG NTSTATUS, *PNTSTATUS;      // not declared in ntstatus.h
50
51 #include <roken.h>
52
53 #include <devioctl.h>
54
55 #include <tchar.h>
56 #include <winbase.h>
57 #include <winreg.h>
58 #include <strsafe.h>
59
60 #include "..\\Common\\AFSUserDefines.h"
61 #include "..\\Common\\AFSUserIoctl.h"
62 #include "..\\Common\\AFSUserStructs.h"
63
64 extern "C" {
65 #include <osilog.h>
66 extern osi_log_t *afsd_logp;
67 extern int cm_directIO;
68
69 #include <WINNT/afsreg.h>
70 #include <afs/cm_config.h>
71 #include <afs/cm_error.h>
72 #include <afs/cm_nls.h>
73 #include <afs/cm_user.h>
74 }
75 #include <RDRPrototypes.h>
76
77 static DWORD
78 RDR_SetFileStatus2( AFSFileID * pFileId,
79                     GUID *pAuthGroup,
80                     DWORD dwStatus);
81
82 #ifndef FlagOn
83 #define FlagOn(_F,_SF)        ((_F) & (_SF))
84 #endif
85
86 #ifndef BooleanFlagOn
87 #define BooleanFlagOn(F,SF)   ((BOOLEAN)(((F) & (SF)) != 0))
88 #endif
89
90 #ifndef SetFlag
91 #define SetFlag(_F,_SF)       ((_F) |= (_SF))
92 #endif
93
94 #ifndef ClearFlag
95 #define ClearFlag(_F,_SF)     ((_F) &= ~(_SF))
96 #endif
97
98 #define QuadAlign(Ptr) (                \
99     ((((ULONG)(Ptr)) + 7) & 0xfffffff8) \
100     )
101
102 #define MIN_WORKER_THREADS 5
103 #define MAX_WORKER_THREADS 512
104
105 typedef struct _worker_thread_info {
106
107     HANDLE hThread;
108
109     ULONG  Flags;
110
111     HANDLE hEvent;
112
113 } WorkerThreadInfo;
114
115 WorkerThreadInfo glWorkerThreadInfo[ MAX_WORKER_THREADS];
116
117 UINT   glThreadHandleIndex = 0;
118
119 HANDLE glDevHandle = INVALID_HANDLE_VALUE;
120
121 static DWORD Exit = false;
122
123 static DWORD ExitPending = false;
124
125 DWORD  dwOvEvIdx = 0;
126
127 extern "C" wchar_t RDR_UNCName[64]=L"AFS";
128
129 HANDLE RDR_SuspendEvent = INVALID_HANDLE_VALUE;
130
131 /* returns 0 on success */
132 extern "C" DWORD
133 RDR_Initialize(void)
134 {
135
136     DWORD dwRet = 0;
137     HKEY parmKey;
138     DWORD dummyLen;
139     DWORD numSvThreads = CM_CONFIGDEFAULT_SVTHREADS;
140
141     // Initialize the Suspend Event
142     RDR_SuspendEvent = CreateEvent( NULL,
143                                     TRUE, // manual reset event
144                                     TRUE, // signaled
145                                     NULL);
146
147     dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
148                          0, KEY_QUERY_VALUE, &parmKey);
149     if (dwRet == ERROR_SUCCESS) {
150         dummyLen = sizeof(numSvThreads);
151         dwRet = RegQueryValueEx(parmKey, TEXT("ServerThreads"), NULL, NULL,
152                                 (BYTE *) &numSvThreads, &dummyLen);
153
154         dummyLen = sizeof(RDR_UNCName);
155         dwRet = RegQueryValueExW(parmKey, L"NetbiosName", NULL, NULL,
156                                  (BYTE *) RDR_UNCName, &dummyLen);
157
158         RegCloseKey (parmKey);
159     }
160
161     // Initialize the Thread local storage index for the overlapped i/o
162     // Event Handle
163     dwOvEvIdx = TlsAlloc();
164
165     Exit = false;
166
167     //
168     // Launch our workers down to the
169     // filters control device for processing requests
170     //
171
172     dwRet = RDR_ProcessWorkerThreads(numSvThreads);
173
174     if (dwRet == ERROR_SUCCESS) {
175
176         RDR_InitIoctl();
177         RDR_InitPipe();
178     }
179
180     return dwRet;
181 }
182
183 BOOL RDR_DeviceIoControl( HANDLE hDevice,
184                           DWORD dwIoControlCode,
185                           LPVOID lpInBuffer,
186                           DWORD nInBufferSize,
187                           LPVOID lpOutBuffer,
188                           DWORD nOutBufferSize,
189                           LPDWORD lpBytesReturned )
190 {
191     OVERLAPPED ov;
192     HANDLE hEvent;
193     BOOL rc = FALSE;
194     DWORD gle;
195
196     ZeroMemory(&ov, sizeof(OVERLAPPED));
197
198     hEvent = (HANDLE)TlsGetValue(dwOvEvIdx);
199     if (hEvent == NULL) {
200         hEvent = CreateEvent( NULL, TRUE, TRUE, NULL );
201         if (hEvent == INVALID_HANDLE_VALUE || hEvent == NULL)
202             return FALSE;
203         TlsSetValue( dwOvEvIdx, (LPVOID) hEvent );
204     }
205
206     ResetEvent( hEvent);
207     ov.hEvent = hEvent;
208     *lpBytesReturned = 0;
209
210     rc = DeviceIoControl( hDevice,
211                           dwIoControlCode,
212                           lpInBuffer,
213                           nInBufferSize,
214                           lpOutBuffer,
215                           nOutBufferSize,
216                           lpBytesReturned,
217                           &ov );
218     if ( !rc ) {
219         gle = GetLastError();
220
221         if ( gle == ERROR_IO_PENDING )
222             rc = GetOverlappedResult( hDevice, &ov, lpBytesReturned, TRUE );
223     }
224
225     return rc;
226 }
227
228 extern "C" DWORD
229 RDR_ShutdownFinal(void)
230 {
231
232     DWORD dwIndex = 0;
233
234     Exit = true;
235
236     //
237     // Close all the worker thread handles
238     //
239
240     while( dwIndex < glThreadHandleIndex)
241     {
242
243         CloseHandle( glWorkerThreadInfo[ dwIndex].hThread);
244
245         dwIndex++;
246     }
247
248     if( glDevHandle != INVALID_HANDLE_VALUE)
249     {
250
251         CloseHandle( glDevHandle);
252     }
253
254     return 0;
255 }
256
257 extern "C" DWORD
258 RDR_ShutdownNotify(void)
259 {
260
261     HANDLE hDevHandle = NULL;
262     DWORD bytesReturned;
263
264     //
265     // We use the global handle to the control device instance
266     //
267
268     hDevHandle = glDevHandle;
269
270
271     //
272     // First, notify the file system driver that
273     // we are shutting down.
274     //
275
276     ExitPending = true;
277
278     if( !RDR_DeviceIoControl( hDevHandle,
279                               IOCTL_AFS_SHUTDOWN,
280                               NULL,
281                               0,
282                               NULL,
283                               0,
284                               &bytesReturned ))
285     {
286         // log the error, nothing to do
287     }
288
289
290     RDR_ShutdownIoctl();
291     RDR_ShutdownPipe();
292
293     return 0;
294 }
295
296 //
297 // Here we launch the worker threads for the given volume
298 //
299
300 DWORD
301 RDR_ProcessWorkerThreads(DWORD numThreads)
302 {
303     DWORD WorkerID;
304     HANDLE hEvent;
305     DWORD index = 0;
306     DWORD bytesReturned = 0;
307     DWORD dwRedirInitInfo;
308     AFSRedirectorInitInfo * redirInitInfo = NULL;
309     DWORD dwErr;
310
311     if (dwErr = RDR_SetInitParams(&redirInitInfo, &dwRedirInitInfo))
312         return dwErr;
313
314     glDevHandle = CreateFile( AFS_SYMLINK_W,
315                               GENERIC_READ | GENERIC_WRITE,
316                               FILE_SHARE_READ | FILE_SHARE_WRITE,
317                               NULL,
318                               OPEN_EXISTING,
319                               FILE_FLAG_OVERLAPPED,
320                               NULL);
321
322     if( glDevHandle == INVALID_HANDLE_VALUE)
323     {
324         dwErr = GetLastError();
325
326         free(redirInitInfo);
327
328         return dwErr;
329     }
330
331     //
332     // Now call down to initialize the pool.
333     //
334
335     if( !RDR_DeviceIoControl( glDevHandle,
336                               IOCTL_AFS_INITIALIZE_CONTROL_DEVICE,
337                               NULL,
338                               0,
339                               NULL,
340                               0,
341                               &bytesReturned ))
342     {
343
344         dwErr = GetLastError();
345
346         CloseHandle( glDevHandle);
347
348         glDevHandle = NULL;
349
350         free(redirInitInfo);
351
352         return dwErr;
353     }
354
355     //
356     // OK, now launch the workers
357     //
358
359     hEvent = CreateEvent( NULL,
360                           TRUE,
361                           FALSE,
362                           NULL);
363
364     //
365     // Here we create a pool of worker threads but you can create the pool with as many requests
366     // as you want
367     //
368
369     if (numThreads < MIN_WORKER_THREADS)
370         numThreads = MIN_WORKER_THREADS;
371     else if (numThreads > MAX_WORKER_THREADS)
372         numThreads = MAX_WORKER_THREADS;
373
374     for (index = 0; index < numThreads; index++)
375     {
376         if ( !cm_directIO)
377         {
378             //
379             // 20% of worker threads should be reserved for release extent
380             // event processing
381             //
382             glWorkerThreadInfo[ glThreadHandleIndex].Flags =
383                 (glThreadHandleIndex % 5) ? 0 : AFS_REQUEST_RELEASE_THREAD;
384         }
385         else
386         {
387             glWorkerThreadInfo[ glThreadHandleIndex].Flags = 0;
388         }
389         glWorkerThreadInfo[ glThreadHandleIndex].hEvent = hEvent;
390         glWorkerThreadInfo[ glThreadHandleIndex].hThread =
391             CreateThread( NULL,
392                           0,
393                           RDR_RequestWorkerThread,
394                           (void *)&glWorkerThreadInfo[ glThreadHandleIndex],
395                           0,
396                           &WorkerID);
397
398         if( glWorkerThreadInfo[ glThreadHandleIndex].hThread != NULL)
399         {
400
401             //
402             // Wait for the thread to signal it is ready for processing
403             //
404
405             WaitForSingleObject( hEvent,
406                                  INFINITE);
407
408             glThreadHandleIndex++;
409
410             ResetEvent( hEvent);
411         }
412         else
413         {
414
415             //
416             // Perform cleanup specific to your application
417             //
418
419         }
420     }
421
422     if( !RDR_DeviceIoControl( glDevHandle,
423                               IOCTL_AFS_INITIALIZE_REDIRECTOR_DEVICE,
424                               redirInitInfo,
425                               dwRedirInitInfo,
426                               NULL,
427                               0,
428                               &bytesReturned ))
429     {
430
431         //
432         // Must kill off the worker threads we spawned.
433         //
434
435         RDR_ShutdownFinal();
436
437         dwErr = GetLastError();
438
439         CloseHandle( glDevHandle);
440
441         glDevHandle = NULL;
442
443         free(redirInitInfo);
444
445         return dwErr;
446     }
447
448     free(redirInitInfo);
449
450     return 0;
451 }
452
453 //
454 // Entry point for the worker thread
455 //
456
457 DWORD
458 WINAPI
459 RDR_RequestWorkerThread( LPVOID lpParameter)
460 {
461
462     HANDLE hDevHandle = NULL;
463     DWORD bytesReturned;
464     AFSCommRequest *requestBuffer;
465     bool bError = false;
466     WorkerThreadInfo * pInfo = (WorkerThreadInfo *)lpParameter;
467
468     //
469     // We use the global handle to the control device instance
470     //
471
472     hDevHandle = glDevHandle;
473
474     //
475     // Allocate a request buffer.
476     //
477
478     requestBuffer = (AFSCommRequest *)malloc( sizeof( AFSCommRequest) + AFS_PAYLOAD_BUFFER_SIZE);
479
480     if( requestBuffer)
481     {
482
483         //
484         // Here we simply signal back to the main thread that we ahve started
485         //
486
487         SetEvent( pInfo->hEvent);
488
489         //
490         // Process requests until we are told to stop
491         //
492
493         while( !Exit)
494         {
495
496             memset( requestBuffer, '\0', sizeof( AFSCommRequest) + AFS_PAYLOAD_BUFFER_SIZE);
497
498             requestBuffer->RequestFlags = pInfo->Flags;
499
500             if( RDR_DeviceIoControl( hDevHandle,
501                                       IOCTL_AFS_PROCESS_IRP_REQUEST,
502                                       (void *)requestBuffer,
503                                       sizeof( AFSCommRequest),
504                                       (void *)requestBuffer,
505                                       sizeof( AFSCommRequest) + AFS_PAYLOAD_BUFFER_SIZE,
506                                       &bytesReturned ))
507             {
508
509                 WaitForSingleObject( RDR_SuspendEvent, INFINITE);
510
511                 //
512                 // Go process the request
513                 //
514
515                 if (!Exit)
516                     RDR_ProcessRequest( requestBuffer);
517             }
518             else
519             {
520
521                 if (afsd_logp->enabled) {
522                     WCHAR wchBuffer[256];
523                     DWORD gle = GetLastError();
524
525                     swprintf( wchBuffer,
526                               L"Failed to post IOCTL_AFS_IRP_REQUEST gle 0x%x", gle);
527                     osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
528                 }
529             }
530         }
531
532         free( requestBuffer);
533     }
534
535     ExitThread( 0);
536
537     return 0;
538 }
539
540 //
541 // This is the entry point for the worker threads to process the request from the TC Filter driver
542 //
543
544 void
545 RDR_ProcessRequest( AFSCommRequest *RequestBuffer)
546 {
547
548     DWORD               bytesReturned;
549     DWORD               result = 0;
550     ULONG               ulIndex = 0;
551     ULONG               ulCreateFlags = 0;
552     AFSCommResult *     pResultCB = NULL;
553     AFSCommResult       stResultCB;
554     DWORD               dwResultBufferLength = 0;
555     AFSSetFileExtentsCB * SetFileExtentsResultCB = NULL;
556     AFSSetByteRangeLockResultCB *SetByteRangeLockResultCB = NULL;
557     WCHAR               wchBuffer[1024];
558     char *pBuffer = (char *)wchBuffer;
559     DWORD gle;
560     cm_user_t *         userp = NULL;
561     BOOL                bWow64 = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_WOW64) ? TRUE : FALSE;
562     BOOL                bFast  = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_FAST_REQUEST) ? TRUE : FALSE;
563     BOOL                bHoldFid = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_HOLD_FID) ? TRUE : FALSE;
564     BOOL                bFlushFile = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_FLUSH_FILE) ? TRUE : FALSE;
565     BOOL                bDeleteFile = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_FILE_DELETED) ? TRUE : FALSE;
566     BOOL                bUnlockFile = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_BYTE_RANGE_UNLOCK_ALL) ? TRUE : FALSE;
567     BOOL                bCheckOnly = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_CHECK_ONLY) ? TRUE : FALSE;
568     BOOL                bCacheBypass = (RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_CACHE_BYPASS) ? TRUE : FALSE;
569     BOOL                bRetry = FALSE;
570     BOOL                bUnsupported = FALSE;
571     BOOL                bIsLocalSystem = (RequestBuffer->RequestFlags & AFS_REQUEST_LOCAL_SYSTEM_PAG) ? TRUE : FALSE;
572
573     userp = RDR_UserFromCommRequest(RequestBuffer);
574
575   retry:
576     //
577     // Build up the string to display based on the request type.
578     //
579
580     switch( RequestBuffer->RequestType)
581     {
582
583         case AFS_REQUEST_TYPE_DIR_ENUM:
584         {
585
586             AFSDirQueryCB *pQueryCB = (AFSDirQueryCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
587
588             if (afsd_logp->enabled) {
589                 swprintf( wchBuffer,
590                           L"ProcessRequest Processing AFS_REQUEST_TYPE_DIR_ENUM Index %08lX",
591                           RequestBuffer->RequestIndex);
592
593                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
594             }
595
596             //
597             // Here is where the content of the specific directory is enumerated.
598             //
599
600             RDR_EnumerateDirectory( userp, RequestBuffer->FileId,
601                                     pQueryCB, bWow64, bFast,
602                                     RequestBuffer->ResultBufferLength,
603                                     &pResultCB);
604             break;
605         }
606
607         case AFS_REQUEST_TYPE_EVAL_TARGET_BY_ID:
608         {
609             AFSEvalTargetCB *pEvalTargetCB = (AFSEvalTargetCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
610
611             if (afsd_logp->enabled) {
612                 swprintf( wchBuffer,
613                           L"ProcessRequest Processing AFS_REQUEST_TYPE_EVAL_TARGET_BY_ID Index %08lX",
614                           RequestBuffer->RequestIndex);
615
616                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
617             }
618             //
619             // Here is where the specified node is evaluated.
620             //
621
622             RDR_EvaluateNodeByID( userp, pEvalTargetCB->ParentId,
623                                   RequestBuffer->FileId,
624                                   bWow64, bFast, bHoldFid,
625                                   RequestBuffer->ResultBufferLength,
626                                   &pResultCB);
627             break;
628         }
629
630         case AFS_REQUEST_TYPE_EVAL_TARGET_BY_NAME:
631         {
632             AFSEvalTargetCB *pEvalTargetCB = (AFSEvalTargetCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
633
634             if (afsd_logp->enabled) {
635                 swprintf( wchBuffer,
636                           L"ProcessRequest Processing AFS_REQUEST_TYPE_EVAL_TARGET_BY_NAME Index %08lX",
637                           RequestBuffer->RequestIndex);
638
639                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
640             }
641             //
642             // Here is where the specified node is evaluated.
643             //
644
645             RDR_EvaluateNodeByName( userp, pEvalTargetCB->ParentId,
646                                     RequestBuffer->Name,
647                                     RequestBuffer->NameLength,
648                                     RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_CASE_SENSITIVE ? TRUE : FALSE,
649                                     RequestBuffer->RequestFlags & AFS_REQUEST_FLAG_LAST_COMPONENT ? TRUE : FALSE,
650                                     bWow64, bFast, bHoldFid,
651                                     RequestBuffer->ResultBufferLength,
652                                     &pResultCB);
653             break;
654         }
655
656         case AFS_REQUEST_TYPE_CREATE_FILE:
657         {
658
659             AFSFileCreateCB *pCreateCB = (AFSFileCreateCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
660
661             WCHAR wchFileName[ 256];
662
663             if (afsd_logp->enabled) {
664                 memset( wchFileName, '\0', 256 * sizeof( WCHAR));
665
666                 memcpy( wchFileName,
667                         RequestBuffer->Name,
668                         RequestBuffer->NameLength);
669
670                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_CREATE_FILE Index %08lX File %S",
671                           RequestBuffer->RequestIndex, wchFileName);
672
673                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
674             }
675
676             RDR_CreateFileEntry( userp,
677                                  RequestBuffer->Name,
678                                  RequestBuffer->NameLength,
679                                  pCreateCB,
680                                  bWow64,
681                                  bHoldFid,
682                                  RequestBuffer->ResultBufferLength,
683                                  &pResultCB);
684
685             break;
686         }
687
688         case AFS_REQUEST_TYPE_UPDATE_FILE:
689         {
690
691             AFSFileUpdateCB *pUpdateCB = (AFSFileUpdateCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
692
693             if (afsd_logp->enabled) {
694                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_UPDATE_FILE Index %08lX File %08lX.%08lX.%08lX.%08lX",
695                           RequestBuffer->RequestIndex,
696                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
697                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
698
699                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
700             }
701
702             RDR_UpdateFileEntry( userp, RequestBuffer->FileId,
703                                  pUpdateCB,
704                                  bWow64,
705                                  RequestBuffer->ResultBufferLength,
706                                  &pResultCB);
707
708             break;
709         }
710
711         case AFS_REQUEST_TYPE_DELETE_FILE:
712         {
713
714             AFSFileDeleteCB *pDeleteCB = (AFSFileDeleteCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
715
716             if (afsd_logp->enabled) {
717                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_DELETE_FILE Index %08lX %08lX.%08lX.%08lX.%08lX CheckOnly %X",
718                           RequestBuffer->RequestIndex,
719                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
720                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique,
721                           bCheckOnly);
722
723                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
724             }
725
726             RDR_DeleteFileEntry( userp,
727                                  pDeleteCB->ParentId,
728                                  pDeleteCB->ProcessId,
729                                  RequestBuffer->Name,
730                                  RequestBuffer->NameLength,
731                                  bWow64,
732                                  bCheckOnly,
733                                  RequestBuffer->ResultBufferLength,
734                                  &pResultCB);
735
736             break;
737         }
738
739         case AFS_REQUEST_TYPE_RENAME_FILE:
740         {
741
742             AFSFileRenameCB *pFileRenameCB = (AFSFileRenameCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
743
744             if (afsd_logp->enabled) {
745                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_RENAME_FILE Index %08lX File %08lX.%08lX.%08lX.%08lX NameLength %08lX Name %*S TargetLength %08lX Target %*S",
746                           RequestBuffer->RequestIndex,
747                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
748                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique,
749                           RequestBuffer->NameLength, (int)RequestBuffer->NameLength, RequestBuffer->Name,
750                           pFileRenameCB->TargetNameLength, (int)pFileRenameCB->TargetNameLength, pFileRenameCB->TargetName);
751
752                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
753             }
754
755             RDR_RenameFileEntry( userp,
756                                  RequestBuffer->Name,
757                                  RequestBuffer->NameLength,
758                                  RequestBuffer->FileId,
759                                  pFileRenameCB,
760                                  bWow64,
761                                  RequestBuffer->ResultBufferLength,
762                                  &pResultCB);
763
764             break;
765         }
766
767         case AFS_REQUEST_TYPE_HARDLINK_FILE:
768         {
769
770             AFSFileHardLinkCB *pFileHardLinkCB = (AFSFileHardLinkCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
771
772             if (afsd_logp->enabled) {
773                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_HARDLINK_FILE Index %08lX File %08lX.%08lX.%08lX.%08lX NameLength %08lX Name %*S TargetLength %08lX Target %*S",
774                           RequestBuffer->RequestIndex,
775                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
776                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique,
777                           RequestBuffer->NameLength, (int)RequestBuffer->NameLength, RequestBuffer->Name,
778                           pFileHardLinkCB->TargetNameLength, (int)pFileHardLinkCB->TargetNameLength, pFileHardLinkCB->TargetName);
779
780                     osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
781                 }
782
783                 RDR_HardLinkFileEntry( userp,
784                                        RequestBuffer->Name,
785                                        RequestBuffer->NameLength,
786                                        RequestBuffer->FileId,
787                                        pFileHardLinkCB,
788                                        bWow64,
789                                        RequestBuffer->ResultBufferLength,
790                                        &pResultCB);
791
792             break;
793         }
794
795         case AFS_REQUEST_TYPE_CREATE_SYMLINK:
796         {
797
798             AFSCreateSymlinkCB *pCreateSymlinkCB = (AFSCreateSymlinkCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
799
800             WCHAR wchFileName[ 256];
801
802             if (afsd_logp->enabled) {
803                 memset( wchFileName, '\0', 256 * sizeof( WCHAR));
804
805                 memcpy( wchFileName,
806                         RequestBuffer->Name,
807                         RequestBuffer->NameLength);
808
809                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_CREATE_SYMLINK Index %08lX File %S",
810                           RequestBuffer->RequestIndex, wchFileName);
811
812                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
813             }
814
815             RDR_CreateSymlinkEntry( userp,
816                                     RequestBuffer->FileId,
817                                     RequestBuffer->Name,
818                                     RequestBuffer->NameLength,
819                                     pCreateSymlinkCB,
820                                     bWow64,
821                                     RequestBuffer->ResultBufferLength,
822                                     &pResultCB);
823
824             break;
825         }
826
827         case AFS_REQUEST_TYPE_REQUEST_FILE_EXTENTS:
828         {
829
830             AFSRequestExtentsCB *pFileRequestExtentsCB = (AFSRequestExtentsCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
831
832             if (afsd_logp->enabled) {
833                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_REQUEST_FILE_EXTENTS Index %08lX File %08lX.%08lX.%08lX.%08lX %S",
834                           RequestBuffer->RequestIndex,
835                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
836                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique,
837                           BooleanFlagOn( RequestBuffer->RequestFlags, AFS_REQUEST_FLAG_SYNCHRONOUS) ? L"Sync" : L"Async");
838
839                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
840             }
841
842             if (BooleanFlagOn( RequestBuffer->RequestFlags, AFS_REQUEST_FLAG_SYNCHRONOUS))
843                 osi_panic("SYNCHRONOUS AFS_REQUEST_TYPE_REQUEST_FILE_EXTENTS not supported",
844                           __FILE__, __LINE__);
845             else
846                 bRetry = RDR_RequestFileExtentsAsync( userp, RequestBuffer->FileId,
847                                                       pFileRequestExtentsCB,
848                                                       bWow64,
849                                                       &dwResultBufferLength,
850                                                       &SetFileExtentsResultCB );
851             break;
852         }
853
854         case AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS:
855         {
856
857             AFSReleaseExtentsCB *pFileReleaseExtentsCB = (AFSReleaseExtentsCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
858
859             if (afsd_logp->enabled) {
860                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS Index %08lX File %08lX.%08lX.%08lX.%08lX",
861                           RequestBuffer->RequestIndex,
862                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
863                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
864
865                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
866             }
867
868             RDR_ReleaseFileExtents( userp, RequestBuffer->FileId,
869                                     pFileReleaseExtentsCB,
870                                     bWow64,
871                                     RequestBuffer->ResultBufferLength,
872                                     &pResultCB);
873
874             break;
875         }
876
877         case AFS_REQUEST_TYPE_FLUSH_FILE:
878         {
879             if (afsd_logp->enabled) {
880                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_FLUSH_FILE Index %08lX File %08lX.%08lX.%08lX.%08lX",
881                           RequestBuffer->RequestIndex,
882                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
883                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
884
885                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
886             }
887
888             RDR_FlushFileEntry( userp, RequestBuffer->FileId,
889                                 bWow64,
890                                 RequestBuffer->ResultBufferLength,
891                                 &pResultCB);
892             break;
893         }
894
895         case AFS_REQUEST_TYPE_OPEN_FILE:
896         {
897             AFSFileOpenCB *pFileOpenCB = (AFSFileOpenCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
898
899             if (afsd_logp->enabled) {
900                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_OPEN_FILE Index %08lX File %08lX.%08lX.%08lX.%08lX",
901                           RequestBuffer->RequestIndex,
902                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
903                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
904
905                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
906             }
907
908             RDR_OpenFileEntry( userp, RequestBuffer->FileId,
909                                pFileOpenCB,
910                                bWow64,
911                                bHoldFid,
912                                RequestBuffer->ResultBufferLength,
913                                &pResultCB);
914
915             break;
916         }
917
918         case AFS_REQUEST_TYPE_RELEASE_FILE_ACCESS:
919         {
920             AFSFileAccessReleaseCB *pFileAccessReleaseCB = (AFSFileAccessReleaseCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
921
922             if (afsd_logp->enabled) {
923                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_RELEASE_FILE_ACCESS Index %08lX File %08lX.%08lX.%08lX.%08lX",
924                           RequestBuffer->RequestIndex,
925                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
926                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
927
928                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
929             }
930
931             RDR_ReleaseFileAccess( userp,
932                                    RequestBuffer->FileId,
933                                    pFileAccessReleaseCB,
934                                    bWow64,
935                                    RequestBuffer->ResultBufferLength,
936                                    &pResultCB);
937
938             break;
939         }
940
941         case AFS_REQUEST_TYPE_PIOCTL_OPEN:
942         {
943             AFSPIOCtlOpenCloseRequestCB *pPioctlCB = (AFSPIOCtlOpenCloseRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
944
945             if (afsd_logp->enabled) {
946                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIOCTL_OPEN Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
947                           RequestBuffer->RequestIndex,
948                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
949                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
950
951                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
952             }
953
954             RDR_PioctlOpen( userp,
955                             RequestBuffer->FileId,
956                             pPioctlCB,
957                             bWow64,
958                             RequestBuffer->ResultBufferLength,
959                             &pResultCB);
960             break;
961         }
962
963         case AFS_REQUEST_TYPE_PIOCTL_WRITE:
964         {
965             AFSPIOCtlIORequestCB *pPioctlCB = (AFSPIOCtlIORequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
966
967             if (afsd_logp->enabled) {
968                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIOCTL_WRITE Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
969                           RequestBuffer->RequestIndex,
970                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
971                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
972
973                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
974             }
975
976             RDR_PioctlWrite( userp,
977                              RequestBuffer->FileId,
978                              pPioctlCB,
979                              bWow64,
980                              RequestBuffer->ResultBufferLength,
981                              &pResultCB);
982             break;
983         }
984
985         case AFS_REQUEST_TYPE_PIOCTL_READ:
986         {
987             AFSPIOCtlIORequestCB *pPioctlCB = (AFSPIOCtlIORequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
988
989             if (afsd_logp->enabled) {
990                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIOCTL_READ Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
991                           RequestBuffer->RequestIndex,
992                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
993                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
994
995                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
996             }
997
998             RDR_PioctlRead( userp,
999                             RequestBuffer->FileId,
1000                             pPioctlCB,
1001                             bWow64,
1002                             bIsLocalSystem,
1003                             RequestBuffer->ResultBufferLength,
1004                             &pResultCB);
1005             break;
1006         }
1007
1008         case AFS_REQUEST_TYPE_PIOCTL_CLOSE:
1009         {
1010             AFSPIOCtlOpenCloseRequestCB *pPioctlCB = (AFSPIOCtlOpenCloseRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1011
1012             if (afsd_logp->enabled) {
1013                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIOCTL_CLOSE Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
1014                           RequestBuffer->RequestIndex,
1015                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1016                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1017
1018                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1019             }
1020
1021             RDR_PioctlClose( userp,
1022                              RequestBuffer->FileId,
1023                              pPioctlCB,
1024                              bWow64,
1025                              RequestBuffer->ResultBufferLength,
1026                              &pResultCB);
1027             break;
1028         }
1029
1030
1031         case AFS_REQUEST_TYPE_BYTE_RANGE_LOCK:
1032         {
1033             if (afsd_logp->enabled) {
1034                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_BYTE_RANGE_LOCK Index %08lX File %08lX.%08lX.%08lX.%08lX %S",
1035                           RequestBuffer->RequestIndex,
1036                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1037                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique,
1038                           BooleanFlagOn( RequestBuffer->RequestFlags, AFS_REQUEST_FLAG_SYNCHRONOUS) ? L"Sync" : L"Async");
1039
1040                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1041             }
1042
1043             AFSByteRangeLockRequestCB *pBRLRequestCB = (AFSByteRangeLockRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1044
1045             RDR_ByteRangeLockSync( userp,
1046                                    RequestBuffer->FileId,
1047                                    pBRLRequestCB,
1048                                    bWow64,
1049                                    RequestBuffer->ResultBufferLength,
1050                                    &pResultCB);
1051
1052             break;
1053         }
1054
1055         case AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK:
1056         {
1057             AFSByteRangeUnlockRequestCB *pBRURequestCB = (AFSByteRangeUnlockRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1058
1059             if (afsd_logp->enabled) {
1060                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK Index %08lX File %08lX.%08lX.%08lX.%08lX",
1061                           RequestBuffer->RequestIndex,
1062                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1063                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1064
1065                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1066             }
1067
1068             RDR_ByteRangeUnlock( userp,
1069                                  RequestBuffer->FileId,
1070                                  pBRURequestCB,
1071                                  bWow64,
1072                                  RequestBuffer->ResultBufferLength,
1073                                  &pResultCB);
1074             break;
1075         }
1076
1077         case AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK_ALL:
1078         {
1079             AFSByteRangeUnlockRequestCB *pBRURequestCB = (AFSByteRangeUnlockRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1080
1081             if (afsd_logp->enabled) {
1082                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_BYTE_RANGE_UNLOCK_ALL Index %08lX File %08lX.%08lX.%08lX.%08lX",
1083                           RequestBuffer->RequestIndex,
1084                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1085                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1086
1087                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1088             }
1089
1090             RDR_ByteRangeUnlockAll( userp,
1091                                     RequestBuffer->FileId,
1092                                     pBRURequestCB,
1093                                     bWow64,
1094                                     RequestBuffer->ResultBufferLength,
1095                                     &pResultCB);
1096             break;
1097         }
1098
1099         case AFS_REQUEST_TYPE_GET_VOLUME_INFO:
1100         {
1101             if (afsd_logp->enabled) {
1102                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_GET_VOLUME_INFO Index %08lX File %08lX.%08lX.%08lX.%08lX",
1103                           RequestBuffer->RequestIndex,
1104                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1105                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1106
1107                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1108             }
1109
1110             RDR_GetVolumeInfo( userp,
1111                                RequestBuffer->FileId,
1112                                bWow64,
1113                                RequestBuffer->ResultBufferLength,
1114                                &pResultCB);
1115             break;
1116         }
1117
1118         case AFS_REQUEST_TYPE_GET_VOLUME_SIZE_INFO:
1119         {
1120             if (afsd_logp->enabled) {
1121                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_GET_VOLUME_SIZE_INFO Index %08lX File %08lX.%08lX.%08lX.%08lX",
1122                           RequestBuffer->RequestIndex,
1123                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1124                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1125
1126                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1127             }
1128
1129             RDR_GetVolumeSizeInfo( userp,
1130                                    RequestBuffer->FileId,
1131                                    bWow64,
1132                                    RequestBuffer->ResultBufferLength,
1133                                    &pResultCB);
1134             break;
1135         }
1136
1137         case AFS_REQUEST_TYPE_HOLD_FID:
1138         {
1139
1140             AFSHoldFidRequestCB *pHoldFidCB = (AFSHoldFidRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1141
1142             if (afsd_logp->enabled) {
1143                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_HOLD_FID Index %08lX",
1144                           RequestBuffer->RequestIndex);
1145
1146                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1147             }
1148
1149             RDR_HoldFid( userp,
1150                          pHoldFidCB,
1151                          bFast,
1152                          RequestBuffer->ResultBufferLength,
1153                          &pResultCB);
1154
1155             break;
1156         }
1157
1158         case AFS_REQUEST_TYPE_RELEASE_FID:
1159         {
1160
1161             AFSReleaseFidRequestCB *pReleaseFidCB = (AFSReleaseFidRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1162
1163             if (afsd_logp->enabled) {
1164                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_RELEASE_FID Index %08lX",
1165                           RequestBuffer->RequestIndex);
1166
1167                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1168             }
1169
1170             RDR_ReleaseFid( userp,
1171                             pReleaseFidCB,
1172                             bFast,
1173                             RequestBuffer->ResultBufferLength,
1174                             &pResultCB);
1175
1176             break;
1177         }
1178
1179         case AFS_REQUEST_TYPE_CLEANUP_PROCESSING:
1180         {
1181
1182             AFSFileCleanupCB *pCleanupCB = (AFSFileCleanupCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1183
1184             if (afsd_logp->enabled) {
1185                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_CLEANUP_FILE Index %08lX File %08lX.%08lX.%08lX.%08lX",
1186                           RequestBuffer->RequestIndex,
1187                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1188                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1189
1190                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1191             }
1192
1193             RDR_CleanupFileEntry( userp,
1194                                   RequestBuffer->FileId,
1195                                   RequestBuffer->Name,
1196                                   RequestBuffer->NameLength,
1197                                   pCleanupCB,
1198                                   bWow64,
1199                                   bFlushFile,
1200                                   bDeleteFile,
1201                                   bUnlockFile,
1202                                   RequestBuffer->ResultBufferLength,
1203                                   &pResultCB);
1204
1205             break;
1206         }
1207
1208         case AFS_REQUEST_TYPE_PIPE_OPEN:
1209         {
1210             AFSPipeOpenCloseRequestCB *pPipeCB = (AFSPipeOpenCloseRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1211
1212             if (afsd_logp->enabled) {
1213                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIPE_OPEN Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
1214                           RequestBuffer->RequestIndex,
1215                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1216                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1217
1218                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1219             }
1220
1221             RDR_PipeOpen( userp,
1222                           RequestBuffer->FileId,
1223                           RequestBuffer->Name,
1224                           RequestBuffer->NameLength,
1225                           pPipeCB,
1226                           bWow64,
1227                           RequestBuffer->ResultBufferLength,
1228                           &pResultCB);
1229             break;
1230         }
1231
1232         case AFS_REQUEST_TYPE_PIPE_WRITE:
1233         {
1234             AFSPipeIORequestCB *pPipeCB = (AFSPipeIORequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1235             BYTE *pPipeData = ((BYTE *)RequestBuffer->Name + RequestBuffer->DataOffset + sizeof(AFSPipeIORequestCB));
1236
1237             if (afsd_logp->enabled) {
1238                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIPE_WRITE Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
1239                           RequestBuffer->RequestIndex,
1240                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1241                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1242
1243                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1244             }
1245
1246             RDR_PipeWrite( userp,
1247                            RequestBuffer->FileId,
1248                            pPipeCB,
1249                            pPipeData,
1250                            bWow64,
1251                            RequestBuffer->ResultBufferLength,
1252                            &pResultCB);
1253             break;
1254         }
1255
1256         case AFS_REQUEST_TYPE_PIPE_READ:
1257         {
1258             AFSPipeIORequestCB *pPipeCB = (AFSPipeIORequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1259
1260             if (afsd_logp->enabled) {
1261                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIPE_READ Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
1262                           RequestBuffer->RequestIndex,
1263                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1264                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1265
1266                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1267             }
1268
1269             RDR_PipeRead( userp,
1270                           RequestBuffer->FileId,
1271                           pPipeCB,
1272                           bWow64,
1273                           RequestBuffer->ResultBufferLength,
1274                           &pResultCB);
1275             break;
1276         }
1277
1278         case AFS_REQUEST_TYPE_PIPE_CLOSE:
1279         {
1280             AFSPipeOpenCloseRequestCB *pPipeCB = (AFSPipeOpenCloseRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1281
1282             if (afsd_logp->enabled) {
1283                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIPE_CLOSE Index %08lX Parent %08lX.%08lX.%08lX.%08lX",
1284                           RequestBuffer->RequestIndex,
1285                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1286                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1287
1288                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1289             }
1290
1291             RDR_PipeClose( userp,
1292                            RequestBuffer->FileId,
1293                            pPipeCB,
1294                            bWow64,
1295                            RequestBuffer->ResultBufferLength,
1296                            &pResultCB);
1297             break;
1298         }
1299
1300
1301         case AFS_REQUEST_TYPE_PIPE_TRANSCEIVE:
1302         {
1303             AFSPipeIORequestCB *pPipeCB = (AFSPipeIORequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1304             BYTE *pPipeData = ((BYTE *)RequestBuffer->Name + RequestBuffer->DataOffset + sizeof(AFSPipeIORequestCB));
1305
1306             if (afsd_logp->enabled) {
1307                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIPE_TRANSCEIVE Index %08lX",
1308                           RequestBuffer->RequestIndex);
1309
1310                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1311             }
1312
1313             RDR_PipeTransceive( userp,
1314                                 RequestBuffer->FileId,
1315                                 pPipeCB,
1316                                 pPipeData,
1317                                 bWow64,
1318                                 RequestBuffer->ResultBufferLength,
1319                                 &pResultCB);
1320             break;
1321         }
1322
1323         case AFS_REQUEST_TYPE_PIPE_QUERY_INFO:
1324         {
1325             AFSPipeInfoRequestCB *pPipeInfoCB = (AFSPipeInfoRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1326
1327             if (afsd_logp->enabled) {
1328                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIPE_QUERY_INFO Index %08lX",
1329                           RequestBuffer->RequestIndex);
1330
1331                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1332             }
1333
1334             RDR_PipeQueryInfo( userp,
1335                                RequestBuffer->FileId,
1336                                pPipeInfoCB,
1337                                bWow64,
1338                                RequestBuffer->ResultBufferLength,
1339                                &pResultCB);
1340             break;
1341         }
1342
1343         case AFS_REQUEST_TYPE_PIPE_SET_INFO:
1344         {
1345             AFSPipeInfoRequestCB *pPipeInfoCB = (AFSPipeInfoRequestCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1346             BYTE *pPipeData = ((BYTE *)RequestBuffer->Name + RequestBuffer->DataOffset + sizeof(AFSPipeInfoRequestCB));
1347
1348             if (afsd_logp->enabled) {
1349                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_PIPE_SET_INFO Index %08lX",
1350                           RequestBuffer->RequestIndex);
1351
1352                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1353             }
1354
1355             RDR_PipeSetInfo( userp,
1356                              RequestBuffer->FileId,
1357                              pPipeInfoCB,
1358                              pPipeData,
1359                              bWow64,
1360                              RequestBuffer->ResultBufferLength,
1361                              &pResultCB);
1362
1363             break;
1364         }
1365
1366
1367         case AFS_REQUEST_TYPE_PROCESS_READ_FILE: {
1368             AFSFileIOCB *pFileIOCB = (AFSFileIOCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1369
1370             if (afsd_logp->enabled) {
1371                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_READ_FILE Index %08lX FID %08lX.%08lX.%08lX.%08lX",
1372                           RequestBuffer->RequestIndex,
1373                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1374                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1375
1376                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1377             }
1378
1379             RDR_ReadFile( userp,
1380                           RequestBuffer->FileId,
1381                           &pFileIOCB->IOOffset,
1382                           pFileIOCB->IOLength,
1383                           pFileIOCB->MappedIOBuffer,
1384                           bWow64,
1385                           bCacheBypass,
1386                           RequestBuffer->ResultBufferLength,
1387                           &pResultCB);
1388
1389             break;
1390         }
1391
1392         case AFS_REQUEST_TYPE_PROCESS_WRITE_FILE: {
1393             AFSFileIOCB *pFileIOCB = (AFSFileIOCB *)((char *)RequestBuffer->Name + RequestBuffer->DataOffset);
1394
1395             if (afsd_logp->enabled) {
1396                 swprintf( wchBuffer, L"ProcessRequest Processing AFS_REQUEST_TYPE_WRITE_FILE Index %08lX FID %08lX.%08lX.%08lX.%08lX",
1397                           RequestBuffer->RequestIndex,
1398                           RequestBuffer->FileId.Cell, RequestBuffer->FileId.Volume,
1399                           RequestBuffer->FileId.Vnode, RequestBuffer->FileId.Unique);
1400
1401                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1402             }
1403
1404             RDR_WriteFile( userp,
1405                            RequestBuffer->FileId,
1406                            pFileIOCB,
1407                            &pFileIOCB->IOOffset,
1408                            pFileIOCB->IOLength,
1409                            pFileIOCB->MappedIOBuffer,
1410                            bWow64,
1411                            bCacheBypass,
1412                            RequestBuffer->ResultBufferLength,
1413                            &pResultCB);
1414
1415             break;
1416         }
1417
1418         default:
1419             bUnsupported = TRUE;
1420
1421             if (afsd_logp->enabled) {
1422                 swprintf( wchBuffer, L"ProcessRequest Received unknown request type %08lX Index %08lX",
1423                           RequestBuffer->RequestType,
1424                           RequestBuffer->RequestIndex);
1425
1426                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1427             }
1428
1429             break;
1430     }
1431
1432     if( BooleanFlagOn( RequestBuffer->RequestFlags, AFS_REQUEST_FLAG_SYNCHRONOUS))
1433     {
1434
1435         if (pResultCB == NULL) {
1436             // We failed probably due to a memory allocation error
1437             // unless the unsupported flag was set.
1438             pResultCB = &stResultCB;
1439             memset(&stResultCB, 0, sizeof(stResultCB));
1440             if ( bUnsupported )
1441                 pResultCB->ResultStatus = STATUS_NOT_IMPLEMENTED;
1442             else
1443                 pResultCB->ResultStatus = STATUS_NO_MEMORY;
1444         }
1445
1446         if (userp) {
1447             pResultCB->Authenticated = cm_HaveToken( userp,
1448                                                      RequestBuffer->FileId.Cell);
1449         }
1450
1451         //
1452         // This is how the filter associates the response information passed in the IOCtl below to the
1453         // original call. This request index is setup by the filter and should not be modified, otherwise the
1454         // filter will not be able to locate the request in its internal queue and the blocking thread will
1455         // not be awakened
1456         //
1457
1458         pResultCB->RequestIndex = RequestBuffer->RequestIndex;
1459
1460         if (afsd_logp->enabled) {
1461             swprintf( wchBuffer,
1462                       L"ProcessRequest Responding to Index %08lX Length %08lX",
1463                       pResultCB->RequestIndex,
1464                       pResultCB->ResultBufferLength);
1465
1466             osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1467         }
1468
1469         //
1470         // Now post the result back to the driver.
1471         //
1472
1473         if( !RDR_DeviceIoControl( glDevHandle,
1474                                   IOCTL_AFS_PROCESS_IRP_RESULT,
1475                                   (void *)pResultCB,
1476                                   sizeof( AFSCommResult) + pResultCB->ResultBufferLength,
1477                                   (void *)NULL,
1478                                   0,
1479                                   &bytesReturned ))
1480         {
1481             char *pBuffer = (char *)wchBuffer;
1482             gle = GetLastError();
1483             if (afsd_logp->enabled) {
1484                 swprintf( wchBuffer,
1485                           L"Failed to post IOCTL_AFS_PROCESS_IRP_RESULT gle %X", gle);
1486                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1487             }
1488
1489             if (gle != ERROR_NOT_READY) {
1490                 sprintf( pBuffer,
1491                          "Failed to post IOCTL_AFS_PROCESS_IRP_RESULT gle %X",
1492                          GetLastError());
1493                 osi_panic(pBuffer, __FILE__, __LINE__);
1494             }
1495         }
1496
1497     }
1498     else if (RequestBuffer->RequestType == AFS_REQUEST_TYPE_REQUEST_FILE_EXTENTS) {
1499
1500         if (SetFileExtentsResultCB) {
1501
1502             if (1 || afsd_logp->enabled) {
1503                 if (SetFileExtentsResultCB->ResultStatus != 0)
1504                     swprintf( wchBuffer,
1505                           L"ProcessRequest Responding Asynchronously with FAILURE to REQUEST_FILE_EXTENTS Index %08lX Count %08lX Status %08lX",
1506                           RequestBuffer->RequestIndex, SetFileExtentsResultCB->ExtentCount, SetFileExtentsResultCB->ResultStatus);
1507                 else
1508                     swprintf( wchBuffer,
1509                           L"ProcessRequest Responding Asynchronously with SUCCESS to REQUEST_FILE_EXTENTS Index %08lX Count %08lX",
1510                           RequestBuffer->RequestIndex, SetFileExtentsResultCB->ExtentCount);
1511
1512                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1513             }
1514
1515             if( (SetFileExtentsResultCB->ExtentCount != 0 ||
1516                  SetFileExtentsResultCB->ResultStatus != 0) &&
1517                 !RDR_DeviceIoControl( glDevHandle,
1518                                        IOCTL_AFS_SET_FILE_EXTENTS,
1519                                       (void *)SetFileExtentsResultCB,
1520                                       dwResultBufferLength,
1521                                       (void *)NULL,
1522                                       0,
1523                                       &bytesReturned ))
1524             {
1525                 gle = GetLastError();
1526                 if (afsd_logp->enabled) {
1527                     swprintf( wchBuffer,
1528                               L"Failed to post IOCTL_AFS_SET_FILE_EXTENTS gle %X",
1529                               gle);
1530                     osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1531                 }
1532
1533                 // The file system returns an error when it can't find the FID
1534                 // This is a bug in the file system but we should try to avoid
1535                 // the crash and clean up our own memory space.
1536                 //
1537                 // Since we couldn't deliver the extents to the file system
1538                 // we should release them.
1539                 if ( SetFileExtentsResultCB->ExtentCount != 0)
1540                 {
1541                     RDR_ReleaseFailedSetFileExtents( userp,
1542                                                  SetFileExtentsResultCB,
1543                                                  dwResultBufferLength);
1544                 }
1545
1546                 if (gle != ERROR_GEN_FAILURE &&
1547                     gle != ERROR_NOT_READY) {
1548                     sprintf( pBuffer,
1549                              "Failed to post IOCTL_AFS_SET_FILE_EXTENTS gle %X",
1550                              gle);
1551                     osi_panic(pBuffer, __FILE__, __LINE__);
1552                 }
1553             }
1554
1555             free(SetFileExtentsResultCB);
1556
1557       } else {
1558           /* Must be out of memory */
1559           if (afsd_logp->enabled) {
1560               swprintf( wchBuffer,
1561                         L"ProcessRequest Responding Asynchronously STATUS_NO_MEMORY to REQUEST_FILE_EXTENTS Index %08lX",
1562                         RequestBuffer->RequestIndex);
1563
1564               osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1565           }
1566
1567           RDR_SetFileStatus2( &RequestBuffer->FileId, &RequestBuffer->AuthGroup, STATUS_NO_MEMORY);
1568        }
1569     }
1570     else if (RequestBuffer->RequestType == AFS_REQUEST_TYPE_BYTE_RANGE_LOCK) {
1571
1572         if (afsd_logp->enabled) {
1573             swprintf( wchBuffer,
1574                       L"ProcessRequest Responding Asynchronously to REQUEST_TYPE_BYTE_RANGELOCK Index %08lX",
1575                       RequestBuffer->RequestIndex);
1576
1577             osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1578         }
1579
1580
1581         if (SetByteRangeLockResultCB) {
1582
1583             if( !RDR_DeviceIoControl( glDevHandle,
1584                                   IOCTL_AFS_SET_BYTE_RANGE_LOCKS,
1585                                   (void *)SetByteRangeLockResultCB,
1586                                   dwResultBufferLength,
1587                                   (void *)NULL,
1588                                   0,
1589                                   &bytesReturned ))
1590             {
1591                 gle = GetLastError();
1592
1593                 if (afsd_logp->enabled) {
1594                     swprintf( wchBuffer,
1595                               L"Failed to post IOCTL_AFS_SET_BYTE_RANGE_LOCKS gle 0x%x", gle);
1596                     osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1597                 }
1598
1599
1600                 if (gle != ERROR_NOT_READY) {
1601                     // TODO - instead of a panic we should release the locks
1602                     sprintf( pBuffer,
1603                              "Failed to post IOCTL_AFS_SET_BYTE_RANGE_LOCKS gle %X", gle);
1604                     osi_panic(pBuffer, __FILE__, __LINE__);
1605                 }
1606             }
1607
1608             free(SetByteRangeLockResultCB);
1609         } else {
1610             /* Must be out of memory */
1611             AFSSetByteRangeLockResultCB SetByteRangeLockResultCB;
1612
1613             dwResultBufferLength = sizeof(AFSSetByteRangeLockResultCB);
1614             memset( &SetByteRangeLockResultCB, '\0', dwResultBufferLength );
1615             SetByteRangeLockResultCB.FileId = RequestBuffer->FileId;
1616             SetByteRangeLockResultCB.Result[0].Status = STATUS_NO_MEMORY;
1617
1618             if( !RDR_DeviceIoControl( glDevHandle,
1619                                       IOCTL_AFS_SET_BYTE_RANGE_LOCKS,
1620                                       (void *)&SetByteRangeLockResultCB,
1621                                       dwResultBufferLength,
1622                                       (void *)NULL,
1623                                       0,
1624                                       &bytesReturned ))
1625             {
1626                 gle = GetLastError();
1627
1628                 if (afsd_logp->enabled) {
1629                     swprintf( wchBuffer,
1630                               L"Failed to post IOCTL_AFS_SET_BYTE_RANGE_LOCKS gle 0x%x", gle);
1631                     osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1632                 }
1633
1634                 /* We were out of memory - nothing to do */
1635             }
1636         }
1637     }
1638     else {
1639
1640         if (afsd_logp->enabled) {
1641             swprintf( wchBuffer,
1642                       L"ProcessRequest Not responding to async Index %08lX",
1643                       RequestBuffer->RequestIndex);
1644
1645             osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1646         }
1647     }
1648
1649     if (bRetry)
1650         goto retry;
1651
1652     if (userp)
1653         RDR_ReleaseUser(userp);
1654
1655
1656     if( pResultCB && pResultCB != &stResultCB)
1657     {
1658
1659         free( pResultCB);
1660     }
1661     return;
1662 }
1663
1664
1665 extern "C" DWORD
1666 RDR_SetFileExtents( AFSSetFileExtentsCB *pSetFileExtentsResultCB,
1667                     DWORD dwResultBufferLength)
1668 {
1669     WCHAR wchBuffer[1024];
1670     DWORD bytesReturned;
1671     DWORD gle;
1672
1673     if (1 || afsd_logp->enabled) {
1674         if (pSetFileExtentsResultCB->ResultStatus != 0)
1675             swprintf( wchBuffer,
1676                L"RDR_SetFileExtents IOCTL_AFS_SET_FILE_EXTENTS FAILURE Count %08lX Status %08lX",
1677                pSetFileExtentsResultCB->ExtentCount, pSetFileExtentsResultCB->ResultStatus);
1678         else
1679             swprintf( wchBuffer,
1680                L"RDR_SetFileExtents IOCTL_AFS_SET_FILE_EXTENTS SUCCESS Count %08lX",
1681                pSetFileExtentsResultCB->ExtentCount);
1682
1683         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1684     }
1685
1686     if( !RDR_DeviceIoControl( glDevHandle,
1687                               IOCTL_AFS_SET_FILE_EXTENTS,
1688                               (void *)pSetFileExtentsResultCB,
1689                               dwResultBufferLength,
1690                               (void *)NULL,
1691                               0,
1692                               &bytesReturned ))
1693     {
1694         gle = GetLastError();
1695         return gle;
1696     }
1697
1698     return 0;
1699 }
1700
1701
1702 extern "C" DWORD
1703 RDR_SetFileStatus( cm_fid_t *fidp,
1704                    GUID *pAuthGroup,
1705                    DWORD dwStatus)
1706 {
1707     WCHAR               wchBuffer[1024];
1708     AFSExtentFailureCB  SetFileStatusCB;
1709     DWORD               bytesReturned;
1710     DWORD               gle;
1711
1712     RDR_fid2FID(fidp, &SetFileStatusCB.FileId);
1713     memcpy(&SetFileStatusCB.AuthGroup, pAuthGroup, sizeof(GUID));
1714     SetFileStatusCB.FailureStatus = dwStatus;
1715
1716     if (afsd_logp->enabled) {
1717         swprintf( wchBuffer, L"RDR_SetFileStatus IOCTL_AFS_EXTENT_FAILURE_CB Fid %08lX.%08lX.%08lX.%08lX Status 0x%lX",
1718                   SetFileStatusCB.FileId.Cell, SetFileStatusCB.FileId.Volume,
1719                   SetFileStatusCB.FileId.Vnode, SetFileStatusCB.FileId.Unique,
1720                   dwStatus);
1721
1722         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1723     }
1724
1725     if( !RDR_DeviceIoControl( glDevHandle,
1726                               IOCTL_AFS_SET_FILE_EXTENT_FAILURE,
1727                               (void *)&SetFileStatusCB,
1728                               sizeof(AFSExtentFailureCB),
1729                               (void *)NULL,
1730                               0,
1731                               &bytesReturned ))
1732     {
1733         gle = GetLastError();
1734         return gle;
1735     }
1736
1737     return 0;
1738 }
1739
1740 static DWORD
1741 RDR_SetFileStatus2( AFSFileID *pFileId,
1742                    GUID *pAuthGroup,
1743                    DWORD dwStatus)
1744 {
1745     WCHAR               wchBuffer[1024];
1746     AFSExtentFailureCB  SetFileStatusCB;
1747     DWORD               bytesReturned;
1748     DWORD               gle;
1749
1750     memcpy(&SetFileStatusCB.FileId, pFileId, sizeof(AFSFileID));
1751     memcpy(&SetFileStatusCB.AuthGroup, pAuthGroup, sizeof(GUID));
1752     SetFileStatusCB.FailureStatus = dwStatus;
1753
1754     if (afsd_logp->enabled) {
1755         swprintf( wchBuffer, L"RDR_SetFileStatus2 IOCTL_AFS_EXTENT_FAILURE_CB Fid %08lX.%08lX.%08lX.%08lX Status 0x%lX",
1756                   SetFileStatusCB.FileId.Cell, SetFileStatusCB.FileId.Volume,
1757                   SetFileStatusCB.FileId.Vnode, SetFileStatusCB.FileId.Unique,
1758                   dwStatus);
1759
1760         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1761     }
1762
1763     if( !RDR_DeviceIoControl( glDevHandle,
1764                               IOCTL_AFS_SET_FILE_EXTENT_FAILURE,
1765                               (void *)&SetFileStatusCB,
1766                               sizeof(AFSExtentFailureCB),
1767                               (void *)NULL,
1768                               0,
1769                               &bytesReturned ))
1770     {
1771         gle = GetLastError();
1772         return gle;
1773     }
1774
1775     return 0;
1776 }
1777
1778 extern "C" DWORD
1779 RDR_RequestExtentRelease(cm_fid_t *fidp, LARGE_INTEGER numOfHeldExtents, DWORD numOfExtents, AFSFileExtentCB *extentList)
1780 {
1781
1782     HANDLE hDevHandle = NULL;
1783     DWORD bytesReturned;
1784     AFSReleaseFileExtentsCB *requestBuffer = NULL;
1785     AFSReleaseFileExtentsResultCB *responseBuffer = NULL;
1786     DWORD requestBufferLen, responseBufferLen;
1787     bool bError = false;
1788     DWORD rc = 0;
1789     WCHAR wchBuffer[256];
1790     DWORD gle;
1791
1792     if (ExitPending) {
1793         if (afsd_logp->enabled) {
1794             swprintf( wchBuffer,
1795                       L"IOCTL_AFS_RELEASE_FILE_EXTENTS request ignored due to shutdown pending");
1796
1797             osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1798         }
1799
1800         OutputDebugString(L"RDR_RequestExtentRequest ignored - shutdown pending\n");
1801         return CM_ERROR_WOULDBLOCK;
1802     }
1803
1804     if (afsd_logp->enabled) {
1805         swprintf( wchBuffer,
1806                   L"IOCTL_AFS_RELEASE_FILE_EXTENTS request - number %08lX",
1807                   numOfExtents);
1808
1809         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1810     }
1811
1812     //
1813     // We use the global handle to the control device instance
1814     //
1815
1816     hDevHandle = glDevHandle;
1817
1818     //
1819     // Allocate a request buffer.
1820     //
1821
1822     requestBufferLen = sizeof( AFSReleaseFileExtentsCB) + sizeof(AFSFileExtentCB) * numOfExtents;
1823     requestBuffer = (AFSReleaseFileExtentsCB *)malloc( requestBufferLen);
1824     responseBufferLen = (sizeof( AFSReleaseFileExtentsResultCB) + sizeof( AFSReleaseFileExtentsResultFileCB)) * numOfExtents;
1825     responseBuffer = (AFSReleaseFileExtentsResultCB *)malloc( responseBufferLen);
1826
1827
1828     if( requestBuffer && responseBuffer)
1829     {
1830
1831         memset( requestBuffer, '\0', sizeof( AFSReleaseFileExtentsCB));
1832         memset( responseBuffer, '\0', responseBufferLen);
1833
1834         // If there is a FID provided, use it
1835         if (fidp && extentList)
1836         {
1837             RDR_fid2FID( fidp, &requestBuffer->FileId);
1838
1839             memcpy(&requestBuffer->FileExtents, extentList, numOfExtents * sizeof(AFSFileExtentCB));
1840
1841             requestBuffer->Flags = 0;
1842         } else {
1843
1844             requestBuffer->Flags = AFS_RELEASE_EXTENTS_FLAGS_RELEASE_ALL;
1845         }
1846
1847         // Set the number of extents to be freed
1848         // Leave the rest of the structure as zeros to indicate free anything
1849         requestBuffer->ExtentCount = numOfExtents;
1850
1851         requestBuffer->HeldExtentCount = numOfHeldExtents;
1852
1853         if( !RDR_DeviceIoControl( hDevHandle,
1854                                   IOCTL_AFS_RELEASE_FILE_EXTENTS,
1855                                   (void *)requestBuffer,
1856                                   requestBufferLen,
1857                                   (void *)responseBuffer,
1858                                   responseBufferLen,
1859                                   &bytesReturned ))
1860         {
1861             //
1862             // Error condition back from driver
1863             //
1864             if (afsd_logp->enabled) {
1865                 gle = GetLastError();
1866                 swprintf( wchBuffer,
1867                           L"Failed to post IOCTL_AFS_RELEASE_FILE_EXTENTS - gle 0x%x", gle);
1868                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1869             }
1870             rc = -1;
1871             goto cleanup;
1872         }
1873
1874         //
1875         // Go process the request
1876         //
1877
1878         if (afsd_logp->enabled) {
1879             swprintf( wchBuffer,
1880                       L"IOCTL_AFS_RELEASE_FILE_EXTENTS returns - serial number %08lX flags %lX FileCount %lX",
1881                       responseBuffer->SerialNumber, responseBuffer->Flags, responseBuffer->FileCount);
1882             osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1883         }
1884
1885         rc = RDR_ProcessReleaseFileExtentsResult( responseBuffer, bytesReturned);
1886     } else {
1887
1888         rc = ENOMEM;
1889     }
1890
1891   cleanup:
1892     if (requestBuffer)
1893         free( requestBuffer);
1894     if (responseBuffer)
1895         free( responseBuffer);
1896
1897     return rc;
1898 }
1899
1900
1901 extern "C" DWORD
1902 RDR_NetworkStatus(BOOLEAN status)
1903 {
1904
1905     HANDLE hDevHandle = NULL;
1906     DWORD bytesReturned;
1907     AFSNetworkStatusCB *requestBuffer = NULL;
1908     DWORD rc = 0;
1909     WCHAR wchBuffer[256];
1910     DWORD gle;
1911
1912     if (afsd_logp->enabled) {
1913         swprintf( wchBuffer,
1914                   L"IOCTL_AFS_NETWORK_STATUS request - status %d",
1915                   status);
1916
1917         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1918     }
1919
1920     //
1921     // We use the global handle to the control device instance
1922     //
1923
1924     hDevHandle = glDevHandle;
1925
1926     //
1927     // Allocate a request buffer.
1928     //
1929
1930     requestBuffer = (AFSNetworkStatusCB *)malloc( sizeof( AFSNetworkStatusCB));
1931
1932
1933     if( requestBuffer)
1934     {
1935
1936         memset( requestBuffer, '\0', sizeof( AFSNetworkStatusCB));
1937
1938         // Set the number of extents to be freed
1939         // Leave the rest of the structure as zeros to indicate free anything
1940         requestBuffer->Online = status;
1941
1942         if( !RDR_DeviceIoControl( hDevHandle,
1943                                   IOCTL_AFS_NETWORK_STATUS,
1944                                   (void *)requestBuffer,
1945                                   sizeof( AFSNetworkStatusCB),
1946                                   NULL,
1947                                   0,
1948                                   &bytesReturned ))
1949         {
1950             //
1951             // Error condition back from driver
1952             //
1953             if (afsd_logp->enabled) {
1954                 gle = GetLastError();
1955                 swprintf( wchBuffer,
1956                           L"Failed to post IOCTL_AFS_NETWORK_STATUS gle 0x%x",
1957                           gle);
1958                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1959             }
1960
1961             rc = -1;
1962             goto cleanup;
1963         }
1964     } else
1965         rc = ENOMEM;
1966
1967   cleanup:
1968     if (requestBuffer)
1969         free( requestBuffer);
1970
1971     return rc;
1972 }
1973
1974
1975
1976 extern "C" DWORD
1977 RDR_VolumeStatus(ULONG cellID, ULONG volID, BOOLEAN online)
1978 {
1979
1980     HANDLE hDevHandle = NULL;
1981     DWORD bytesReturned;
1982     AFSVolumeStatusCB *requestBuffer = NULL;
1983     DWORD rc = 0;
1984     WCHAR wchBuffer[256];
1985     DWORD gle;
1986
1987     if (afsd_logp->enabled) {
1988         swprintf( wchBuffer,
1989                   L"IOCTL_AFS_VOLUME_STATUS request - cell 0x%x vol 0x%x online %d",
1990                   cellID, volID, online);
1991
1992         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
1993     }
1994
1995     //
1996     // We use the global handle to the control device instance
1997     //
1998
1999     hDevHandle = glDevHandle;
2000
2001     //
2002     // Allocate a request buffer.
2003     //
2004
2005     requestBuffer = (AFSVolumeStatusCB *)malloc( sizeof( AFSVolumeStatusCB));
2006
2007
2008     if( requestBuffer)
2009     {
2010
2011         memset( requestBuffer, '\0', sizeof( AFSVolumeStatusCB));
2012
2013         requestBuffer->FileID.Cell = cellID;
2014         requestBuffer->FileID.Volume = volID;
2015         requestBuffer->Online = online;
2016
2017         if( !RDR_DeviceIoControl( hDevHandle,
2018                                   IOCTL_AFS_VOLUME_STATUS,
2019                                   (void *)requestBuffer,
2020                                   sizeof( AFSVolumeStatusCB),
2021                                   NULL,
2022                                   0,
2023                                   &bytesReturned ))
2024         {
2025             //
2026             // Error condition back from driver
2027             //
2028
2029             if (afsd_logp->enabled) {
2030                 gle = GetLastError();
2031                 swprintf( wchBuffer,
2032                           L"Failed to post IOCTL_AFS_VOLUME_STATUS gle 0x%x", gle);
2033                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
2034             }
2035
2036             rc = -1;
2037             goto cleanup;
2038         }
2039     } else
2040         rc = ENOMEM;
2041
2042   cleanup:
2043     if (requestBuffer)
2044         free( requestBuffer);
2045
2046     return rc;
2047 }
2048
2049 extern "C" DWORD
2050 RDR_NetworkAddrChange(void)
2051 {
2052     return 0;
2053 }
2054
2055
2056 extern "C" DWORD
2057 RDR_InvalidateVolume(ULONG cellID, ULONG volID, ULONG reason)
2058 {
2059
2060     HANDLE hDevHandle = NULL;
2061     DWORD bytesReturned;
2062     AFSInvalidateCacheCB *requestBuffer = NULL;
2063     DWORD rc = 0;
2064     WCHAR wchBuffer[256];
2065     DWORD gle;
2066
2067     if (afsd_logp->enabled) {
2068         swprintf( wchBuffer,
2069                   L"IOCTL_AFS_INVALIDATE_CACHE (vol) request - cell 0x%x vol 0x%x reason %d",
2070                   cellID, volID, reason);
2071
2072         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
2073     }
2074
2075     //
2076     // We use the global handle to the control device instance
2077     //
2078
2079     hDevHandle = glDevHandle;
2080
2081     //
2082     // Allocate a request buffer.
2083     //
2084
2085     requestBuffer = (AFSInvalidateCacheCB *)malloc( sizeof( AFSInvalidateCacheCB));
2086
2087
2088     if( requestBuffer)
2089     {
2090
2091         memset( requestBuffer, '\0', sizeof( AFSInvalidateCacheCB));
2092
2093         requestBuffer->FileID.Cell = cellID;
2094         requestBuffer->FileID.Volume = volID;
2095         requestBuffer->WholeVolume = TRUE;
2096         requestBuffer->Reason = reason;
2097
2098         if( !RDR_DeviceIoControl( hDevHandle,
2099                                   IOCTL_AFS_INVALIDATE_CACHE,
2100                                   (void *)requestBuffer,
2101                                   sizeof( AFSInvalidateCacheCB),
2102                                   NULL,
2103                                   0,
2104                                   &bytesReturned ))
2105         {
2106             //
2107             // Error condition back from driver
2108             //
2109
2110             if (afsd_logp->enabled) {
2111                 gle = GetLastError();
2112                 swprintf( wchBuffer,
2113                           L"Failed to post IOCTL_AFS_INVALIDATE_VOLUME gle 0x%x", gle);
2114                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
2115             }
2116
2117             rc = -1;
2118             goto cleanup;
2119         }
2120     } else
2121         rc = ENOMEM;
2122
2123   cleanup:
2124     if (requestBuffer)
2125         free( requestBuffer);
2126
2127     return rc;
2128 }
2129
2130
2131 extern "C" DWORD
2132 RDR_InvalidateObject(ULONG cellID, ULONG volID, ULONG vnode, ULONG uniq, ULONG hash, ULONG fileType, ULONG reason)
2133 {
2134
2135     HANDLE hDevHandle = NULL;
2136     DWORD bytesReturned;
2137     AFSInvalidateCacheCB *requestBuffer = NULL;
2138     DWORD rc = 0;
2139     WCHAR wchBuffer[256];
2140     DWORD gle;
2141
2142     if (afsd_logp->enabled) {
2143         swprintf( wchBuffer,
2144                   L"IOCTL_AFS_INVALIDATE_CACHE (obj) request - cell 0x%x vol 0x%x vn 0x%x uniq 0x%x hash 0x%x type 0x%x reason %d",
2145                   cellID, volID, vnode, uniq, hash, fileType, reason);
2146
2147         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
2148     }
2149
2150     //
2151     // We use the global handle to the control device instance
2152     //
2153
2154     hDevHandle = glDevHandle;
2155
2156     //
2157     // Allocate a request buffer.
2158     //
2159
2160     requestBuffer = (AFSInvalidateCacheCB *)malloc( sizeof( AFSInvalidateCacheCB));
2161
2162
2163     if( requestBuffer)
2164     {
2165
2166         memset( requestBuffer, '\0', sizeof( AFSInvalidateCacheCB));
2167
2168         requestBuffer->FileID.Cell = cellID;
2169         requestBuffer->FileID.Volume = volID;
2170         requestBuffer->FileID.Vnode = vnode;
2171         requestBuffer->FileID.Unique = uniq;
2172         requestBuffer->FileID.Hash = hash;
2173         requestBuffer->FileType = fileType;
2174         requestBuffer->WholeVolume = FALSE;
2175         requestBuffer->Reason = reason;
2176
2177         if( !RDR_DeviceIoControl( hDevHandle,
2178                                   IOCTL_AFS_INVALIDATE_CACHE,
2179                                   (void *)requestBuffer,
2180                                   sizeof( AFSInvalidateCacheCB),
2181                                   NULL,
2182                                   0,
2183                                   &bytesReturned ))
2184         {
2185             //
2186             // Error condition back from driver
2187             //
2188             if (afsd_logp->enabled) {
2189                 gle = GetLastError();
2190                 swprintf( wchBuffer,
2191                           L"Failed to post IOCTL_AFS_INVALIDATE_CACHE gle 0x%x", gle);
2192                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
2193             }
2194
2195             rc = -1;
2196             goto cleanup;
2197         }
2198     } else
2199         rc = ENOMEM;
2200
2201   cleanup:
2202     if (requestBuffer)
2203         free( requestBuffer);
2204
2205     return rc;
2206 }
2207
2208
2209
2210 extern "C" DWORD
2211 RDR_SysName(ULONG Architecture, ULONG Count, WCHAR **NameList)
2212 {
2213
2214     HANDLE hDevHandle = NULL;
2215     DWORD bytesReturned;
2216     AFSSysNameNotificationCB *requestBuffer = NULL;
2217     DWORD rc = 0;
2218     WCHAR wchBuffer[256];
2219     DWORD Length;
2220     DWORD gle;
2221
2222     if (afsd_logp->enabled) {
2223         swprintf( wchBuffer,
2224                   L"IOCTL_AFS_SYSNAME_NOTIFICATION request - Arch %d Count %d",
2225                   Architecture, Count);
2226
2227         osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
2228     }
2229
2230     if (Count <= 0 || NameList == NULL)
2231         return -1;
2232
2233     //
2234     // We use the global handle to the control device instance
2235     //
2236
2237     hDevHandle = glDevHandle;
2238
2239     //
2240     // Allocate a request buffer.
2241     //
2242
2243     Length = sizeof (AFSSysNameNotificationCB) + (Count - 1) * sizeof (AFSSysName);
2244     requestBuffer = (AFSSysNameNotificationCB *)malloc( Length );
2245
2246
2247     if( requestBuffer)
2248     {
2249         unsigned int i;
2250
2251         memset( requestBuffer, '\0', Length);
2252
2253         requestBuffer->Architecture = Architecture;
2254         requestBuffer->NumberOfNames = Count;
2255         for ( i=0 ; i<Count; i++) {
2256             size_t len = wcslen(NameList[i]);
2257             requestBuffer->SysNames[i].Length = (ULONG) (len * sizeof(WCHAR));
2258             StringCchCopyNW(requestBuffer->SysNames[i].String, AFS_MAX_SYSNAME_LENGTH,
2259                             NameList[i], len);
2260         }
2261
2262         if( !RDR_DeviceIoControl( hDevHandle,
2263                                   IOCTL_AFS_SYSNAME_NOTIFICATION,
2264                                   (void *)requestBuffer,
2265                                   Length,
2266                                   NULL,
2267                                   0,
2268                                   &bytesReturned ))
2269         {
2270             //
2271             // Error condition back from driver
2272             //
2273             if (afsd_logp->enabled) {
2274                 gle = GetLastError();
2275                 swprintf( wchBuffer,
2276                           L"Failed to post IOCTL_AFS_SYSNAME_NOTIFICATION gle 0x%x", gle);
2277                 osi_Log1(afsd_logp, "%S", osi_LogSaveStringW(afsd_logp, wchBuffer));
2278             }
2279
2280             rc = -1;
2281             goto cleanup;
2282         }
2283     } else
2284         rc = ENOMEM;
2285
2286   cleanup:
2287     if (requestBuffer)
2288         free( requestBuffer);
2289
2290     return rc;
2291 }
2292
2293 extern "C" VOID
2294 RDR_Suspend( VOID)
2295 {
2296     ResetEvent( RDR_SuspendEvent);
2297 }
2298
2299 extern "C" VOID
2300 RDR_Resume( VOID)
2301 {
2302     SetEvent( RDR_SuspendEvent);
2303 }