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.
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
28 /* INCLUDES *****************************************************************/
35 /* FUNCTIONS ****************************************************************/
37 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
38 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
41 /* FUNCTIONS ****************************************************************/
44 CdfsReadFile(PCDFS_IRP_CONTEXT IrpContext
,
45 PFILE_OBJECT FileObject
,
52 * FUNCTION: Reads data from a file
55 NTSTATUS Status
= STATUS_SUCCESS
;
56 PDEVICE_EXTENSION DeviceExt
;
58 ULONG ToRead
= Length
;
60 DPRINT("CdfsReadFile(ReadOffset %lu Length %lu)\n", ReadOffset
, Length
);
65 return(STATUS_SUCCESS
);
67 DeviceExt
= IrpContext
->DeviceObject
->DeviceExtension
;
68 Fcb
= (PFCB
)FileObject
->FsContext
;
70 if (ReadOffset
>= Fcb
->Entry
.DataLengthL
)
71 return(STATUS_END_OF_FILE
);
73 if (ReadOffset
+ Length
> Fcb
->Entry
.DataLengthL
)
74 ToRead
= Fcb
->Entry
.DataLengthL
- ReadOffset
;
76 if (!(IrpFlags
& IRP_PAGING_IO
) &&
77 FsRtlAreThereCurrentFileLocks(&Fcb
->FileLock
))
79 if (!FsRtlCheckLockForReadAccess(&Fcb
->FileLock
, IrpContext
->Irp
))
81 return STATUS_FILE_LOCK_CONFLICT
;
85 DPRINT("Reading %u bytes at %u\n", Length
, ReadOffset
);
87 if (!(IrpFlags
& (IRP_NOCACHE
|IRP_PAGING_IO
)))
89 LARGE_INTEGER FileOffset
;
90 IO_STATUS_BLOCK IoStatus
;
91 CC_FILE_SIZES FileSizes
;
93 DPRINT("Using cache\n");
95 if (FileObject
->PrivateCacheMap
== NULL
)
97 FileSizes
.AllocationSize
= Fcb
->RFCB
.AllocationSize
;
98 FileSizes
.FileSize
= Fcb
->RFCB
.FileSize
;
99 FileSizes
.ValidDataLength
= Fcb
->RFCB
.ValidDataLength
;
101 DPRINT("Attach FCB to File: Size %08x%08x\n",
102 Fcb
->RFCB
.ValidDataLength
.HighPart
,
103 Fcb
->RFCB
.ValidDataLength
.LowPart
);
107 CcInitializeCacheMap(FileObject
,
110 &(CdfsGlobalData
->CacheMgrCallbacks
),
113 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
115 return _SEH2_GetExceptionCode();
120 FileOffset
.QuadPart
= (LONGLONG
)ReadOffset
;
123 CcCopyRead(FileObject
,
130 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
132 IoStatus
.Information
= 0;
133 IoStatus
.Status
= _SEH2_GetExceptionCode();
136 *LengthRead
= IoStatus
.Information
;
138 Status
= IoStatus
.Status
;
142 ULONG ActualReadOffset
= ROUND_DOWN(ReadOffset
, BLOCKSIZE
);
143 ULONG nBlocks
= (ROUND_UP(ReadOffset
+ ToRead
, BLOCKSIZE
) - ActualReadOffset
) / BLOCKSIZE
;
145 BOOLEAN bFreeBuffer
= FALSE
;
146 if ((ReadOffset
% BLOCKSIZE
) != 0 || (ToRead
% BLOCKSIZE
) != 0)
148 PageBuf
= ExAllocatePoolWithTag(NonPagedPool
,
153 return STATUS_NO_MEMORY
;
161 Status
= CdfsReadSectors(DeviceExt
->StorageDevice
,
162 Fcb
->Entry
.ExtentLocationL
+ (ActualReadOffset
/ BLOCKSIZE
),
167 if(NT_SUCCESS(Status
))
169 *LengthRead
= ToRead
;
172 /* Copy what we've got */
173 RtlCopyMemory(Buffer
, PageBuf
+ (ReadOffset
- ActualReadOffset
), ToRead
);
175 /* Zero out the rest */
177 RtlZeroMemory(Buffer
+ ToRead
, Length
- ToRead
);
181 ExFreePoolWithTag(PageBuf
, CDFS_TAG
);
190 PCDFS_IRP_CONTEXT IrpContext
)
193 PIO_STACK_LOCATION Stack
;
194 PFILE_OBJECT FileObject
;
197 LARGE_INTEGER ReadOffset
;
198 ULONG ReturnedReadLength
= 0;
199 NTSTATUS Status
= STATUS_SUCCESS
;
201 DPRINT("CdfsRead(%p)\n", IrpContext
);
205 Irp
= IrpContext
->Irp
;
206 Stack
= IrpContext
->Stack
;
208 FileObject
= Stack
->FileObject
;
210 ReadLength
= Stack
->Parameters
.Read
.Length
;
211 ReadOffset
= Stack
->Parameters
.Read
.ByteOffset
;
212 if (ReadLength
) Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
214 Status
= CdfsReadFile(IrpContext
,
218 ReadOffset
.u
.LowPart
,
220 &ReturnedReadLength
);
221 if (NT_SUCCESS(Status
))
223 if (FileObject
->Flags
& FO_SYNCHRONOUS_IO
)
225 FileObject
->CurrentByteOffset
.QuadPart
=
226 ReadOffset
.QuadPart
+ ReturnedReadLength
;
228 Irp
->IoStatus
.Information
= ReturnedReadLength
;
232 Irp
->IoStatus
.Information
= 0;
241 PCDFS_IRP_CONTEXT IrpContext
)
243 DPRINT("CdfsWrite(%p)\n", IrpContext
);
247 IrpContext
->Irp
->IoStatus
.Information
= 0;
248 return(STATUS_NOT_SUPPORTED
);