* Sync up to trunk head (r65147).
[reactos.git] / drivers / filesystems / ntfs / blockdev.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/blockdev.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #include "ntfs.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 /* FUNCTIONS ****************************************************************/
34
35 NTSTATUS
36 NtfsReadDisk(IN PDEVICE_OBJECT DeviceObject,
37 IN LONGLONG StartingOffset,
38 IN ULONG Length,
39 IN OUT PUCHAR Buffer,
40 IN BOOLEAN Override)
41 {
42 PIO_STACK_LOCATION Stack;
43 IO_STATUS_BLOCK IoStatus;
44 LARGE_INTEGER Offset;
45 KEVENT Event;
46 PIRP Irp;
47 NTSTATUS Status;
48
49 DPRINT("NtfsReadDisk(%p, %I64x, %u, %p, %d)\n", DeviceObject, StartingOffset, Length, Buffer, Override);
50
51 KeInitializeEvent(&Event,
52 NotificationEvent,
53 FALSE);
54
55 Offset.QuadPart = StartingOffset;
56
57 DPRINT("Building synchronous FSD Request...\n");
58 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
59 DeviceObject,
60 Buffer,
61 Length,
62 &Offset,
63 &Event,
64 &IoStatus);
65 if (Irp == NULL)
66 {
67 DPRINT("IoBuildSynchronousFsdRequest failed\n");
68 return STATUS_INSUFFICIENT_RESOURCES;
69 }
70
71 if (Override)
72 {
73 Stack = IoGetNextIrpStackLocation(Irp);
74 Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
75 }
76
77 DPRINT("Calling IO Driver... with irp %p\n", Irp);
78 Status = IoCallDriver(DeviceObject, Irp);
79
80 DPRINT("Waiting for IO Operation for %p\n", Irp);
81 if (Status == STATUS_PENDING)
82 {
83 DPRINT("Operation pending\n");
84 KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
85 DPRINT("Getting IO Status... for %p\n", Irp);
86 Status = IoStatus.Status;
87 }
88
89 DPRINT("NtfsReadDisk() done (Status %x)\n", Status);
90
91 return Status;
92 }
93
94 NTSTATUS
95 NtfsReadSectors(IN PDEVICE_OBJECT DeviceObject,
96 IN ULONG DiskSector,
97 IN ULONG SectorCount,
98 IN ULONG SectorSize,
99 IN OUT PUCHAR Buffer,
100 IN BOOLEAN Override)
101 {
102 LONGLONG Offset;
103 ULONG BlockSize;
104
105 Offset = (LONGLONG)DiskSector * (LONGLONG)SectorSize;
106 BlockSize = SectorCount * SectorSize;
107
108 return NtfsReadDisk(DeviceObject, Offset, BlockSize, Buffer, Override);
109 }
110
111
112 NTSTATUS
113 NtfsDeviceIoControl(IN PDEVICE_OBJECT DeviceObject,
114 IN ULONG ControlCode,
115 IN PVOID InputBuffer,
116 IN ULONG InputBufferSize,
117 IN OUT PVOID OutputBuffer,
118 IN OUT PULONG OutputBufferSize,
119 IN BOOLEAN Override)
120 {
121 PIO_STACK_LOCATION Stack;
122 IO_STATUS_BLOCK IoStatus;
123 KEVENT Event;
124 PIRP Irp;
125 NTSTATUS Status;
126
127 KeInitializeEvent(&Event, NotificationEvent, FALSE);
128
129 DPRINT("Building device I/O control request ...\n");
130 Irp = IoBuildDeviceIoControlRequest(ControlCode,
131 DeviceObject,
132 InputBuffer,
133 InputBufferSize,
134 OutputBuffer,
135 (OutputBufferSize) ? *OutputBufferSize : 0,
136 FALSE,
137 &Event,
138 &IoStatus);
139 if (Irp == NULL)
140 {
141 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
142 return STATUS_INSUFFICIENT_RESOURCES;
143 }
144
145 if (Override)
146 {
147 Stack = IoGetNextIrpStackLocation(Irp);
148 Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
149 }
150
151 DPRINT("Calling IO Driver... with irp %p\n", Irp);
152 Status = IoCallDriver(DeviceObject, Irp);
153 if (Status == STATUS_PENDING)
154 {
155 KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
156 Status = IoStatus.Status;
157 }
158
159 if (OutputBufferSize)
160 {
161 *OutputBufferSize = IoStatus.Information;
162 }
163
164 return Status;
165 }
166
167 /* EOF */