Lots of changes to the kernel
[reactos.git] / reactos / drivers / fs / ext2 / blockdev.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: services/fs/ext2/blockdev.c
5 * PURPOSE: Temporary sector reading support
6 * PROGRAMMER: David Welch (welch@cwcom.net)
7 * UPDATE HISTORY:
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ddk/ntddk.h>
13 #include <string.h>
14 #include <internal/string.h>
15
16 //#define NDEBUG
17 #include <internal/debug.h>
18
19 #include "ext2fs.h"
20
21 /* FUNCTIONS ***************************************************************/
22
23 BOOLEAN Ext2ReadSectors(IN PDEVICE_OBJECT pDeviceObject,
24 IN ULONG DiskSector,
25 IN ULONG SectorCount,
26 IN PVOID Buffer)
27 {
28 LARGE_INTEGER sectorNumber;
29 PIRP irp;
30 IO_STATUS_BLOCK ioStatus;
31 KEVENT event;
32 NTSTATUS status;
33 ULONG sectorSize;
34 int j;
35
36 DPRINT("VFATReadSector(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
37 pDeviceObject,DiskSector,Buffer);
38
39 sectorNumber.u.HighPart = 0;
40 sectorNumber.u.LowPart = DiskSector * BLOCKSIZE;
41
42 DPRINT("DiskSector:%ld BLKSZ:%ld sectorNumber:%ld:%ld\n",
43 (unsigned long) DiskSector,
44 (unsigned long) BLOCKSIZE,
45 (unsigned long) sectorNumber.u.HighPart,
46 (unsigned long) sectorNumber.u.LowPart);
47
48 KeInitializeEvent(&event, NotificationEvent, FALSE);
49
50 sectorSize = BLOCKSIZE*SectorCount;
51
52
53 DPRINT("Building synchronous FSD Request...\n");
54 irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
55 pDeviceObject,
56 Buffer,
57 sectorSize,
58 &sectorNumber,
59 &event,
60 &ioStatus );
61
62 if (!irp)
63 {
64 DbgPrint("READ failed!!!\n");
65 return FALSE;
66 }
67
68 DPRINT("Calling IO Driver...\n");
69 status = IoCallDriver(pDeviceObject, irp);
70
71 DPRINT("Waiting for IO Operation...\n");
72 if (status == STATUS_PENDING)
73 {
74 KeWaitForSingleObject(&event,
75 Suspended,
76 KernelMode,
77 FALSE,
78 NULL);
79 DPRINT("Getting IO Status...\n");
80 status = ioStatus.Status;
81 }
82
83 if (!NT_SUCCESS(status))
84 {
85 DbgPrint("IO failed!!! Error code: %d(%x)\n", status, status);
86 return FALSE;
87 }
88
89 return TRUE;
90 }
91
92 BOOLEAN VFATWriteSectors(IN PDEVICE_OBJECT pDeviceObject,
93 IN ULONG DiskSector,
94 IN ULONG SectorCount,
95 IN UCHAR* Buffer)
96 {
97 LARGE_INTEGER sectorNumber;
98 PIRP irp;
99 IO_STATUS_BLOCK ioStatus;
100 KEVENT event;
101 NTSTATUS status;
102 ULONG sectorSize;
103 PULONG mbr;
104 int j;
105
106 DPRINT("VFATWriteSector(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
107 pDeviceObject,DiskSector,Buffer);
108
109 sectorNumber.u.HighPart = 0;
110 sectorNumber.u.LowPart = DiskSector * BLOCKSIZE;
111
112 KeInitializeEvent(&event, NotificationEvent, FALSE);
113
114 sectorSize = BLOCKSIZE*SectorCount;
115
116
117 DPRINT("Building synchronous FSD Request...\n");
118 irp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE,
119 pDeviceObject,
120 Buffer,
121 sectorSize,
122 &sectorNumber,
123 &event,
124 &ioStatus );
125
126 if (!irp) {
127 DbgPrint("WRITE failed!!!\n");
128 return FALSE;
129 }
130
131 DPRINT("Calling IO Driver...\n");
132 status = IoCallDriver(pDeviceObject,
133 irp);
134
135 DPRINT("Waiting for IO Operation...\n");
136 if (status == STATUS_PENDING) {
137 KeWaitForSingleObject(&event,
138 Suspended,
139 KernelMode,
140 FALSE,
141 NULL);
142 DPRINT("Getting IO Status...\n");
143 status = ioStatus.Status;
144 }
145
146 if (!NT_SUCCESS(status)) {
147 DbgPrint("IO failed!!! Error code: %d(%x)\n", status, status);
148 return FALSE;
149 }
150
151
152 ExFreePool(mbr);
153 DPRINT("Block request succeeded\n");
154 return TRUE;
155 }
156