1 /* $Id: rw.c,v 1.4 2000/03/12 23:28:59 ekohl 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
59 && !(Fcb
->entry
.Attrib
& FILE_ATTRIBUTE_DIRECTORY
))
61 return(STATUS_END_OF_FILE
);
63 if ((ReadOffset
+ Length
) > Fcb
->entry
.FileSize
64 && !(Fcb
->entry
.Attrib
& FILE_ATTRIBUTE_DIRECTORY
))
66 Length
= Fcb
->entry
.FileSize
- ReadOffset
;
69 /* FIXME: optimize by remembering the last cluster read and using if possible */
70 Temp
= ExAllocatePool(NonPagedPool
,DeviceExt
->BytesPerCluster
);
71 if(!Temp
) return STATUS_UNSUCCESSFUL
;
73 { //root of FAT16 or FAT12
74 CurrentCluster
=DeviceExt
->rootStart
+ReadOffset
75 /(DeviceExt
->BytesPerCluster
)*DeviceExt
->Boot
->SectorsPerCluster
;
78 for (FileOffset
=0; FileOffset
< ReadOffset
/ DeviceExt
->BytesPerCluster
81 CurrentCluster
= GetNextCluster(DeviceExt
,CurrentCluster
);
84 if ((ReadOffset
% DeviceExt
->BytesPerCluster
)!=0)
88 VFATReadSectors(DeviceExt
->StorageDevice
,CurrentCluster
89 ,DeviceExt
->Boot
->SectorsPerCluster
,Temp
);
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
)
112 VFATReadSectors(DeviceExt
->StorageDevice
,CurrentCluster
113 ,DeviceExt
->Boot
->SectorsPerCluster
,Buffer
);
114 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
118 VFATLoadCluster(DeviceExt
,Buffer
,CurrentCluster
);
119 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
121 if (CurrentCluster
== 0xffffffff)
124 return(STATUS_SUCCESS
);
127 (*LengthRead
) = (*LengthRead
) + DeviceExt
->BytesPerCluster
;
128 Buffer
= Buffer
+ DeviceExt
->BytesPerCluster
;
129 Length
= Length
- DeviceExt
->BytesPerCluster
;
134 (*LengthRead
) = (*LengthRead
) + Length
;
137 VFATReadSectors(DeviceExt
->StorageDevice
,CurrentCluster
138 ,DeviceExt
->Boot
->SectorsPerCluster
,Temp
);
139 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
143 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
144 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
146 memcpy(Buffer
, Temp
, Length
);
149 return(STATUS_SUCCESS
);
152 NTSTATUS
FsdWriteFile(PDEVICE_EXTENSION DeviceExt
, PFILE_OBJECT FileObject
,
153 PVOID Buffer
, ULONG Length
, ULONG WriteOffset
)
155 * FUNCTION: Writes data to file
158 ULONG CurrentCluster
;
164 ULONG TempLength
,Length2
=Length
;
165 LARGE_INTEGER SystemTime
, LocalTime
;
167 DPRINT1("FsdWriteFile(FileObject %x, Buffer %x, Length %x, "
168 "WriteOffset %x\n", FileObject
, Buffer
, Length
, WriteOffset
);
170 /* Locate the first cluster of the file */
172 pCcb
=(PVFATCCB
)(FileObject
->FsContext2
);
176 if (DeviceExt
->FatType
== FAT32
)
179 Fcb
->entry
.FirstCluster
+Fcb
->entry
.FirstClusterHigh
*65536;
183 CurrentCluster
= Fcb
->entry
.FirstCluster
;
185 FirstCluster
=CurrentCluster
;
187 /* Allocate a buffer to hold 1 cluster of data */
188 Temp
= ExAllocatePool(NonPagedPool
, DeviceExt
->BytesPerCluster
);
191 /* Find the cluster according to the offset in the file */
192 if (CurrentCluster
==1)
194 CurrentCluster
=DeviceExt
->rootStart
+WriteOffset
195 / DeviceExt
->BytesPerCluster
*DeviceExt
->Boot
->SectorsPerCluster
;
199 if (CurrentCluster
==0)
204 CurrentCluster
=GetNextWriteCluster(DeviceExt
,0);
205 if (DeviceExt
->FatType
== FAT32
)
207 Fcb
->entry
.FirstClusterHigh
= CurrentCluster
>>16;
208 Fcb
->entry
.FirstCluster
= CurrentCluster
;
211 Fcb
->entry
.FirstCluster
=CurrentCluster
;
216 FileOffset
< WriteOffset
/ DeviceExt
->BytesPerCluster
;
219 CurrentCluster
= GetNextCluster(DeviceExt
,CurrentCluster
);
226 * If the offset in the cluster doesn't fall on the cluster boundary
227 * then we have to write only from the specified offset
230 if ((WriteOffset
% DeviceExt
->BytesPerCluster
)!=0)
233 TempLength
= min(Length
,DeviceExt
->BytesPerCluster
-
234 (WriteOffset
% DeviceExt
->BytesPerCluster
));
235 /* Read in the existing cluster data */
238 VFATReadSectors(DeviceExt
->StorageDevice
,
240 DeviceExt
->Boot
->SectorsPerCluster
,
245 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
248 /* Overwrite the last parts of the data as necessary */
249 memcpy(Temp
+ (WriteOffset
% DeviceExt
->BytesPerCluster
),
253 /* Write the cluster back */
256 VFATWriteSectors(DeviceExt
->StorageDevice
,
258 DeviceExt
->Boot
->SectorsPerCluster
,
260 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
264 VFATWriteCluster(DeviceExt
,Temp
,CurrentCluster
);
265 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
267 Length2
-= TempLength
;
268 Buffer
= Buffer
+ TempLength
;
272 /* Write the buffer in chunks of 1 cluster */
274 while (Length2
>= DeviceExt
->BytesPerCluster
)
277 if (CurrentCluster
== 0)
280 return(STATUS_UNSUCCESSFUL
);
284 VFATWriteSectors(DeviceExt
->StorageDevice
,
286 DeviceExt
->Boot
->SectorsPerCluster
,
288 CurrentCluster
+= DeviceExt
->Boot
->SectorsPerCluster
;
292 VFATWriteCluster(DeviceExt
,Buffer
,CurrentCluster
);
293 CurrentCluster
= GetNextCluster(DeviceExt
, CurrentCluster
);
295 Buffer
= Buffer
+ DeviceExt
->BytesPerCluster
;
296 Length2
-= DeviceExt
->BytesPerCluster
;
300 /* Write the remainder */
305 if (CurrentCluster
== 0)
308 return(STATUS_UNSUCCESSFUL
);
311 /* Read in the existing cluster data */
314 VFATReadSectors(DeviceExt
->StorageDevice
,
316 DeviceExt
->Boot
->SectorsPerCluster
,
321 VFATLoadCluster(DeviceExt
,Temp
,CurrentCluster
);
323 memcpy(Temp
, Buffer
, Length2
);
327 VFATWriteSectors(DeviceExt
->StorageDevice
,
329 DeviceExt
->Boot
->SectorsPerCluster
,
334 VFATWriteCluster(DeviceExt
,Temp
,CurrentCluster
);
341 /* set dates and times */
342 KeQuerySystemTime (&SystemTime
);
343 ExSystemTimeToLocalTime (&SystemTime
,
345 FsdFileTimeToDosDateTime ((TIME
*)&LocalTime
,
346 &Fcb
->entry
.UpdateDate
,
347 &Fcb
->entry
.UpdateTime
);
348 Fcb
->entry
.AccessDate
= Fcb
->entry
.UpdateDate
;
350 if (Fcb
->entry
.FileSize
< WriteOffset
+Length
351 && !(Fcb
->entry
.Attrib
& FILE_ATTRIBUTE_DIRECTORY
))
353 Fcb
->entry
.FileSize
= WriteOffset
+Length
;
355 * update entry in directory
357 updEntry(DeviceExt
,FileObject
);
361 return (STATUS_SUCCESS
);
364 NTSTATUS
FsdWrite(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
366 * FUNCTION: Write to a file
372 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
373 PFILE_OBJECT FileObject
= Stack
->FileObject
;
374 PDEVICE_EXTENSION DeviceExt
= DeviceObject
->DeviceExtension
;
377 DPRINT("FsdWrite(DeviceObject %x Irp %x)\n",DeviceObject
,Irp
);
379 Length
= Stack
->Parameters
.Write
.Length
;
380 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
381 Offset
= Stack
->Parameters
.Write
.ByteOffset
.u
.LowPart
;
383 Status
= FsdWriteFile(DeviceExt
,FileObject
,Buffer
,Length
,Offset
);
385 Irp
->IoStatus
.Status
= Status
;
386 Irp
->IoStatus
.Information
= Length
;
387 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);
392 NTSTATUS
FsdRead(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
394 * FUNCTION: Read from a file
400 PIO_STACK_LOCATION Stack
;
401 PFILE_OBJECT FileObject
;
402 PDEVICE_EXTENSION DeviceExt
;
407 DPRINT("FsdRead(DeviceObject %x, Irp %x)\n",DeviceObject
,Irp
);
409 /* Precondition / Initialization */
411 Stack
= IoGetCurrentIrpStackLocation(Irp
);
412 assert(Stack
!= NULL
);
413 FileObject
= Stack
->FileObject
;
414 assert(FileObject
!= NULL
);
415 DeviceExt
= DeviceObject
->DeviceExtension
;
416 assert(DeviceExt
!= NULL
);
418 Length
= Stack
->Parameters
.Read
.Length
;
419 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
420 Offset
= Stack
->Parameters
.Read
.ByteOffset
.u
.LowPart
;
422 /* fail if file is a directory */
423 Fcb
= ((PVFATCCB
)(FileObject
->FsContext2
))->pFcb
;
424 if (Fcb
->entry
.Attrib
& FILE_ATTRIBUTE_DIRECTORY
)
426 Status
= STATUS_FILE_IS_A_DIRECTORY
;
430 Status
= FsdReadFile(DeviceExt
,
438 Irp
->IoStatus
.Status
= Status
;
439 Irp
->IoStatus
.Information
= LengthRead
;
440 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);