1 /* $Id: rw.c,v 1.10 2000/12/01 12:41:08 jean 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 <ddk/cctypes.h>
16 #include <ntos/minmax.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
= GetNextWriteCluster(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 */
258 Length2
-= TempLength
;
261 VFATWriteSectors(DeviceExt
->StorageDevice
,
263 DeviceExt
->Boot
->SectorsPerCluster
,
265 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
269 VFATWriteCluster(DeviceExt
,Temp
,CurrentCluster
);
271 CurrentCluster
= GetNextWriteCluster(DeviceExt
, CurrentCluster
);
273 Buffer
= Buffer
+ TempLength
;
277 /* Write the buffer in chunks of 1 cluster */
279 while (Length2
>= DeviceExt
->BytesPerCluster
)
282 if (CurrentCluster
== 0)
285 return(STATUS_UNSUCCESSFUL
);
287 Length2
-= DeviceExt
->BytesPerCluster
;
290 VFATWriteSectors(DeviceExt
->StorageDevice
,
292 DeviceExt
->Boot
->SectorsPerCluster
,
294 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
298 VFATWriteCluster(DeviceExt
,Buffer
,CurrentCluster
);
300 CurrentCluster
= GetNextWriteCluster(DeviceExt
, CurrentCluster
);
302 Buffer
= Buffer
+ DeviceExt
->BytesPerCluster
;
306 /* Write the remainder */
311 if (CurrentCluster
== 0)
314 return(STATUS_UNSUCCESSFUL
);
317 /* Read in the existing cluster data */
320 VFATReadSectors(DeviceExt
->StorageDevice
,
322 DeviceExt
->Boot
->SectorsPerCluster
,
327 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
329 memcpy(Temp
, Buffer
, Length2
);
333 VFATWriteSectors(DeviceExt
->StorageDevice
,
335 DeviceExt
->Boot
->SectorsPerCluster
,
340 VFATWriteCluster(DeviceExt
,Temp
,CurrentCluster
);
347 /* set dates and times */
348 KeQuerySystemTime (&SystemTime
);
349 ExSystemTimeToLocalTime (&SystemTime
,
351 FsdFileTimeToDosDateTime ((TIME
*)&LocalTime
,
352 &Fcb
->entry
.UpdateDate
,
353 &Fcb
->entry
.UpdateTime
);
354 Fcb
->entry
.AccessDate
= Fcb
->entry
.UpdateDate
;
356 if (Fcb
->entry
.FileSize
< WriteOffset
+Length
)
358 Fcb
->entry
.FileSize
= WriteOffset
+Length
;
360 * update entry in directory
362 updEntry(DeviceExt
,FileObject
);
366 return (STATUS_SUCCESS
);
369 NTSTATUS STDCALL
FsdWrite(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
371 * FUNCTION: Write to a file
377 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
378 PFILE_OBJECT FileObject
= Stack
->FileObject
;
379 PDEVICE_EXTENSION DeviceExt
= DeviceObject
->DeviceExtension
;
382 DPRINT("FsdWrite(DeviceObject %x Irp %x)\n",DeviceObject
,Irp
);
384 Length
= Stack
->Parameters
.Write
.Length
;
385 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
386 Offset
= Stack
->Parameters
.Write
.ByteOffset
.u
.LowPart
;
388 Status
= FsdWriteFile(DeviceExt
,FileObject
,Buffer
,Length
,Offset
);
390 Irp
->IoStatus
.Status
= Status
;
391 Irp
->IoStatus
.Information
= Length
;
392 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);
397 NTSTATUS STDCALL
FsdRead(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
399 * FUNCTION: Read from a file
405 PIO_STACK_LOCATION Stack
;
406 PFILE_OBJECT FileObject
;
407 PDEVICE_EXTENSION DeviceExt
;
412 DPRINT("FsdRead(DeviceObject %x, Irp %x)\n",DeviceObject
,Irp
);
414 /* Precondition / Initialization */
416 Stack
= IoGetCurrentIrpStackLocation(Irp
);
417 assert(Stack
!= NULL
);
418 FileObject
= Stack
->FileObject
;
419 assert(FileObject
!= NULL
);
420 DeviceExt
= DeviceObject
->DeviceExtension
;
421 assert(DeviceExt
!= NULL
);
423 Length
= Stack
->Parameters
.Read
.Length
;
424 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
425 Offset
= Stack
->Parameters
.Read
.ByteOffset
.u
.LowPart
;
427 /* fail if file is a directory */
428 Fcb
= ((PVFATCCB
)(FileObject
->FsContext2
))->pFcb
;
429 if (Fcb
->entry
.Attrib
& FILE_ATTRIBUTE_DIRECTORY
)
431 Status
= STATUS_FILE_IS_A_DIRECTORY
;
435 Status
= FsdReadFile(DeviceExt
,
443 Irp
->IoStatus
.Status
= Status
;
444 Irp
->IoStatus
.Information
= LengthRead
;
445 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);