1 /* $Id: rw.c,v 1.6 2000/06/17 22:04:08 phreak Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: services/fs/vfat/rw.c
6 * PURPOSE: VFAT Filesystem
7 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
11 /* INCLUDES *****************************************************************/
13 #include <ddk/ntddk.h>
15 #include <internal/string.h>
16 #include <ddk/cctypes.h>
19 #include <internal/debug.h>
23 /* FUNCTIONS ****************************************************************/
25 NTSTATUS
FsdReadFile(PDEVICE_EXTENSION DeviceExt
, PFILE_OBJECT FileObject
,
26 PVOID Buffer
, ULONG Length
, ULONG ReadOffset
,
29 * FUNCTION: Reads data from a file
40 assert(DeviceExt
!= NULL
);
41 assert(DeviceExt
->BytesPerCluster
!= 0);
42 assert(FileObject
!= NULL
);
43 assert(FileObject
->FsContext
!= NULL
);
45 DPRINT("FsdReadFile(DeviceExt %x, FileObject %x, Buffer %x, "
46 "Length %d, ReadOffset %d)\n",DeviceExt
,FileObject
,Buffer
,
49 Fcb
= ((PVFATCCB
)(FileObject
->FsContext2
))->pFcb
;
50 if (DeviceExt
->FatType
== FAT32
)
51 CurrentCluster
= Fcb
->entry
.FirstCluster
52 +Fcb
->entry
.FirstClusterHigh
*65536;
54 CurrentCluster
= Fcb
->entry
.FirstCluster
;
55 FirstCluster
=CurrentCluster
;
56 DPRINT("DeviceExt->BytesPerCluster %x\n",DeviceExt
->BytesPerCluster
);
58 if (ReadOffset
>= Fcb
->entry
.FileSize
)
60 return(STATUS_END_OF_FILE
);
62 if ((ReadOffset
+ Length
) > Fcb
->entry
.FileSize
)
64 Length
= Fcb
->entry
.FileSize
- ReadOffset
;
67 /* FIXME: optimize by remembering the last cluster read and using if possible */
68 Temp
= ExAllocatePool(NonPagedPool
,DeviceExt
->BytesPerCluster
);
69 if(!Temp
) return STATUS_UNSUCCESSFUL
;
71 { //root of FAT16 or FAT12
72 CurrentCluster
=DeviceExt
->rootStart
+ReadOffset
73 /(DeviceExt
->BytesPerCluster
)*DeviceExt
->Boot
->SectorsPerCluster
;
76 for (FileOffset
=0; FileOffset
< ReadOffset
/ DeviceExt
->BytesPerCluster
79 CurrentCluster
= GetNextCluster(DeviceExt
,CurrentCluster
);
82 if ((ReadOffset
% DeviceExt
->BytesPerCluster
)!=0)
84 if (FirstCluster
== 1)
86 VFATReadSectors(DeviceExt
->StorageDevice
,
88 DeviceExt
->Boot
->SectorsPerCluster
,
90 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
94 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
95 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
97 TempLength
= min(Length
,DeviceExt
->BytesPerCluster
-
98 (ReadOffset
% DeviceExt
->BytesPerCluster
));
100 memcpy(Buffer
, Temp
+ ReadOffset
% DeviceExt
->BytesPerCluster
,
103 (*LengthRead
) = (*LengthRead
) + TempLength
;
104 Length
= Length
- TempLength
;
105 Buffer
= Buffer
+ TempLength
;
108 while (Length
>= DeviceExt
->BytesPerCluster
)
110 if (FirstCluster
== 1)
112 VFATReadSectors(DeviceExt
->StorageDevice
,
114 DeviceExt
->Boot
->SectorsPerCluster
,
116 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
120 VFATLoadCluster(DeviceExt
,Buffer
,CurrentCluster
);
121 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
123 if (CurrentCluster
== 0xffffffff)
126 return(STATUS_SUCCESS
);
129 (*LengthRead
) = (*LengthRead
) + DeviceExt
->BytesPerCluster
;
130 Buffer
= Buffer
+ DeviceExt
->BytesPerCluster
;
131 Length
= Length
- DeviceExt
->BytesPerCluster
;
136 (*LengthRead
) = (*LengthRead
) + Length
;
137 if (FirstCluster
== 1)
139 VFATReadSectors(DeviceExt
->StorageDevice
,
141 DeviceExt
->Boot
->SectorsPerCluster
,
143 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
147 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
148 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
150 memcpy(Buffer
, Temp
, Length
);
153 return(STATUS_SUCCESS
);
156 NTSTATUS
FsdWriteFile(PDEVICE_EXTENSION DeviceExt
, PFILE_OBJECT FileObject
,
157 PVOID Buffer
, ULONG Length
, ULONG WriteOffset
)
159 * FUNCTION: Writes data to file
162 ULONG CurrentCluster
;
168 ULONG TempLength
,Length2
=Length
;
169 LARGE_INTEGER SystemTime
, LocalTime
;
171 DPRINT1("FsdWriteFile(FileObject %x, Buffer %x, Length %x, "
172 "WriteOffset %x\n", FileObject
, Buffer
, Length
, WriteOffset
);
174 /* Locate the first cluster of the file */
176 pCcb
=(PVFATCCB
)(FileObject
->FsContext2
);
180 if (DeviceExt
->FatType
== FAT32
)
183 Fcb
->entry
.FirstCluster
+Fcb
->entry
.FirstClusterHigh
*65536;
187 CurrentCluster
= Fcb
->entry
.FirstCluster
;
189 FirstCluster
=CurrentCluster
;
191 /* Allocate a buffer to hold 1 cluster of data */
192 Temp
= ExAllocatePool(NonPagedPool
, DeviceExt
->BytesPerCluster
);
195 /* Find the cluster according to the offset in the file */
196 if (CurrentCluster
==1)
198 CurrentCluster
=DeviceExt
->rootStart
+WriteOffset
199 / DeviceExt
->BytesPerCluster
*DeviceExt
->Boot
->SectorsPerCluster
;
203 if (CurrentCluster
==0)
208 CurrentCluster
=GetNextWriteCluster(DeviceExt
,0);
209 if (DeviceExt
->FatType
== FAT32
)
211 Fcb
->entry
.FirstClusterHigh
= CurrentCluster
>>16;
212 Fcb
->entry
.FirstCluster
= CurrentCluster
;
215 Fcb
->entry
.FirstCluster
=CurrentCluster
;
220 FileOffset
< WriteOffset
/ DeviceExt
->BytesPerCluster
;
223 CurrentCluster
= GetNextCluster(DeviceExt
,CurrentCluster
);
230 * If the offset in the cluster doesn't fall on the cluster boundary
231 * then we have to write only from the specified offset
234 if ((WriteOffset
% DeviceExt
->BytesPerCluster
)!=0)
237 TempLength
= min(Length
,DeviceExt
->BytesPerCluster
-
238 (WriteOffset
% DeviceExt
->BytesPerCluster
));
239 /* Read in the existing cluster data */
242 VFATReadSectors(DeviceExt
->StorageDevice
,
244 DeviceExt
->Boot
->SectorsPerCluster
,
249 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
252 /* Overwrite the last parts of the data as necessary */
253 memcpy(Temp
+ (WriteOffset
% DeviceExt
->BytesPerCluster
),
257 /* Write the cluster back */
260 VFATWriteSectors(DeviceExt
->StorageDevice
,
262 DeviceExt
->Boot
->SectorsPerCluster
,
264 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
268 VFATWriteCluster(DeviceExt
,Temp
,CurrentCluster
);
269 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
271 Length2
-= TempLength
;
272 Buffer
= Buffer
+ TempLength
;
276 /* Write the buffer in chunks of 1 cluster */
278 while (Length2
>= DeviceExt
->BytesPerCluster
)
281 if (CurrentCluster
== 0)
284 return(STATUS_UNSUCCESSFUL
);
288 VFATWriteSectors(DeviceExt
->StorageDevice
,
290 DeviceExt
->Boot
->SectorsPerCluster
,
292 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
296 VFATWriteCluster(DeviceExt
,Buffer
,CurrentCluster
);
297 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
299 Buffer
= Buffer
+ DeviceExt
->BytesPerCluster
;
300 Length2
-= DeviceExt
->BytesPerCluster
;
304 /* Write the remainder */
309 if (CurrentCluster
== 0)
312 return(STATUS_UNSUCCESSFUL
);
315 /* Read in the existing cluster data */
318 VFATReadSectors(DeviceExt
->StorageDevice
,
320 DeviceExt
->Boot
->SectorsPerCluster
,
325 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
327 memcpy(Temp
, Buffer
, Length2
);
331 VFATWriteSectors(DeviceExt
->StorageDevice
,
333 DeviceExt
->Boot
->SectorsPerCluster
,
338 VFATWriteCluster(DeviceExt
,Temp
,CurrentCluster
);
345 /* set dates and times */
346 KeQuerySystemTime (&SystemTime
);
347 ExSystemTimeToLocalTime (&SystemTime
,
349 FsdFileTimeToDosDateTime ((TIME
*)&LocalTime
,
350 &Fcb
->entry
.UpdateDate
,
351 &Fcb
->entry
.UpdateTime
);
352 Fcb
->entry
.AccessDate
= Fcb
->entry
.UpdateDate
;
354 if (Fcb
->entry
.FileSize
< WriteOffset
+Length
)
356 Fcb
->entry
.FileSize
= WriteOffset
+Length
;
358 * update entry in directory
360 updEntry(DeviceExt
,FileObject
);
364 return (STATUS_SUCCESS
);
367 NTSTATUS
FsdWrite(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
369 * FUNCTION: Write to a file
375 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
376 PFILE_OBJECT FileObject
= Stack
->FileObject
;
377 PDEVICE_EXTENSION DeviceExt
= DeviceObject
->DeviceExtension
;
380 DPRINT("FsdWrite(DeviceObject %x Irp %x)\n",DeviceObject
,Irp
);
382 Length
= Stack
->Parameters
.Write
.Length
;
383 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
384 Offset
= Stack
->Parameters
.Write
.ByteOffset
.u
.LowPart
;
386 Status
= FsdWriteFile(DeviceExt
,FileObject
,Buffer
,Length
,Offset
);
388 Irp
->IoStatus
.Status
= Status
;
389 Irp
->IoStatus
.Information
= Length
;
390 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);
395 NTSTATUS
FsdRead(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
397 * FUNCTION: Read from a file
403 PIO_STACK_LOCATION Stack
;
404 PFILE_OBJECT FileObject
;
405 PDEVICE_EXTENSION DeviceExt
;
410 DPRINT("FsdRead(DeviceObject %x, Irp %x)\n",DeviceObject
,Irp
);
412 /* Precondition / Initialization */
414 Stack
= IoGetCurrentIrpStackLocation(Irp
);
415 assert(Stack
!= NULL
);
416 FileObject
= Stack
->FileObject
;
417 assert(FileObject
!= NULL
);
418 DeviceExt
= DeviceObject
->DeviceExtension
;
419 assert(DeviceExt
!= NULL
);
421 Length
= Stack
->Parameters
.Read
.Length
;
422 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
423 Offset
= Stack
->Parameters
.Read
.ByteOffset
.u
.LowPart
;
425 /* fail if file is a directory */
426 Fcb
= ((PVFATCCB
)(FileObject
->FsContext2
))->pFcb
;
427 if (Fcb
->entry
.Attrib
& FILE_ATTRIBUTE_DIRECTORY
)
429 Status
= STATUS_FILE_IS_A_DIRECTORY
;
433 Status
= FsdReadFile(DeviceExt
,
441 Irp
->IoStatus
.Status
= Status
;
442 Irp
->IoStatus
.Information
= LengthRead
;
443 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);