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
;
60 DPRINT("CdfsReadFile(ReadOffset %lu Length %lu)\n", ReadOffset
, Length
);
65 return(STATUS_SUCCESS
);
67 Ccb
= (PCCB
)FileObject
->FsContext2
;
68 Fcb
= (PFCB
)FileObject
->FsContext
;
70 if (ReadOffset
>= Fcb
->Entry
.DataLengthL
)
71 return(STATUS_END_OF_FILE
);
73 DPRINT("Reading %d bytes at %d\n", Length
, ReadOffset
);
75 if (!(IrpFlags
& (IRP_NOCACHE
|IRP_PAGING_IO
)))
77 LARGE_INTEGER FileOffset
;
78 IO_STATUS_BLOCK IoStatus
;
79 CC_FILE_SIZES FileSizes
;
81 if (ReadOffset
+ Length
> Fcb
->Entry
.DataLengthL
)
82 Length
= Fcb
->Entry
.DataLengthL
- ReadOffset
;
84 if (FileObject
->PrivateCacheMap
== NULL
)
86 FileSizes
.AllocationSize
= Fcb
->RFCB
.AllocationSize
;
87 FileSizes
.FileSize
= Fcb
->RFCB
.FileSize
;
88 FileSizes
.ValidDataLength
= Fcb
->RFCB
.ValidDataLength
;
90 DPRINT("Attach FCB to File: Size %08x%08x\n",
91 Fcb
->RFCB
.ValidDataLength
.HighPart
,
92 Fcb
->RFCB
.ValidDataLength
.LowPart
);
94 CcInitializeCacheMap(FileObject
,
97 &(CdfsGlobalData
->CacheMgrCallbacks
),
101 FileOffset
.QuadPart
= (LONGLONG
)ReadOffset
;
102 CcCopyRead(FileObject
,
108 *LengthRead
= IoStatus
.Information
;
110 return(IoStatus
.Status
);
113 if ((ReadOffset
% BLOCKSIZE
) != 0 || (Length
% BLOCKSIZE
) != 0)
115 /* Then we need to do a partial or misaligned read ... */
116 PVOID PageBuf
= ExAllocatePool(NonPagedPool
, BLOCKSIZE
);
117 PCHAR ReadInPage
= (PCHAR
)PageBuf
+ (ReadOffset
& (BLOCKSIZE
- 1));
118 PCHAR TargetRead
= (PCHAR
)Buffer
;
119 ULONG ActualReadOffset
, EndOfExtent
, ReadLen
;
123 return STATUS_NO_MEMORY
;
126 ActualReadOffset
= ReadOffset
& ~(BLOCKSIZE
- 1);
127 EndOfExtent
= ReadOffset
+ Length
;
129 while (ActualReadOffset
< EndOfExtent
)
131 Status
= CdfsReadSectors
132 (DeviceExt
->StorageDevice
,
133 Fcb
->Entry
.ExtentLocationL
+ (ActualReadOffset
/ BLOCKSIZE
),
138 if (!NT_SUCCESS(Status
))
141 ReadLen
= BLOCKSIZE
- (ActualReadOffset
& (BLOCKSIZE
- 1));
142 if (ReadLen
> EndOfExtent
- ActualReadOffset
)
144 ReadLen
= EndOfExtent
- ActualReadOffset
;
147 RtlCopyMemory(TargetRead
, ReadInPage
, ReadLen
);
149 ActualReadOffset
+= ReadLen
;
150 TargetRead
+= ReadLen
;
157 if (ReadOffset
+ Length
> ROUND_UP(Fcb
->Entry
.DataLengthL
, BLOCKSIZE
))
158 Length
= ROUND_UP(Fcb
->Entry
.DataLengthL
, BLOCKSIZE
) - ReadOffset
;
160 Status
= CdfsReadSectors(DeviceExt
->StorageDevice
,
161 Fcb
->Entry
.ExtentLocationL
+ (ReadOffset
/ BLOCKSIZE
),
165 if (NT_SUCCESS(Status
))
167 *LengthRead
= Length
;
168 if (Length
+ ReadOffset
> Fcb
->Entry
.DataLengthL
)
170 memset(Buffer
+ Fcb
->Entry
.DataLengthL
- ReadOffset
,
172 Length
+ ReadOffset
- Fcb
->Entry
.DataLengthL
);
182 CdfsRead(PDEVICE_OBJECT DeviceObject
,
185 PDEVICE_EXTENSION DeviceExt
;
186 PIO_STACK_LOCATION Stack
;
187 PFILE_OBJECT FileObject
;
190 LARGE_INTEGER ReadOffset
;
191 ULONG ReturnedReadLength
= 0;
192 NTSTATUS Status
= STATUS_SUCCESS
;
194 DPRINT("CdfsRead(DeviceObject %x, Irp %x)\n",DeviceObject
,Irp
);
196 DeviceExt
= DeviceObject
->DeviceExtension
;
197 Stack
= IoGetCurrentIrpStackLocation(Irp
);
198 FileObject
= Stack
->FileObject
;
200 ReadLength
= Stack
->Parameters
.Read
.Length
;
201 ReadOffset
= Stack
->Parameters
.Read
.ByteOffset
;
202 if (ReadLength
) Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
204 Status
= CdfsReadFile(DeviceExt
,
208 ReadOffset
.u
.LowPart
,
210 &ReturnedReadLength
);
211 if (NT_SUCCESS(Status
))
213 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
)
215 FileObject
->CurrentByteOffset
.QuadPart
=
216 ReadOffset
.QuadPart
+ ReturnedReadLength
;
218 Irp
->IoStatus
.Information
= ReturnedReadLength
;
222 Irp
->IoStatus
.Information
= 0;
225 Irp
->IoStatus
.Status
= Status
;
226 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);
233 CdfsWrite(PDEVICE_OBJECT DeviceObject
,
236 DPRINT("CdfsWrite(DeviceObject %x Irp %x)\n",DeviceObject
,Irp
);
238 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
239 Irp
->IoStatus
.Information
= 0;
240 return(STATUS_NOT_SUPPORTED
);