3 * Copyright (C) 2002, 2003 ReactOS Team
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.
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.
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.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: drivers/fs/cdfs/rw.c
24 * PURPOSE: CDROM (ISO 9660) filesystem driver
25 * PROGRAMMER: Art Yerkes
29 /* INCLUDES *****************************************************************/
36 /* FUNCTIONS ****************************************************************/
38 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
39 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
42 /* FUNCTIONS ****************************************************************/
45 CdfsReadFile(PDEVICE_EXTENSION DeviceExt
,
46 PFILE_OBJECT FileObject
,
53 * FUNCTION: Reads data from a file
56 NTSTATUS Status
= STATUS_SUCCESS
;
59 DPRINT("CdfsReadFile(ReadOffset %lu Length %lu)\n", ReadOffset
, Length
);
64 return(STATUS_SUCCESS
);
66 Fcb
= (PFCB
)FileObject
->FsContext
;
68 if (ReadOffset
>= Fcb
->Entry
.DataLengthL
)
69 return(STATUS_END_OF_FILE
);
71 DPRINT("Reading %d bytes at %d\n", Length
, ReadOffset
);
73 if (!(IrpFlags
& (IRP_NOCACHE
|IRP_PAGING_IO
)))
75 LARGE_INTEGER FileOffset
;
76 IO_STATUS_BLOCK IoStatus
;
77 CC_FILE_SIZES FileSizes
;
79 if (ReadOffset
+ Length
> Fcb
->Entry
.DataLengthL
)
80 Length
= Fcb
->Entry
.DataLengthL
- ReadOffset
;
82 if (FileObject
->PrivateCacheMap
== NULL
)
84 FileSizes
.AllocationSize
= Fcb
->RFCB
.AllocationSize
;
85 FileSizes
.FileSize
= Fcb
->RFCB
.FileSize
;
86 FileSizes
.ValidDataLength
= Fcb
->RFCB
.ValidDataLength
;
88 DPRINT("Attach FCB to File: Size %08x%08x\n",
89 Fcb
->RFCB
.ValidDataLength
.HighPart
,
90 Fcb
->RFCB
.ValidDataLength
.LowPart
);
92 CcInitializeCacheMap(FileObject
,
95 &(CdfsGlobalData
->CacheMgrCallbacks
),
99 FileOffset
.QuadPart
= (LONGLONG
)ReadOffset
;
100 CcCopyRead(FileObject
,
106 *LengthRead
= IoStatus
.Information
;
108 return(IoStatus
.Status
);
111 if ((ReadOffset
% BLOCKSIZE
) != 0 || (Length
% BLOCKSIZE
) != 0)
113 /* Then we need to do a partial or misaligned read ... */
114 PVOID PageBuf
= ExAllocatePool(NonPagedPool
, BLOCKSIZE
);
115 PCHAR ReadInPage
= (PCHAR
)PageBuf
+ (ReadOffset
& (BLOCKSIZE
- 1));
116 PCHAR TargetRead
= (PCHAR
)Buffer
;
117 ULONG ActualReadOffset
, EndOfExtent
, ReadLen
;
121 return STATUS_NO_MEMORY
;
124 ActualReadOffset
= ReadOffset
& ~(BLOCKSIZE
- 1);
125 EndOfExtent
= ReadOffset
+ Length
;
127 while (ActualReadOffset
< EndOfExtent
)
129 Status
= CdfsReadSectors
130 (DeviceExt
->StorageDevice
,
131 Fcb
->Entry
.ExtentLocationL
+ (ActualReadOffset
/ BLOCKSIZE
),
136 if (!NT_SUCCESS(Status
))
139 ReadLen
= BLOCKSIZE
- (ActualReadOffset
& (BLOCKSIZE
- 1));
140 if (ReadLen
> EndOfExtent
- ActualReadOffset
)
142 ReadLen
= EndOfExtent
- ActualReadOffset
;
145 RtlCopyMemory(TargetRead
, ReadInPage
, ReadLen
);
147 ActualReadOffset
+= ReadLen
;
148 TargetRead
+= ReadLen
;
155 if (ReadOffset
+ Length
> ROUND_UP(Fcb
->Entry
.DataLengthL
, BLOCKSIZE
))
156 Length
= ROUND_UP(Fcb
->Entry
.DataLengthL
, BLOCKSIZE
) - ReadOffset
;
158 Status
= CdfsReadSectors(DeviceExt
->StorageDevice
,
159 Fcb
->Entry
.ExtentLocationL
+ (ReadOffset
/ BLOCKSIZE
),
163 if (NT_SUCCESS(Status
))
165 *LengthRead
= Length
;
166 if (Length
+ ReadOffset
> Fcb
->Entry
.DataLengthL
)
168 memset(Buffer
+ Fcb
->Entry
.DataLengthL
- ReadOffset
,
170 Length
+ ReadOffset
- Fcb
->Entry
.DataLengthL
);
180 CdfsRead(PDEVICE_OBJECT DeviceObject
,
183 PDEVICE_EXTENSION DeviceExt
;
184 PIO_STACK_LOCATION Stack
;
185 PFILE_OBJECT FileObject
;
188 LARGE_INTEGER ReadOffset
;
189 ULONG ReturnedReadLength
= 0;
190 NTSTATUS Status
= STATUS_SUCCESS
;
192 DPRINT("CdfsRead(DeviceObject %x, Irp %x)\n",DeviceObject
,Irp
);
194 DeviceExt
= DeviceObject
->DeviceExtension
;
195 Stack
= IoGetCurrentIrpStackLocation(Irp
);
196 FileObject
= Stack
->FileObject
;
198 ReadLength
= Stack
->Parameters
.Read
.Length
;
199 ReadOffset
= Stack
->Parameters
.Read
.ByteOffset
;
200 if (ReadLength
) Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
202 Status
= CdfsReadFile(DeviceExt
,
206 ReadOffset
.u
.LowPart
,
208 &ReturnedReadLength
);
209 if (NT_SUCCESS(Status
))
211 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
)
213 FileObject
->CurrentByteOffset
.QuadPart
=
214 ReadOffset
.QuadPart
+ ReturnedReadLength
;
216 Irp
->IoStatus
.Information
= ReturnedReadLength
;
220 Irp
->IoStatus
.Information
= 0;
223 Irp
->IoStatus
.Status
= Status
;
224 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);
231 CdfsWrite(PDEVICE_OBJECT DeviceObject
,
234 DPRINT("CdfsWrite(DeviceObject %x Irp %x)\n",DeviceObject
,Irp
);
236 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
237 Irp
->IoStatus
.Information
= 0;
238 return(STATUS_NOT_SUPPORTED
);