IopSynchronousServiceTail

INT64 __fastcall IopSynchronousServiceTail(
        DEVICE_OBJECT *DeviceObject,
        IRP *Irp,
        FILE_OBJECT *FileObject,
        CHAR a4,
        CHAR a5,
        CHAR a6,
        UINT64 a7){
  __int64 v7; 
  _QWORD *p_Type; 
  _IO_COMPLETION_CONTEXT *CompletionContext; 
  unsigned __int64 *FileObjectExtension; 
  _DWORD *v14; 
  _ETHREAD *v15; 
  unsigned int v16; 
  char *v17; 
  __int64 v18; 
  _ETHREAD *v19; 
  unsigned int v20; 
  unsigned int v21; 
  unsigned int v22; 
  unsigned __int64 IoPriorityHint; 
  _BYTE *v24; 
  unsigned int v26; 
  KEVENT *v28; 
  int v29; 
  KPROCESSOR_MODE v30; 
  NTSTATUS v31; 
  unsigned __int8 CurrentIrql; 
  int v33; 
  _ETHREAD *CurrentThread; 
  __int64 v35; 
  int v36; 
  unsigned __int64 v37; 
  _GUID *v38; 
  _GUID *v39; 
  _BYTE *v40; 
  PVOID Object; 
  __int64 v42; 
  __int64 v43; 
  _GUID ActivityId; 
  v7 = *((_QWORD *)Irp + 11);
  p_Type = &FileObject->Type;
  Object = FileObject;
  if( (v7 & 1) != 0 )
  {
    *((_BYTE *)Irp + 71) |= 0x10u;
    v7 &= ~1ui64;
    *((_QWORD *)Irp + 11) = v7;
  }
  CompletionContext = FileObject->CompletionContext;
  if( a6 || v7 )
  {
    if( (FileObject->Flags & 2) != 0 )
      *((_BYTE *)Irp + 71) |= 2u;
    goto LABEL_13;
  }
  if( *((_QWORD *)Irp + 12) && !*((_QWORD *)Irp + 10) && CompletionContext )
  {
    if( IopQueueIrpToFileObject(Irp, FileObject) )
      goto LABEL_14;
    goto LABEL_13;
  }
  if( (*((_DWORD *)Irp + 4) & 0x10) != 0
    || CompletionContext
    || !FileObject->FileObjectExtension
    || (FileObjectExtension = (unsigned __int64 *)IopGetFileObjectExtension(FileObject, FoExtTypeIosbRange, 0i64)) == 0i64 )
  {
LABEL_13:
    IopQueueThreadIrp(Irp);
    goto LABEL_14;
  }
  while( 1 )
  {
    v37 = *((_QWORD *)Irp + 9);
    if( v37 >= *FileObjectExtension && v37 + 16 <= FileObjectExtension[1] )
    {
      p_Type = Object;
      if( FileObjectExtension[4] == *((_QWORD *)KeGetCurrentThread() + 23) )
      {
        if( IopQueueIrpToFileObject(Irp, (_FILE_OBJECT *)Object) )
          break;
      }
    }
    FileObjectExtension = (unsigned __int64 *)FileObjectExtension[5];
    if( !FileObjectExtension )
      goto LABEL_13;
  }
  *((_QWORD *)Irp + 9) += FileObjectExtension[3] - *FileObjectExtension;
LABEL_14:
  v14 = (_DWORD *)p_Type[26];
  if( v14 && (*v14 & 4) != 0 && PsIsProcessAppContainer(*((_EPROCESS **)KeGetCurrentThread() + 23)) )
  {
    v26 = -1073739504;
    *((_DWORD *)Irp + 12) = -1073739504;
    IofCompleteRequest(Irp, 0);
    v17 = (char *)Object;
    goto LABEL_33;
  }
  if( (unsigned int)a7 <= 1 )
  {
    v33 = *((_DWORD *)DeviceObject + 18);
    if( v33 == 8 || v33 == 7 || v33 == 9 || v33 == 36 )
      IoSetDiskIoAttributionFromThread(Irp, (_ETHREAD *)KeGetCurrentThread());
    CurrentThread = (_ETHREAD *)KeGetCurrentThread();
    if( (_DWORD)a7 )
    {
      ++*((_QWORD *)CurrentThread + 113);
      v16 = 12000;
    }
    else
    {
      ++*((_QWORD *)CurrentThread + 112);
      v16 = 11996;
    }
  }
  else
  {
    v15 = (_ETHREAD *)KeGetCurrentThread();
    ++*((_QWORD *)v15 + 114);
    v16 = 12004;
  }
  __incgsdword(v16);
  v17 = (char *)Object;
  if( !a6 )
    ObfReferenceObject(Object);
  v18 = p_Type[26];
  if( v18 && *(_DWORD *)(v18 + 80) )
  {
    v22 = *((_DWORD *)Irp + 4) & 0xFFF1FFFF;
    *((_DWORD *)Irp + 4) = v22;
    v21 = *(_DWORD *)(v18 + 80);
  }
  else
  {
    v19 = (_ETHREAD *)KeGetCurrentThread();
    v20 = (*((_DWORD *)v19 + 324) >> 9) & 7;
    if( (*(_DWORD *)(*((_QWORD *)v19 + 68) + 1124i64) & 0x100000) != 0 )
      v20 = 0;
    if( v20 < 2 && v19 == (_ETHREAD *)KeGetCurrentThread() && *((_DWORD *)v19 + 340) )
      v20 = 2;
    v21 = v20 + 1;
    v17 = (char *)Object;
    v22 = *((_DWORD *)Irp + 4) & 0xFFF1FFFF;
    *((_DWORD *)Irp + 4) = v22;
  }
  *((_DWORD *)Irp + 4) = v22 | (v21 << 17);
  IoPriorityHint = (unsigned int)IoGetIoPriorityHint(Irp);
  if( *((_BYTE *)Irp + 64) )
    goto LABEL_25;
  if( (int)IoPriorityHint < 2 )
  {
    v35 = *((_QWORD *)Irp + 19);
    if( v35 && ((*(_DWORD *)(v35 + 116) & 0x400) != 0 || (*(_DWORD *)(v35 + 1300) & 0x80u) != 0) )
    {
LABEL_25:
      if( (int)IoPriorityHint < 2 )
      {
        if( (_DWORD)a7 )
        {
          if( (_DWORD)a7 == 1 )
            ++IoLowPriorityWriteOperationCount;
        }
        else
        {
          ++*(_DWORD *)IoLowPriorityReadOperationCount;
        }
      }
      goto LABEL_26;
    }
    v36 = *((_DWORD *)Irp + 4);
    ++IoKernelIssuedIoBoostedCount;
    *((_DWORD *)Irp + 4) = v36 & 0xFFF1FFFF | 0x60000;
  }
LABEL_26:
  if( *((char *)Irp + 71) >= 0 && (v24 = (_BYTE *)*((_QWORD *)Irp + 25)) != 0i64 && (*v24 & 2) != 0 )
  {
    ActivityId = *(_GUID *)(*((_QWORD *)Irp + 25) + 24i64);
    IoSetActivityIdThread(&ActivityId, (_BYTE *)IoPriorityHint);
    v39 = v38;
    v26 = IofCallDriver(DeviceObject, Irp);
    IoSetActivityIdThread(v39, v40);
  }
  else
  {
    v26 = IofCallDriver(DeviceObject, Irp);
  }
  if( !a6 )
    ObDereferenceObjectDeferDelete(v17);
LABEL_33:
  if( a4 && v26 != 259 )
  {
    v43 = 0i64;
    v42 = 0i64;
    CurrentIrql = KeGetCurrentIrql();
    __writecr8(1ui64);
    IopCompleteRequest((__int64)Irp + 120, (__int64)&v43, &v42, (ULONG_PTR *)&Object, &v42);
    __writecr8(CurrentIrql);
  }
  if( a6 )
  {
    if( v26 == 259 )
    {
      v28 = (KEVENT *)(v17 + 152);
      v29 = *((_DWORD *)v17 + 20) & 4;
      while( (v28->Header.Type & 0x7F) != 0 || !*((_DWORD *)v17 + 39) )
      {
        v30 = v29 ? a5 : 0;
        v31 = KeWaitForSingleObject(v28, Executive, v30, 1u, 0i64);
        if( v31 != 257 && v31 != 192 )
          break;
        if( v29 )
          goto LABEL_98;
        if( (*((_DWORD *)KeGetCurrentThread() + 324) & 1) != 0 || IopCheckIrpCancelled((INT64)v28, (INT64)Irp) )
        {
          v17 = (char *)Object;
LABEL_98:
          IopCancelAlertedRequest(v28, Irp);
          break;
        }
        v17 = (char *)Object;
      }
      v26 = *((_DWORD *)v17 + 14);
    }
    IopReleaseFileObjectLock((PADAPTER_OBJECT)v17);
  }
  else if( CompletionContext && (v26 & 0xC0000000) == 0x80000000 )
  {
    return 259i64;
  }
  return v26;
}

Referenced by:

IopSetEaOrQuotaInformationFile
IopXxxControlFile
NtFlushBuffersFileEx
NtLockFile
NtNotifyChangeDirectoryFileEx
NtQueryDirectoryFileEx
NtQueryEaFile
NtQueryQuotaInformationFile
NtQueryVolumeInformationFile
NtReadFile
NtReadFileScatter
NtSetEaFile
NtSetVolumeInformationFile
NtUnlockFile
NtWriteFile
NtWriteFileGather