Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
2.7 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 2000
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Utility routines for iScsi Port driver
  7. Environment:
  8. kernel mode only
  9. Revision History:
  10. --*/
  11. #include "port.h"
  12. PVOID
  13. iSpAllocatePool(
  14. IN POOL_TYPE PoolType,
  15. IN SIZE_T NumberOfBytes,
  16. IN ULONG Tag
  17. )
  18. {
  19. PVOID Block;
  20. Block = ExAllocatePoolWithTag(PoolType,
  21. NumberOfBytes,
  22. Tag);
  23. if (Block != NULL) {
  24. RtlZeroMemory(Block, NumberOfBytes);
  25. }
  26. return Block;
  27. }
  28. NTSTATUS
  29. iSpAllocateMdlAndIrp(
  30. IN PVOID Buffer,
  31. IN ULONG BufferLen,
  32. IN CCHAR StackSize,
  33. IN BOOLEAN NonPagedPool,
  34. OUT PIRP *Irp,
  35. OUT PMDL *Mdl
  36. )
  37. {
  38. PMDL localMdl = NULL;
  39. PIRP localIrp = NULL;
  40. NTSTATUS status;
  41. //
  42. // Allocate an MDL for this request
  43. //
  44. localMdl = IoAllocateMdl(Buffer,
  45. BufferLen,
  46. FALSE,
  47. FALSE,
  48. NULL);
  49. if (localMdl == NULL) {
  50. DebugPrint((0, "iSpAllocateMdlAndIrp : Failed to allocate MDL\n"));
  51. return STATUS_INSUFFICIENT_RESOURCES;
  52. }
  53. //
  54. // Initialize the MDL. If the buffer is from NonPaged pool
  55. // use MmBuildMdlForNonPagedPool. Else, use MmProbeAndLockPages
  56. //
  57. if (NonPagedPool == TRUE) {
  58. MmBuildMdlForNonPagedPool(localMdl);
  59. } else {
  60. try {
  61. MmProbeAndLockPages(localMdl, KernelMode, IoModifyAccess);
  62. } except(EXCEPTION_EXECUTE_HANDLER) {
  63. DebugPrint((0,
  64. "iSpAllocateMdlAndIrp : Failed to Lockpaged\n"));
  65. IoFreeMdl(localMdl);
  66. return STATUS_INSUFFICIENT_RESOURCES;
  67. }
  68. }
  69. //
  70. // Allocate an IRP
  71. //
  72. localIrp = IoAllocateIrp(StackSize, FALSE);
  73. if (localIrp == NULL) {
  74. DebugPrint((0, "iSpAllocateMdlAndIrp. Failed to allocate IRP\n"));
  75. IoFreeMdl(localMdl);
  76. return STATUS_INSUFFICIENT_RESOURCES;
  77. }
  78. DebugPrint((3, "Allocated IRP 0x%08x and MDL 0x%08x\n",
  79. localIrp, localMdl));
  80. *Irp = localIrp;
  81. *Mdl = localMdl;
  82. return STATUS_SUCCESS;
  83. }
  84. VOID
  85. iSpFreeMdlAndIrp(
  86. IN PMDL Mdl,
  87. IN PIRP Irp,
  88. BOOLEAN UnlockPages
  89. )
  90. {
  91. PMDL tmpMdlPtr = NULL;
  92. if (Irp == NULL) {
  93. return;
  94. }
  95. //
  96. // Free any MDLs allocated for this IRP
  97. //
  98. if (Mdl != NULL) {
  99. while ((Irp->MdlAddress) != NULL) {
  100. tmpMdlPtr = (Irp->MdlAddress)->Next;
  101. if (UnlockPages) {
  102. MmUnlockPages(Irp->MdlAddress);
  103. }
  104. IoFreeMdl(Irp->MdlAddress);
  105. Irp->MdlAddress = tmpMdlPtr;
  106. }
  107. }
  108. IoFreeIrp(Irp);
  109. }