1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
| #include <ntifs.h> #include <windef.h> #include <mountmgr.h> #include <mountdev.h> #include <ntddvol.h> #include <ntstrsafe.h> #include "DPBitmap.h" #include "Driver.h"
PDP_FILTER_DEV_EXTENSION gProtectDevExt = NULL;
VOID DPReinitializationRoutine(IN PDRIVER_OBJECT DriverObject, IN PVOID Context, IN ULONG Count) { NTSTATUS ntStatus; WCHAR SparseFilename[] = L"\\??\\E:\\temp.dat"; UNICODE_STRING SparseFilenameUni; IO_STATUS_BLOCK ios = { 0 }; OBJECT_ATTRIBUTES ObjAttr = { 0 }; FILE_END_OF_FILE_INFORMATION FileEndInfo = { 0 }; RtlInitUnicodeString(&SparseFilenameUni, SparseFilename); InitializeObjectAttributes(&ObjAttr, &SparseFilenameUni, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL); ntStatus = ZwCreateFile(&gProtectDevExt->TempFile, GENERIC_READ | GENERIC_WRITE, &ObjAttr, &ios, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, FILE_NON_DIRECTORY_FILE | FILE_RANDOM_ACCESS | FILE_SYNCHRONOUS_IO_NONALERT | FILE_NO_INTERMEDIATE_BUFFERING, NULL, 0); if (!NT_SUCCESS(ntStatus)) goto ERROUT; ntStatus = ZwFsControlFile(gProtectDevExt->TempFile, NULL, NULL, NULL, &ios, FSCTL_SET_SPARSE, NULL, 0, NULL, 0); if (!NT_SUCCESS(ntStatus)) goto ERROUT; FileEndInfo.EndOfFile.QuadPart = gProtectDevExt->TotalSizeInByte.QuadPart + 10 * 1024 * 1024; ntStatus = ZwSetInformationFile(gProtectDevExt->TempFile, &ios, &FileEndInfo, sizeof(FILE_END_OF_FILE_INFORMATION), FileEndOfFileInformation); if (!NT_SUCCESS(ntStatus)) goto ERROUT; gProtectDevExt->Protect = TRUE; return; ERROUT: KdPrint(("error create temp file!\n")); return; };
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) { int i; for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++) DriverObject->MajorFunction[i] = DPDispatchAny; DriverObject->MajorFunction[IRP_MJ_POWER] = DPDispatchPower; DriverObject->MajorFunction[IRP_MJ_PNP] = DPDispatchPnp; DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DPDispatchDeviceControl; DriverObject->MajorFunction[IRP_MJ_READ] = DPDispatchReadWrite; DriverObject->MajorFunction[IRP_MJ_WRITE] = DPDispatchReadWrite; DriverObject->DriverExtension->AddDevice = DPAddDevice; DriverObject->DriverUnload = DPUnload; IoRegisterBootDriverReinitialization(DriverObject, DPReinitializationRoutine, NULL); return STATUS_SUCCESS; };
NTSTATUS DPCompleteRequest(IN PIRP Irp, IN NTSTATUS Status, IN CCHAR Priority) { Irp->IoStatus.Status = Status; IoCompleteRequest(Irp, Priority); return STATUS_SUCCESS; };
NTSTATUS DPSendToNextDriver(IN PDEVICE_OBJECT TgtDevObj, IN PIRP Irp) { IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(TgtDevObj, Irp); };
NTSTATUS DPIrpCompletionRoutine(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context) { PKEVENT Event = (PKEVENT)Context; UNREFERENCED_PARAMETER(DeviceObject); UNREFERENCED_PARAMETER(Irp); KeSetEvent(Event, IO_NO_INCREMENT, FALSE); return STATUS_MORE_PROCESSING_REQUIRED; };
NTSTATUS DPForwardIrpSync(IN PDEVICE_OBJECT TgtDevObj, IN PIRP Irp) { KEVENT event; NTSTATUS status; KeInitializeEvent(&event, NotificationEvent, FALSE); IoCopyCurrentIrpStackLocationToNext(Irp); IoSetCompletionRoutine(Irp, DPIrpCompletionRoutine, &event, TRUE, TRUE, TRUE); status = IoCallDriver(TgtDevObj, Irp); if (status == STATUS_PENDING) { KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL); status = Irp->IoStatus.Status; }; return status; };
VOID DPReadWriteThread(IN PVOID Context){ NTSTATUS ntStatus = STATUS_SUCCESS; PDP_FILTER_DEV_EXTENSION DevExt = (PDP_FILTER_DEV_EXTENSION)Context; PLIST_ENTRY ReqEntry = NULL; PIRP Irp = NULL; PIO_STACK_LOCATION Irpsp = NULL; PBYTE sysBuf = NULL; ULONG length = 0; LARGE_INTEGER offset = { 0 }; PBYTE fileBuf = NULL; PBYTE devBuf = NULL; IO_STATUS_BLOCK ios; KeSetPriorityThread(KeGetCurrentThread(), LOW_REALTIME_PRIORITY); for (;;){ KeWaitForSingleObject(&DevExt->ReqEvent,Executive,KernelMode,FALSE,NULL); if (DevExt->ThreadTermFlag) { PsTerminateSystemThread(STATUS_SUCCESS); return; }; while (ReqEntry = ExInterlockedRemoveHeadList(&DevExt->ReqList,&DevExt->ReqLock)){ Irp = CONTAINING_RECORD(ReqEntry, IRP, Tail.Overlay.ListEntry); Irpsp = IoGetCurrentIrpStackLocation(Irp); if (NULL == Irp->MdlAddress) sysBuf = (PBYTE)Irp->UserBuffer; else sysBuf = (PBYTE)MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority); if (IRP_MJ_READ == Irpsp->MajorFunction) { offset = Irpsp->Parameters.Read.ByteOffset; length = Irpsp->Parameters.Read.Length; } else if (IRP_MJ_WRITE == Irpsp->MajorFunction) { offset = Irpsp->Parameters.Write.ByteOffset; length = Irpsp->Parameters.Write.Length; } else { offset.QuadPart = 0; length = 0; }; if (NULL == sysBuf || 0 == length) goto ERRNEXT; if (IRP_MJ_READ == Irpsp->MajorFunction) { long tstResult = DPBitmapTest(DevExt->Bitmap, offset, length); switch (tstResult) { case BITMAP_RANGE_CLEAR: goto ERRNEXT; case BITMAP_RANGE_SET: if (NULL == (fileBuf = (PBYTE)ExAllocatePoolWithTag(NonPagedPool, length, 'xypD'))) { ntStatus = STATUS_INSUFFICIENT_RESOURCES; Irp->IoStatus.Information = 0; goto ERRERR; }; RtlZeroMemory(fileBuf, length); ntStatus = ZwReadFile(DevExt->TempFile, NULL, NULL, NULL, &ios, fileBuf, length, &offset, NULL); if (NT_SUCCESS(ntStatus)) { Irp->IoStatus.Information = length; RtlCopyMemory(sysBuf, fileBuf, Irp->IoStatus.Information); goto ERRCMPLT; } else { ntStatus = STATUS_INSUFFICIENT_RESOURCES; Irp->IoStatus.Information = 0; goto ERRERR; }; break; case BITMAP_RANGE_BLEND: if (NULL == (fileBuf = (PBYTE)ExAllocatePoolWithTag(NonPagedPool, length, 'xypD'))) { ntStatus = STATUS_INSUFFICIENT_RESOURCES; Irp->IoStatus.Information = 0; goto ERRERR; }; RtlZeroMemory(fileBuf, length); if (NULL == (devBuf = (PBYTE)ExAllocatePoolWithTag(NonPagedPool, length, 'xypD'))) { ntStatus = STATUS_INSUFFICIENT_RESOURCES; Irp->IoStatus.Information = 0; goto ERRERR; }; RtlZeroMemory(devBuf, length); ntStatus = ZwReadFile(DevExt->TempFile, NULL, NULL, NULL, &ios, fileBuf, length, &offset, NULL); if (!NT_SUCCESS(ntStatus)) { ntStatus = STATUS_INSUFFICIENT_RESOURCES; Irp->IoStatus.Information = 0; goto ERRERR; }; ntStatus = DPForwardIrpSync(DevExt->LowerDevObj, Irp); if (!NT_SUCCESS(ntStatus)) { ntStatus = STATUS_INSUFFICIENT_RESOURCES; Irp->IoStatus.Information = 0; goto ERRERR; }; memcpy(devBuf, sysBuf, Irp->IoStatus.Information); ntStatus = DPBitmapGet(DevExt->Bitmap, offset, length, devBuf, fileBuf); if (!NT_SUCCESS(ntStatus)) { ntStatus = STATUS_INSUFFICIENT_RESOURCES; Irp->IoStatus.Information = 0; goto ERRERR; }; memcpy(sysBuf, devBuf, Irp->IoStatus.Information); goto ERRCMPLT; default: ntStatus = STATUS_INSUFFICIENT_RESOURCES; goto ERRERR; }; } else { ntStatus = ZwWriteFile(DevExt->TempFile, NULL, NULL, NULL, &ios, sysBuf, length, &offset, NULL); if (!NT_SUCCESS(ntStatus)) { ntStatus = STATUS_INSUFFICIENT_RESOURCES; goto ERRERR; } else { if (NT_SUCCESS(ntStatus = DPBitmapSet(DevExt->Bitmap, offset, length))) goto ERRCMPLT; else { ntStatus = STATUS_INSUFFICIENT_RESOURCES; goto ERRERR; }; }; }; ERRERR: if (NULL != fileBuf) { ExFreePool(fileBuf); fileBuf = NULL; }; if (NULL != devBuf) { ExFreePool(devBuf); devBuf = NULL; }; DPCompleteRequest(Irp,ntStatus,IO_NO_INCREMENT); continue; ERRNEXT: if (NULL != fileBuf) { ExFreePool(fileBuf); fileBuf = NULL; }; if (NULL != devBuf) { ExFreePool(devBuf); devBuf = NULL; }; DPSendToNextDriver(DevExt->LowerDevObj,Irp); continue; ERRCMPLT: if (NULL != fileBuf) { ExFreePool(fileBuf); fileBuf = NULL; }; if (NULL != devBuf) { ExFreePool(devBuf); devBuf = NULL; }; DPCompleteRequest(Irp,STATUS_SUCCESS,IO_DISK_INCREMENT); continue; }; }; return; };
NTSTATUS DPAddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT PhysicalDeviceObject) { NTSTATUS ntStatus = STATUS_SUCCESS; PDP_FILTER_DEV_EXTENSION DevExt = NULL; PDEVICE_OBJECT LowerDevObj = NULL; PDEVICE_OBJECT FltDevObj = NULL; HANDLE ThreadHandle = NULL; ntStatus = IoCreateDevice(DriverObject, sizeof(DP_FILTER_DEV_EXTENSION), NULL, FILE_DEVICE_DISK, FILE_DEVICE_SECURE_OPEN, FALSE, &FltDevObj); if (!NT_SUCCESS(ntStatus)) goto ERROUT; DevExt = FltDevObj->DeviceExtension; RtlZeroMemory(DevExt, sizeof(DP_FILTER_DEV_EXTENSION)); LowerDevObj = IoAttachDeviceToDeviceStack(FltDevObj, PhysicalDeviceObject); if (NULL == LowerDevObj) { ntStatus = STATUS_NO_SUCH_DEVICE; goto ERROUT; }; KeInitializeEvent(&DevExt->PagingPathCountEvent, NotificationEvent, TRUE); FltDevObj->Flags = LowerDevObj->Flags; FltDevObj->Flags |= DO_POWER_PAGABLE; FltDevObj->Flags &= ~DO_DEVICE_INITIALIZING; DevExt->FltDevObj = FltDevObj; DevExt->PhyDevObj = PhysicalDeviceObject; DevExt->LowerDevObj = LowerDevObj; InitializeListHead(&DevExt->ReqList); KeInitializeSpinLock(&DevExt->ReqLock); KeInitializeEvent(&DevExt->ReqEvent, SynchronizationEvent, FALSE); DevExt->ThreadTermFlag = FALSE; ntStatus = PsCreateSystemThread(&ThreadHandle, (ACCESS_MASK)0L, NULL, NULL, NULL, DPReadWriteThread, DevExt); if (!NT_SUCCESS(ntStatus)) goto ERROUT; ntStatus = ObReferenceObjectByHandle(ThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &DevExt->ThreadHandle, NULL); if (!NT_SUCCESS(ntStatus)) { DevExt->ThreadTermFlag = TRUE; KeSetEvent(&DevExt->ReqEvent, (KPRIORITY)0, FALSE); goto ERROUT; }; ERROUT: if (!NT_SUCCESS(ntStatus)) { if (NULL != LowerDevObj) { IoDetachDevice(LowerDevObj); DevExt->LowerDevObj = NULL; }; if (NULL != FltDevObj) { IoDeleteDevice(FltDevObj); DevExt->FltDevObj = NULL; }; }; if (NULL != ThreadHandle) ZwClose(ThreadHandle); return ntStatus; };
VOID DPUnload(IN PDRIVER_OBJECT DriverObject) { UNREFERENCED_PARAMETER(DriverObject); return; };
NTSTATUS DPDispatchAny(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { NTSTATUS ntStatus = STATUS_SUCCESS; PDP_FILTER_DEV_EXTENSION DevExt = DeviceObject->DeviceExtension; return DPSendToNextDriver(DevExt->LowerDevObj, Irp); };
NTSTATUS DPDispatchPower(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PDP_FILTER_DEV_EXTENSION DevExt = DeviceObject->DeviceExtension; #if (NTDDI_VERSION < NTDDI_VISTA) PoStartNextPowerIrp(Irp); IoSkipCurrentIrpStackLocation(Irp); return PoCallDriver(DevExt->LowerDevObj, Irp); #else return DPSendToNextDriver(DevExt->LowerDevObj, Irp); #endif };
NTSTATUS DPDispatchPnp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PDP_FILTER_DEV_EXTENSION DevExt = DeviceObject->DeviceExtension; NTSTATUS ntStatus = STATUS_SUCCESS; PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp); switch (irpsp->MinorFunction) { case IRP_MN_REMOVE_DEVICE: { if (DevExt->ThreadTermFlag != TRUE && NULL != DevExt->ThreadHandle) { DevExt->ThreadTermFlag = TRUE; KeSetEvent(&DevExt->ReqEvent, (KPRIORITY)0, FALSE); KeWaitForSingleObject(DevExt->ThreadHandle, Executive, KernelMode, FALSE, NULL); ObDereferenceObject(DevExt->ThreadHandle); }; if (NULL != DevExt->Bitmap) DPBitmapFree(DevExt->Bitmap); if (NULL != DevExt->LowerDevObj) IoDetachDevice(DevExt->LowerDevObj); if (NULL != DevExt->FltDevObj) IoDeleteDevice(DevExt->FltDevObj); break; }; case IRP_MN_DEVICE_USAGE_NOTIFICATION: { BOOLEAN setPagable; if (irpsp->Parameters.UsageNotification.Type != DeviceUsageTypePaging) { ntStatus = DPSendToNextDriver(DevExt->LowerDevObj, Irp); return ntStatus; }; ntStatus = KeWaitForSingleObject(&DevExt->PagingPathCountEvent, Executive, KernelMode, FALSE, NULL); setPagable = FALSE; if (!irpsp->Parameters.UsageNotification.InPath && DevExt->PagingPathCount == 1) { if (DeviceObject->Flags & DO_POWER_INRUSH) {} else { DeviceObject->Flags |= DO_POWER_PAGABLE; setPagable = TRUE; }; }; ntStatus = DPForwardIrpSync(DevExt->LowerDevObj, Irp); if (NT_SUCCESS(ntStatus)) { IoAdjustPagingPathCount(&DevExt->PagingPathCount, irpsp->Parameters.UsageNotification.InPath); if (irpsp->Parameters.UsageNotification.InPath) if (DevExt->PagingPathCount == 1) DeviceObject->Flags &= ~DO_POWER_PAGABLE; } else if (setPagable == TRUE) { DeviceObject->Flags &= ~DO_POWER_PAGABLE; setPagable = FALSE; }; KeSetEvent(&DevExt->PagingPathCountEvent, IO_NO_INCREMENT, FALSE); IoCompleteRequest(Irp, IO_NO_INCREMENT); return ntStatus; }; default: break; }; return DPSendToNextDriver(DevExt->LowerDevObj, Irp); };
NTSTATUS DPQueryVolumeInformationCompletionRoutine(IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIrp, IN PVOID Context) { KeSetEvent((PKEVENT)Context, IO_NO_INCREMENT, FALSE); return STATUS_MORE_PROCESSING_REQUIRED; };
NTSTATUS DPQueryVolumeInformation(PDEVICE_OBJECT DevObj, PLARGE_INTEGER TotalSize, PDWORD ClusterSize, PDWORD SectorSize) { #define _FileSystemNameLength 64 #define FAT16_SIG_OFFSET 54
#define FAT32_SIG_OFFSET 82
#define NTFS_SIG_OFFSET 3
const UCHAR FAT16FLG[4] = { 'F','A','T','1' }; const UCHAR FAT32FLG[4] = { 'F','A','T','3' }; const UCHAR NTFSFLG[4] = { 'N','T','F','S' }; NTSTATUS ntStatus = STATUS_SUCCESS; BYTE DBR[512] = { 0 }; ULONG DBRLength = 512; PDP_NTFS_BOOT_SECTOR pNtfsBootSector = (PDP_NTFS_BOOT_SECTOR)DBR; PDP_FAT32_BOOT_SECTOR pFat32BootSector = (PDP_FAT32_BOOT_SECTOR)DBR; PDP_FAT16_BOOT_SECTOR pFat16BootSector = (PDP_FAT16_BOOT_SECTOR)DBR; LARGE_INTEGER readOffset = { 0 }; IO_STATUS_BLOCK ios; KEVENT Event; PIRP pIrp = NULL;
KeInitializeEvent(&Event, NotificationEvent, FALSE); pIrp = IoBuildAsynchronousFsdRequest(IRP_MJ_READ, DevObj, DBR, DBRLength, &readOffset, &ios); if (NULL == pIrp) goto ERROUT; IoSetCompletionRoutine(pIrp, DPQueryVolumeInformationCompletionRoutine, &Event, TRUE, TRUE, TRUE); ntStatus = IoCallDriver(DevObj, pIrp); if (ntStatus = STATUS_PENDING) { ntStatus = KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL); ntStatus = pIrp->IoStatus.Status; if (!NT_SUCCESS(ntStatus)) goto ERROUT; }; if (*(DWORD*)NTFSFLG == *(DWORD*)&DBR[NTFS_SIG_OFFSET]) { *SectorSize = (DWORD)(pNtfsBootSector->BytesPerSector); *ClusterSize = (*SectorSize) * (DWORD)(pNtfsBootSector->SectorsPerCluster); TotalSize->QuadPart = (LONGLONG)(*SectorSize) * (LONGLONG)pNtfsBootSector->TotalSectors; } else if (*(DWORD*)FAT32FLG == *(DWORD*)&DBR[FAT32_SIG_OFFSET]) { *SectorSize = (DWORD)(pFat32BootSector->BytesPerSector); *ClusterSize = (*SectorSize) * (DWORD)(pFat32BootSector->SectorsPerCluster); TotalSize->QuadPart = (LONGLONG)(*SectorSize) * (LONGLONG)(pFat32BootSector->LargeSectors + pFat32BootSector->Sectors); } else if (*(DWORD*)FAT16FLG == *(DWORD*)&DBR[FAT16_SIG_OFFSET]) { *SectorSize = (DWORD)(pFat16BootSector->BytesPerSector); *ClusterSize = (*SectorSize) * (DWORD)(pFat16BootSector->SectorsPerCluster); TotalSize->QuadPart = (LONGLONG)(*SectorSize) * (LONGLONG)(pFat16BootSector->LargeSectors + pFat16BootSector->Sectors); } else ntStatus = STATUS_UNSUCCESSFUL; ERROUT: if (NULL != pIrp) IoFreeIrp(pIrp); return ntStatus; };
NTSTATUS DPVolumeOnLineCompleteRoutine(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOLUME_ONLINE_CONTEXT Context) { NTSTATUS ntStatus = STATUS_SUCCESS; UNICODE_STRING DosName = { 0 }; ASSERT(Context != NULL); ntStatus = IoVolumeDeviceToDosName(Context->DevExt->PhyDevObj, &DosName); if (!NT_SUCCESS(ntStatus)) goto ERROUT; Context->DevExt->VolumeLetter = DosName.Buffer[0]; if (Context->DevExt->VolumeLetter > L'Z') Context->DevExt->VolumeLetter -= (L'a' - L'A'); if (Context->DevExt->VolumeLetter == L'D') { ntStatus = DPQueryVolumeInformation(Context->DevExt->PhyDevObj, &(Context->DevExt->TotalSizeInByte), &(Context->DevExt->ClusterSizeInByte), &(Context->DevExt->SectorSizeInByte)); if (!NT_SUCCESS(ntStatus)) goto ERROUT; ntStatus = DPBitmapInit(&Context->DevExt->Bitmap, Context->DevExt->SectorSizeInByte, 8, 25600, (DWORD)(Context->DevExt->TotalSizeInByte.QuadPart / (LONGLONG)(25600 * 8 * Context->DevExt->SectorSizeInByte)) + 1); if (!NT_SUCCESS(ntStatus)) goto ERROUT; gProtectDevExt = Context->DevExt; }; ERROUT: if (!NT_SUCCESS(ntStatus)) { if (NULL != Context->DevExt->Bitmap) DPBitmapFree(Context->DevExt->Bitmap); if (NULL != Context->DevExt->TempFile) ZwClose(Context->DevExt->TempFile); }; if (NULL != DosName.Buffer) ExFreePool(DosName.Buffer); KeSetEvent(Context->Event, 0, FALSE); return STATUS_SUCCESS; };
NTSTATUS DPDispatchDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PDP_FILTER_DEV_EXTENSION DevExt = DeviceObject->DeviceExtension; NTSTATUS ntStatus = STATUS_SUCCESS; PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp); KEVENT Event; VOLUME_ONLINE_CONTEXT context; switch (irpsp->Parameters.DeviceIoControl.IoControlCode) { case IOCTL_VOLUME_ONLINE: { KeInitializeEvent(&Event, NotificationEvent, FALSE); context.DevExt = DevExt; context.Event = &Event; IoCopyCurrentIrpStackLocationToNext(Irp); IoSetCompletionRoutine(Irp, DPVolumeOnLineCompleteRoutine, &context, TRUE, TRUE, TRUE); ntStatus = IoCallDriver(DevExt->LowerDevObj, Irp); KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL); return ntStatus; }; default: break; }; return DPSendToNextDriver(DevExt->LowerDevObj, Irp); };
NTSTATUS DPDispatchReadWrite(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) { PDP_FILTER_DEV_EXTENSION DevExt = DeviceObject->DeviceExtension; NTSTATUS ntStatus = STATUS_SUCCESS; if (DevExt->Protect) { IoMarkIrpPending(Irp); ExInterlockedInsertTailList(&DevExt->ReqList, &Irp->Tail.Overlay.ListEntry, &DevExt->ReqLock); KeSetEvent(&DevExt->ReqEvent, (KPRIORITY)0, FALSE); return STATUS_PENDING; } else return DPSendToNextDriver(DevExt->LowerDevObj, Irp); };
|