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