Implemented read support.
[reactos.git] / reactos / drivers / fs / cdfs / rw.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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: rw.c,v 1.2 2002/05/01 21:52:05 ekohl Exp $
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: services/fs/cdfs/rw.c
24 * PURPOSE: CDROM (ISO 9660) filesystem driver
25 * PROGRAMMER: Art Yerkes
26 * UPDATE HISTORY:
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include <ddk/ntddk.h>
32 #include <ntos/minmax.h>
33
34 #define NDEBUG
35 #include <debug.h>
36
37 #include "cdfs.h"
38
39
40 /* GLOBALS *******************************************************************/
41
42 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
43 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
44
45
46 /* FUNCTIONS ****************************************************************/
47
48 static NTSTATUS
49 CdfsReadFile(PDEVICE_EXTENSION DeviceExt,
50 PFILE_OBJECT FileObject,
51 PUCHAR Buffer,
52 ULONG Length,
53 ULONG ReadOffset,
54 PULONG LengthRead)
55 /*
56 * FUNCTION: Reads data from a file
57 */
58 {
59 NTSTATUS Status = STATUS_SUCCESS;
60 PUCHAR TempBuffer;
61 ULONG TempLength;
62 PCCB Ccb;
63 PFCB Fcb;
64
65 DPRINT("CdfsReadFile(ReadOffset %lu Length %lu)\n", ReadOffset, Length);
66
67 *LengthRead = 0;
68
69 if (Length == 0)
70 return STATUS_SUCCESS;
71
72 Ccb = (PCCB)FileObject->FsContext2;
73 Fcb = Ccb->Fcb;
74
75 if (ReadOffset + Length > Fcb->Entry.DataLengthL)
76 Length = Fcb->Entry.DataLengthL - ReadOffset;
77
78 DPRINT("Reading %d bytes at %d\n", Length, ReadOffset);
79
80 if (Length == 0)
81 return(STATUS_UNSUCCESSFUL);
82
83 #if 0
84 if ((FileObject->Flags & FO_NO_INTERMEDIATE_BUFFERING) == 0)
85 {
86 LARGE_INTEGER FileOffset;
87 IO_STATUS_BLOCK IoStatus;
88
89 FileOffset.QuadPart = ReadOffset;
90 CcCopyRead(FileObject,
91 &FileOffset,
92 Length,
93 TRUE,
94 Buffer,
95 &IoStatus);
96 *LengthRead = IoStatus.Information;
97
98 return(IoStatus.Status);
99 }
100 #endif
101
102 if ((ReadOffset % BLOCKSIZE) != 0)
103 {
104 TempLength = min(Length, BLOCKSIZE - (ReadOffset % BLOCKSIZE));
105 TempBuffer = ExAllocatePool(NonPagedPool, BLOCKSIZE);
106
107 Status = CdfsReadSectors(DeviceExt->StorageDevice,
108 Fcb->Entry.ExtentLocationL + (ReadOffset / BLOCKSIZE),
109 1,
110 TempBuffer);
111 if (NT_SUCCESS(Status))
112 {
113 memcpy(Buffer, TempBuffer + (ReadOffset % BLOCKSIZE), TempLength);
114 (*LengthRead) = (*LengthRead) + TempLength;
115 Length = Length - TempLength;
116 Buffer = Buffer + TempLength;
117 ReadOffset = ReadOffset + TempLength;
118 }
119 ExFreePool(TempBuffer);
120 }
121
122 DPRINT("Status %lx\n", Status);
123
124 if ((Length / BLOCKSIZE) != 0 && NT_SUCCESS(Status))
125 {
126 TempLength = ROUND_DOWN(Length, BLOCKSIZE);
127 Status = CdfsReadSectors(DeviceExt->StorageDevice,
128 Fcb->Entry.ExtentLocationL + (ReadOffset / BLOCKSIZE),
129 Length / BLOCKSIZE,
130 Buffer);
131 if (NT_SUCCESS(Status))
132 {
133 (*LengthRead) = (*LengthRead) + TempLength;
134 Length = Length - TempLength;
135 Buffer = Buffer + TempLength;
136 ReadOffset = ReadOffset + TempLength;
137 }
138 }
139
140 DPRINT("Status %lx\n", Status);
141
142 if (Length > 0 && NT_SUCCESS(Status))
143 {
144 TempBuffer = ExAllocatePool(NonPagedPool, BLOCKSIZE);
145
146 Status = CdfsReadSectors(DeviceExt->StorageDevice,
147 Fcb->Entry.ExtentLocationL + (ReadOffset / BLOCKSIZE),
148 1,
149 TempBuffer);
150 if (NT_SUCCESS(Status))
151 {
152 memcpy(Buffer, TempBuffer, Length);
153 (*LengthRead) = (*LengthRead) + Length;
154 }
155 ExFreePool(TempBuffer);
156 }
157
158 return(Status);
159 }
160
161
162 NTSTATUS STDCALL
163 CdfsRead(PDEVICE_OBJECT DeviceObject,
164 PIRP Irp)
165 {
166 PDEVICE_EXTENSION DeviceExt;
167 PIO_STACK_LOCATION Stack;
168 PFILE_OBJECT FileObject;
169 PVOID Buffer;
170 ULONG ReadLength;
171 LARGE_INTEGER ReadOffset;
172 ULONG ReturnedReadLength = 0;
173 NTSTATUS Status = STATUS_SUCCESS;
174
175 DPRINT("CdfsRead(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
176
177 if (Irp->Flags & IRP_PAGING_IO)
178 {
179 Status = STATUS_NOT_SUPPORTED;
180 goto ByeBye;
181 }
182
183 DeviceExt = DeviceObject->DeviceExtension;
184 Stack = IoGetCurrentIrpStackLocation(Irp);
185 FileObject = Stack->FileObject;
186
187 ReadLength = Stack->Parameters.Read.Length;
188 ReadOffset = Stack->Parameters.Read.ByteOffset;
189 Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
190
191 Status = CdfsReadFile(DeviceExt,
192 FileObject,
193 Buffer,
194 ReadLength,
195 ReadOffset.u.LowPart,
196 &ReturnedReadLength);
197
198 ByeBye:
199 if (NT_SUCCESS(Status))
200 {
201 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
202 {
203 FileObject->CurrentByteOffset.QuadPart =
204 ReadOffset.QuadPart + ReturnedReadLength;
205 }
206 Irp->IoStatus.Information = ReturnedReadLength;
207 }
208 else
209 {
210 Irp->IoStatus.Information = 0;
211 }
212
213 Irp->IoStatus.Status = Status;
214 IoCompleteRequest(Irp,IO_NO_INCREMENT);
215
216 return(Status);
217 }
218
219
220 NTSTATUS STDCALL
221 CdfsWrite(PDEVICE_OBJECT DeviceObject,
222 PIRP Irp)
223 {
224 DPRINT("CdfsWrite(DeviceObject %x Irp %x)\n",DeviceObject,Irp);
225
226 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
227 Irp->IoStatus.Information = 0;
228 return(STATUS_NOT_SUPPORTED);
229 }
230
231 /* EOF */