- Update to r53061
[reactos.git] / drivers / filesystems / cdfs / common.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002, 2003 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: drivers/fs/cdfs/common.c
24 * PURPOSE: CDROM (ISO 9660) filesystem driver
25 * PROGRAMMER: Art Yerkes
26 * Eric Kohl
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include "cdfs.h"
32
33 #define NDEBUG
34 #include <debug.h>
35
36 /* FUNCTIONS ****************************************************************/
37
38 NTSTATUS
39 CdfsReadSectors(IN PDEVICE_OBJECT DeviceObject,
40 IN ULONG DiskSector,
41 IN ULONG SectorCount,
42 IN OUT PUCHAR Buffer,
43 IN BOOLEAN Override)
44 {
45 PIO_STACK_LOCATION Stack;
46 IO_STATUS_BLOCK IoStatus;
47 LARGE_INTEGER Offset;
48 ULONG BlockSize;
49 KEVENT Event;
50 PIRP Irp;
51 NTSTATUS Status;
52
53 again:
54 KeInitializeEvent(&Event,
55 NotificationEvent,
56 FALSE);
57
58 Offset.u.LowPart = DiskSector << 11;
59 Offset.u.HighPart = DiskSector >> 21;
60
61 BlockSize = BLOCKSIZE * SectorCount;
62
63 DPRINT("CdfsReadSectors(DeviceObject %x, DiskSector %d, Buffer %x)\n",
64 DeviceObject, DiskSector, Buffer);
65 DPRINT("Offset %I64x BlockSize %ld\n",
66 Offset.QuadPart,
67 BlockSize);
68
69 DPRINT("Building synchronous FSD Request...\n");
70 Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
71 DeviceObject,
72 Buffer,
73 BlockSize,
74 &Offset,
75 &Event,
76 &IoStatus);
77 if (Irp == NULL)
78 {
79 DPRINT("IoBuildSynchronousFsdRequest failed\n");
80 return(STATUS_INSUFFICIENT_RESOURCES);
81 }
82
83 if (Override)
84 {
85 Stack = IoGetNextIrpStackLocation(Irp);
86 Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
87 }
88
89 DPRINT("Calling IO Driver... with irp %x\n", Irp);
90 Status = IoCallDriver(DeviceObject, Irp);
91
92 DPRINT("Waiting for IO Operation for %x\n", Irp);
93 if (Status == STATUS_PENDING)
94 {
95 DPRINT("Operation pending\n");
96 KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
97 DPRINT("Getting IO Status... for %x\n", Irp);
98 Status = IoStatus.Status;
99 }
100
101 if (!NT_SUCCESS(Status))
102 {
103 if (Status == STATUS_VERIFY_REQUIRED)
104 {
105 PDEVICE_OBJECT DeviceToVerify;
106
107 DPRINT1("STATUS_VERIFY_REQUIRED\n");
108 DeviceToVerify = IoGetDeviceToVerify(PsGetCurrentThread());
109 IoSetDeviceToVerify(PsGetCurrentThread(), NULL);
110
111 Status = IoVerifyVolume(DeviceToVerify, FALSE);
112 DPRINT1("IoVerifyVolume() returned (Status %lx)\n", Status);
113
114 if (NT_SUCCESS(Status))
115 {
116 DPRINT1("Volume verify succeeded; trying request again\n");
117 goto again;
118 }
119 }
120
121 DPRINT("CdfsReadSectors() failed (Status %x)\n", Status);
122 DPRINT("(DeviceObject %x, DiskSector %x, Buffer %x, Offset 0x%I64x)\n",
123 DeviceObject, DiskSector, Buffer,
124 Offset.QuadPart);
125 return(Status);
126 }
127
128 DPRINT("Block request succeeded for %x\n", Irp);
129
130 return(STATUS_SUCCESS);
131 }
132
133
134 NTSTATUS
135 CdfsDeviceIoControl (IN PDEVICE_OBJECT DeviceObject,
136 IN ULONG CtlCode,
137 IN PVOID InputBuffer,
138 IN ULONG InputBufferSize,
139 IN OUT PVOID OutputBuffer,
140 IN OUT PULONG OutputBufferSize,
141 IN BOOLEAN Override)
142 {
143 PIO_STACK_LOCATION Stack;
144 IO_STATUS_BLOCK IoStatus;
145 KEVENT Event;
146 PIRP Irp;
147 NTSTATUS Status;
148
149 DPRINT("CdfsDeviceIoControl(DeviceObject %x, CtlCode %x, "
150 "InputBuffer %x, InputBufferSize %x, OutputBuffer %x, "
151 "POutputBufferSize %x (%x)\n", DeviceObject, CtlCode,
152 InputBuffer, InputBufferSize, OutputBuffer, OutputBufferSize,
153 OutputBufferSize ? *OutputBufferSize : 0);
154
155 again:
156 KeInitializeEvent (&Event, NotificationEvent, FALSE);
157
158 DPRINT("Building device I/O control request ...\n");
159 Irp = IoBuildDeviceIoControlRequest(CtlCode,
160 DeviceObject,
161 InputBuffer,
162 InputBufferSize,
163 OutputBuffer,
164 (OutputBufferSize != NULL) ? *OutputBufferSize : 0,
165 FALSE,
166 &Event,
167 &IoStatus);
168 if (Irp == NULL)
169 {
170 DPRINT("IoBuildDeviceIoControlRequest failed\n");
171 return STATUS_INSUFFICIENT_RESOURCES;
172 }
173
174 if (Override)
175 {
176 Stack = IoGetNextIrpStackLocation(Irp);
177 Stack->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
178 }
179
180 DPRINT ("Calling IO Driver... with irp %x\n", Irp);
181 Status = IoCallDriver(DeviceObject, Irp);
182
183 DPRINT ("Waiting for IO Operation for %x\n", Irp);
184 if (Status == STATUS_PENDING)
185 {
186 DPRINT ("Operation pending\n");
187 KeWaitForSingleObject (&Event, Suspended, KernelMode, FALSE, NULL);
188 DPRINT ("Getting IO Status... for %x\n", Irp);
189
190 Status = IoStatus.Status;
191 }
192
193 if (OutputBufferSize != NULL)
194 {
195 *OutputBufferSize = IoStatus.Information;
196 }
197
198 if (Status == STATUS_VERIFY_REQUIRED)
199 {
200 PDEVICE_OBJECT DeviceToVerify;
201
202 DPRINT1("STATUS_VERIFY_REQUIRED\n");
203 DeviceToVerify = IoGetDeviceToVerify(PsGetCurrentThread());
204 IoSetDeviceToVerify(PsGetCurrentThread(), NULL);
205
206 if (DeviceToVerify)
207 {
208 Status = IoVerifyVolume(DeviceToVerify, FALSE);
209 DPRINT1("IoVerifyVolume() returned (Status %lx)\n", Status);
210 }
211
212 if (NT_SUCCESS(Status))
213 {
214 DPRINT1("Volume verify succeeded; trying request again\n");
215 goto again;
216 }
217 }
218
219 DPRINT("Returning Status %x\n", Status);
220
221 return Status;
222 }
223
224 /* EOF */