Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers...
[reactos.git] / drivers / filesystems / cdfs / rw.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 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: drivers/filesystems/cdfs/rw.c
23 * PURPOSE: CDROM (ISO 9660) filesystem driver
24 * PROGRAMMER: Art Yerkes
25 * Eric Kohl
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include "cdfs.h"
31
32 #define NDEBUG
33 #include <debug.h>
34
35 /* FUNCTIONS ****************************************************************/
36
37 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
38 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
39
40
41 /* FUNCTIONS ****************************************************************/
42
43 static NTSTATUS
44 CdfsReadFile(PCDFS_IRP_CONTEXT IrpContext,
45 PFILE_OBJECT FileObject,
46 PUCHAR Buffer,
47 ULONG Length,
48 ULONG ReadOffset,
49 ULONG IrpFlags,
50 PULONG LengthRead)
51 /*
52 * FUNCTION: Reads data from a file
53 */
54 {
55 NTSTATUS Status = STATUS_SUCCESS;
56 PDEVICE_EXTENSION DeviceExt;
57 PFCB Fcb;
58 ULONG ToRead = Length;
59
60 DPRINT("CdfsReadFile(ReadOffset %lu Length %lu)\n", ReadOffset, Length);
61
62 *LengthRead = 0;
63
64 if (Length == 0)
65 return(STATUS_SUCCESS);
66
67 DeviceExt = IrpContext->DeviceObject->DeviceExtension;
68 Fcb = (PFCB)FileObject->FsContext;
69
70 if (ReadOffset >= Fcb->Entry.DataLengthL)
71 return(STATUS_END_OF_FILE);
72
73 if (ReadOffset + Length > Fcb->Entry.DataLengthL)
74 ToRead = Fcb->Entry.DataLengthL - ReadOffset;
75
76 if (!(IrpFlags & IRP_PAGING_IO) &&
77 FsRtlAreThereCurrentFileLocks(&Fcb->FileLock))
78 {
79 if (!FsRtlCheckLockForReadAccess(&Fcb->FileLock, IrpContext->Irp))
80 {
81 return STATUS_FILE_LOCK_CONFLICT;
82 }
83 }
84
85 DPRINT("Reading %u bytes at %u\n", Length, ReadOffset);
86
87 if (!(IrpFlags & (IRP_NOCACHE|IRP_PAGING_IO)))
88 {
89 LARGE_INTEGER FileOffset;
90 IO_STATUS_BLOCK IoStatus;
91 CC_FILE_SIZES FileSizes;
92
93 DPRINT("Using cache\n");
94
95 if (FileObject->PrivateCacheMap == NULL)
96 {
97 FileSizes.AllocationSize = Fcb->RFCB.AllocationSize;
98 FileSizes.FileSize = Fcb->RFCB.FileSize;
99 FileSizes.ValidDataLength = Fcb->RFCB.ValidDataLength;
100
101 DPRINT("Attach FCB to File: Size %08x%08x\n",
102 Fcb->RFCB.ValidDataLength.HighPart,
103 Fcb->RFCB.ValidDataLength.LowPart);
104
105 _SEH2_TRY
106 {
107 CcInitializeCacheMap(FileObject,
108 &FileSizes,
109 FALSE,
110 &(CdfsGlobalData->CacheMgrCallbacks),
111 Fcb);
112 }
113 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
114 {
115 return _SEH2_GetExceptionCode();
116 }
117 _SEH2_END;
118 }
119
120 FileOffset.QuadPart = (LONGLONG)ReadOffset;
121 _SEH2_TRY
122 {
123 CcCopyRead(FileObject,
124 &FileOffset,
125 ToRead,
126 TRUE,
127 Buffer,
128 &IoStatus);
129 }
130 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
131 {
132 IoStatus.Information = 0;
133 IoStatus.Status = _SEH2_GetExceptionCode();
134 }
135 _SEH2_END;
136 *LengthRead = IoStatus.Information;
137
138 Status = IoStatus.Status;
139 }
140 else
141 {
142 ULONG ActualReadOffset = ROUND_DOWN(ReadOffset, BLOCKSIZE);
143 ULONG nBlocks = (ROUND_UP(ReadOffset + ToRead, BLOCKSIZE) - ActualReadOffset) / BLOCKSIZE;
144 PUCHAR PageBuf;
145 BOOLEAN bFreeBuffer = FALSE;
146 if ((ReadOffset % BLOCKSIZE) != 0 || (ToRead % BLOCKSIZE) != 0)
147 {
148 PageBuf = ExAllocatePoolWithTag(NonPagedPool,
149 nBlocks * BLOCKSIZE,
150 CDFS_TAG);
151 if (!PageBuf)
152 {
153 return STATUS_NO_MEMORY;
154 }
155 bFreeBuffer = TRUE;
156 }
157 else
158 {
159 PageBuf = Buffer;
160 }
161 Status = CdfsReadSectors(DeviceExt->StorageDevice,
162 Fcb->Entry.ExtentLocationL + (ActualReadOffset / BLOCKSIZE),
163 nBlocks,
164 (PVOID)PageBuf,
165 FALSE);
166
167 if(NT_SUCCESS(Status))
168 {
169 *LengthRead = ToRead;
170 if(bFreeBuffer)
171 {
172 /* Copy what we've got */
173 RtlCopyMemory(Buffer, PageBuf + (ReadOffset - ActualReadOffset), ToRead);
174 }
175 /* Zero out the rest */
176 if(ToRead != Length)
177 RtlZeroMemory(Buffer + ToRead, Length - ToRead);
178 }
179
180 if(bFreeBuffer)
181 ExFreePoolWithTag(PageBuf, CDFS_TAG);
182 }
183
184 return Status;
185 }
186
187
188 NTSTATUS NTAPI
189 CdfsRead(
190 PCDFS_IRP_CONTEXT IrpContext)
191 {
192 PIRP Irp;
193 PIO_STACK_LOCATION Stack;
194 PFILE_OBJECT FileObject;
195 PVOID Buffer = NULL;
196 ULONG ReadLength;
197 LARGE_INTEGER ReadOffset;
198 ULONG ReturnedReadLength = 0;
199 NTSTATUS Status = STATUS_SUCCESS;
200
201 DPRINT("CdfsRead(%p)\n", IrpContext);
202
203 ASSERT(IrpContext);
204
205 Irp = IrpContext->Irp;
206 Stack = IrpContext->Stack;
207
208 FileObject = Stack->FileObject;
209
210 ReadLength = Stack->Parameters.Read.Length;
211 ReadOffset = Stack->Parameters.Read.ByteOffset;
212 if (ReadLength) Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
213
214 Status = CdfsReadFile(IrpContext,
215 FileObject,
216 Buffer,
217 ReadLength,
218 ReadOffset.u.LowPart,
219 Irp->Flags,
220 &ReturnedReadLength);
221 if (NT_SUCCESS(Status))
222 {
223 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
224 {
225 FileObject->CurrentByteOffset.QuadPart =
226 ReadOffset.QuadPart + ReturnedReadLength;
227 }
228 Irp->IoStatus.Information = ReturnedReadLength;
229 }
230 else
231 {
232 Irp->IoStatus.Information = 0;
233 }
234
235 return(Status);
236 }
237
238
239 NTSTATUS NTAPI
240 CdfsWrite(
241 PCDFS_IRP_CONTEXT IrpContext)
242 {
243 DPRINT("CdfsWrite(%p)\n", IrpContext);
244
245 ASSERT(IrpContext);
246
247 IrpContext->Irp->IoStatus.Information = 0;
248 return(STATUS_NOT_SUPPORTED);
249 }
250
251 /* EOF */