2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/fs/vfat/rw.c
5 * PURPOSE: VFAT Filesystem
6 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
10 /* INCLUDES *****************************************************************/
16 * Uncomment to enable strict verification of cluster/offset pair
17 * caching. If this option is enabled you lose all the benefits of
18 * the caching and the read/write operations will actually be
19 * slower. It's meant only for debugging!!!
20 * - Filip Navara, 26/07/2004
22 /* #define DEBUG_VERIFY_OFFSET_CACHING */
24 /* FUNCTIONS *****************************************************************/
27 NextCluster(PDEVICE_EXTENSION DeviceExt
,
29 PULONG CurrentCluster
,
32 * Return the next cluster in a FAT chain, possibly extending the chain if
36 if (FirstCluster
== 1)
38 (*CurrentCluster
) += DeviceExt
->FatInfo
.SectorsPerCluster
;
39 return(STATUS_SUCCESS
);
44 return GetNextClusterExtend(DeviceExt
, (*CurrentCluster
), CurrentCluster
);
46 return GetNextCluster(DeviceExt
, (*CurrentCluster
), CurrentCluster
);
51 OffsetToCluster(PDEVICE_EXTENSION DeviceExt
,
57 * Return the cluster corresponding to an offset within a file,
58 * possibly extending the file if necessary
65 DPRINT("OffsetToCluster(DeviceExt %x, Fcb %x, FirstCluster %x,"
66 " FileOffset %x, Cluster %x, Extend %d)\n", DeviceExt,
67 Fcb, FirstCluster, FileOffset, Cluster, Extend);
69 if (FirstCluster
== 0)
71 DbgPrint("OffsetToCluster is called with FirstCluster = 0!\n");
75 if (FirstCluster
== 1)
77 /* root of FAT16 or FAT12 */
78 *Cluster
= DeviceExt
->FatInfo
.rootStart
+ FileOffset
79 / (DeviceExt
->FatInfo
.BytesPerCluster
) * DeviceExt
->FatInfo
.SectorsPerCluster
;
80 return(STATUS_SUCCESS
);
84 CurrentCluster
= FirstCluster
;
87 for (i
= 0; i
< FileOffset
/ DeviceExt
->FatInfo
.BytesPerCluster
; i
++)
89 Status
= GetNextClusterExtend (DeviceExt
, CurrentCluster
, &CurrentCluster
);
90 if (!NT_SUCCESS(Status
))
93 *Cluster
= CurrentCluster
;
97 for (i
= 0; i
< FileOffset
/ DeviceExt
->FatInfo
.BytesPerCluster
; i
++)
99 Status
= GetNextCluster (DeviceExt
, CurrentCluster
, &CurrentCluster
);
100 if (!NT_SUCCESS(Status
))
103 *Cluster
= CurrentCluster
;
105 return(STATUS_SUCCESS
);
110 VfatReadFileData (PVFAT_IRP_CONTEXT IrpContext
,
112 LARGE_INTEGER ReadOffset
,
115 * FUNCTION: Reads data from a file
118 ULONG CurrentCluster
;
122 LARGE_INTEGER StartOffset
;
123 PDEVICE_EXTENSION DeviceExt
;
124 BOOLEAN First
= TRUE
;
129 ULONG BytesPerSector
;
130 ULONG BytesPerCluster
;
136 DeviceExt
= IrpContext
->DeviceExt
;
138 ASSERT(DeviceExt
->FatInfo
.BytesPerCluster
);
139 ASSERT(IrpContext
->FileObject
);
140 ASSERT(IrpContext
->FileObject
->FsContext2
!= NULL
);
142 DPRINT("VfatReadFileData(DeviceExt %p, FileObject %p, "
143 "Length %d, ReadOffset 0x%I64x)\n", DeviceExt
,
144 IrpContext
->FileObject
, Length
, ReadOffset
.QuadPart
);
148 Ccb
= (PVFATCCB
)IrpContext
->FileObject
->FsContext2
;
149 Fcb
= IrpContext
->FileObject
->FsContext
;
150 BytesPerSector
= DeviceExt
->FatInfo
.BytesPerSector
;
151 BytesPerCluster
= DeviceExt
->FatInfo
.BytesPerCluster
;
153 ASSERT(ReadOffset
.QuadPart
+ Length
<= ROUND_UP(Fcb
->RFCB
.FileSize
.QuadPart
, BytesPerSector
));
154 ASSERT(ReadOffset
.u
.LowPart
% BytesPerSector
== 0);
155 ASSERT(Length
% BytesPerSector
== 0);
157 /* Is this a read of the FAT? */
158 if (Fcb
->Flags
& FCB_IS_FAT
)
160 ReadOffset
.QuadPart
+= DeviceExt
->FatInfo
.FATStart
* BytesPerSector
;
161 Status
= VfatReadDiskPartial(IrpContext
, &ReadOffset
, Length
, 0, TRUE
);
163 if (NT_SUCCESS(Status
))
165 *LengthRead
= Length
;
169 DPRINT1("FAT reading failed, Status %x\n", Status
);
173 /* Is this a read of the Volume ? */
174 if (Fcb
->Flags
& FCB_IS_VOLUME
)
176 Status
= VfatReadDiskPartial(IrpContext
, &ReadOffset
, Length
, 0, TRUE
);
177 if (NT_SUCCESS(Status
))
179 *LengthRead
= Length
;
183 DPRINT1("Volume reading failed, Status %x\n", Status
);
189 * Find the first cluster
191 FirstCluster
= CurrentCluster
=
192 vfatDirEntryGetFirstCluster (DeviceExt
, &Fcb
->entry
);
194 if (FirstCluster
== 1)
196 // Directory of FAT12/16 needs a special handling
198 if (ReadOffset
.u
.LowPart
+ Length
> DeviceExt
->FatInfo
.rootDirectorySectors
* BytesPerSector
)
200 Length
= DeviceExt
->FatInfo
.rootDirectorySectors
* BytesPerSector
- ReadOffset
.u
.LowPart
;
202 ReadOffset
.u
.LowPart
+= DeviceExt
->FatInfo
.rootStart
* BytesPerSector
;
204 // Fire up the read command
206 Status
= VfatReadDiskPartial (IrpContext
, &ReadOffset
, Length
, 0, TRUE
);
207 if (NT_SUCCESS(Status
))
209 *LengthRead
= Length
;
214 ExAcquireFastMutex(&Fcb
->LastMutex
);
215 LastCluster
= Fcb
->LastCluster
;
216 LastOffset
= Fcb
->LastOffset
;
217 ExReleaseFastMutex(&Fcb
->LastMutex
);
220 * Find the cluster to start the read from
222 if (LastCluster
> 0 && ReadOffset
.u
.LowPart
>= LastOffset
)
224 Status
= OffsetToCluster(DeviceExt
, LastCluster
,
225 ROUND_DOWN(ReadOffset
.u
.LowPart
, BytesPerCluster
) -
227 &CurrentCluster
, FALSE
);
228 #ifdef DEBUG_VERIFY_OFFSET_CACHING
229 /* DEBUG VERIFICATION */
231 ULONG CorrectCluster
;
232 OffsetToCluster(DeviceExt
, FirstCluster
,
233 ROUND_DOWN(ReadOffset
.u
.LowPart
, BytesPerCluster
),
234 &CorrectCluster
, FALSE
);
235 if (CorrectCluster
!= CurrentCluster
)
236 KEBUGCHECK(FAT_FILE_SYSTEM
);
242 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
243 ROUND_DOWN(ReadOffset
.u
.LowPart
, BytesPerCluster
),
244 &CurrentCluster
, FALSE
);
246 if (!NT_SUCCESS(Status
))
251 ExAcquireFastMutex(&Fcb
->LastMutex
);
252 Fcb
->LastCluster
= CurrentCluster
;
253 Fcb
->LastOffset
= ROUND_DOWN (ReadOffset
.u
.LowPart
, BytesPerCluster
);
254 ExReleaseFastMutex(&Fcb
->LastMutex
);
256 KeInitializeEvent(&IrpContext
->Event
, NotificationEvent
, FALSE
);
257 IrpContext
->RefCount
= 1;
259 while (Length
> 0 && CurrentCluster
!= 0xffffffff)
261 StartCluster
= CurrentCluster
;
262 StartOffset
.QuadPart
= ClusterToSector(DeviceExt
, StartCluster
) * BytesPerSector
;
271 BytesDone
= min (Length
, BytesPerCluster
- (ReadOffset
.u
.LowPart
% BytesPerCluster
));
272 StartOffset
.QuadPart
+= ReadOffset
.u
.LowPart
% BytesPerCluster
;
277 if (Length
- BytesDone
> BytesPerCluster
)
279 BytesDone
+= BytesPerCluster
;
286 Status
= NextCluster(DeviceExt
, FirstCluster
, &CurrentCluster
, FALSE
);
288 while (StartCluster
+ ClusterCount
== CurrentCluster
&& NT_SUCCESS(Status
) && Length
> BytesDone
);
289 DPRINT("start %08x, next %08x, count %d\n",
290 StartCluster
, CurrentCluster
, ClusterCount
);
292 ExAcquireFastMutex(&Fcb
->LastMutex
);
293 Fcb
->LastCluster
= StartCluster
+ (ClusterCount
- 1);
294 Fcb
->LastOffset
= ROUND_DOWN(ReadOffset
.u
.LowPart
, BytesPerCluster
) + (ClusterCount
- 1) * BytesPerCluster
;
295 ExReleaseFastMutex(&Fcb
->LastMutex
);
297 // Fire up the read command
298 Status
= VfatReadDiskPartial (IrpContext
, &StartOffset
, BytesDone
, *LengthRead
, FALSE
);
299 if (!NT_SUCCESS(Status
) && Status
!= STATUS_PENDING
)
303 *LengthRead
+= BytesDone
;
305 ReadOffset
.u
.LowPart
+= BytesDone
;
307 if (0 != InterlockedDecrement((PLONG
)&IrpContext
->RefCount
))
309 KeWaitForSingleObject(&IrpContext
->Event
, Executive
, KernelMode
, FALSE
, NULL
);
311 if (NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
315 Status
= STATUS_UNSUCCESSFUL
;
319 Status
= IrpContext
->Irp
->IoStatus
.Status
;
326 VfatWriteFileData(PVFAT_IRP_CONTEXT IrpContext
,
328 LARGE_INTEGER WriteOffset
)
330 PDEVICE_EXTENSION DeviceExt
;
335 ULONG CurrentCluster
;
339 NTSTATUS Status
= STATUS_SUCCESS
;
340 BOOLEAN First
= TRUE
;
341 ULONG BytesPerSector
;
342 ULONG BytesPerCluster
;
343 LARGE_INTEGER StartOffset
;
350 DeviceExt
= IrpContext
->DeviceExt
;
352 ASSERT(DeviceExt
->FatInfo
.BytesPerCluster
);
353 ASSERT(IrpContext
->FileObject
);
354 ASSERT(IrpContext
->FileObject
->FsContext2
!= NULL
);
356 Ccb
= (PVFATCCB
)IrpContext
->FileObject
->FsContext2
;
357 Fcb
= IrpContext
->FileObject
->FsContext
;
358 BytesPerCluster
= DeviceExt
->FatInfo
.BytesPerCluster
;
359 BytesPerSector
= DeviceExt
->FatInfo
.BytesPerSector
;
361 DPRINT("VfatWriteFileData(DeviceExt %p, FileObject %p, "
362 "Length %d, WriteOffset 0x%I64x), '%wZ'\n", DeviceExt
,
363 IrpContext
->FileObject
, Length
, WriteOffset
,
366 ASSERT(WriteOffset
.QuadPart
+ Length
<= Fcb
->RFCB
.AllocationSize
.QuadPart
);
367 ASSERT(WriteOffset
.u
.LowPart
% BytesPerSector
== 0);
368 ASSERT(Length
% BytesPerSector
== 0);
370 // Is this a write of the volume ?
371 if (Fcb
->Flags
& FCB_IS_VOLUME
)
373 Status
= VfatWriteDiskPartial(IrpContext
, &WriteOffset
, Length
, 0, TRUE
);
374 if (!NT_SUCCESS(Status
))
376 DPRINT1("Volume writing failed, Status %x\n", Status
);
381 // Is this a write to the FAT ?
382 if (Fcb
->Flags
& FCB_IS_FAT
)
384 WriteOffset
.u
.LowPart
+= DeviceExt
->FatInfo
.FATStart
* BytesPerSector
;
385 IrpContext
->RefCount
= 1;
386 for (Count
= 0; Count
< DeviceExt
->FatInfo
.FATCount
; Count
++)
388 Status
= VfatWriteDiskPartial(IrpContext
, &WriteOffset
, Length
, 0, FALSE
);
389 if (!NT_SUCCESS(Status
) && Status
!= STATUS_PENDING
)
391 DPRINT1("FAT writing failed, Status %x\n", Status
);
394 WriteOffset
.u
.LowPart
+= Fcb
->RFCB
.FileSize
.u
.LowPart
;
396 if (0 != InterlockedDecrement((PLONG
)&IrpContext
->RefCount
))
398 KeWaitForSingleObject(&IrpContext
->Event
, Executive
, KernelMode
, FALSE
, NULL
);
400 if (NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
402 Status
= IrpContext
->Irp
->IoStatus
.Status
;
408 * Find the first cluster
410 FirstCluster
= CurrentCluster
=
411 vfatDirEntryGetFirstCluster (DeviceExt
, &Fcb
->entry
);
413 if (FirstCluster
== 1)
415 ASSERT(WriteOffset
.u
.LowPart
+ Length
<= DeviceExt
->FatInfo
.rootDirectorySectors
* BytesPerSector
);
416 // Directory of FAT12/16 needs a special handling
417 WriteOffset
.u
.LowPart
+= DeviceExt
->FatInfo
.rootStart
* BytesPerSector
;
418 // Fire up the write command
419 Status
= VfatWriteDiskPartial (IrpContext
, &WriteOffset
, Length
, 0, TRUE
);
423 ExAcquireFastMutex(&Fcb
->LastMutex
);
424 LastCluster
= Fcb
->LastCluster
;
425 LastOffset
= Fcb
->LastOffset
;
426 ExReleaseFastMutex(&Fcb
->LastMutex
);
429 * Find the cluster to start the write from
431 if (LastCluster
> 0 && WriteOffset
.u
.LowPart
>= LastOffset
)
433 Status
= OffsetToCluster(DeviceExt
, LastCluster
,
434 ROUND_DOWN(WriteOffset
.u
.LowPart
, BytesPerCluster
) -
436 &CurrentCluster
, FALSE
);
437 #ifdef DEBUG_VERIFY_OFFSET_CACHING
438 /* DEBUG VERIFICATION */
440 ULONG CorrectCluster
;
441 OffsetToCluster(DeviceExt
, FirstCluster
,
442 ROUND_DOWN(WriteOffset
.u
.LowPart
, BytesPerCluster
),
443 &CorrectCluster
, FALSE
);
444 if (CorrectCluster
!= CurrentCluster
)
445 KEBUGCHECK(FAT_FILE_SYSTEM
);
451 Status
= OffsetToCluster(DeviceExt
, FirstCluster
,
452 ROUND_DOWN(WriteOffset
.u
.LowPart
, BytesPerCluster
),
453 &CurrentCluster
, FALSE
);
456 if (!NT_SUCCESS(Status
))
461 ExAcquireFastMutex(&Fcb
->LastMutex
);
462 Fcb
->LastCluster
= CurrentCluster
;
463 Fcb
->LastOffset
= ROUND_DOWN (WriteOffset
.u
.LowPart
, BytesPerCluster
);
464 ExReleaseFastMutex(&Fcb
->LastMutex
);
466 IrpContext
->RefCount
= 1;
469 while (Length
> 0 && CurrentCluster
!= 0xffffffff)
471 StartCluster
= CurrentCluster
;
472 StartOffset
.QuadPart
= ClusterToSector(DeviceExt
, StartCluster
) * BytesPerSector
;
481 BytesDone
= min (Length
, BytesPerCluster
- (WriteOffset
.u
.LowPart
% BytesPerCluster
));
482 StartOffset
.QuadPart
+= WriteOffset
.u
.LowPart
% BytesPerCluster
;
487 if (Length
- BytesDone
> BytesPerCluster
)
489 BytesDone
+= BytesPerCluster
;
496 Status
= NextCluster(DeviceExt
, FirstCluster
, &CurrentCluster
, FALSE
);
498 while (StartCluster
+ ClusterCount
== CurrentCluster
&& NT_SUCCESS(Status
) && Length
> BytesDone
);
499 DPRINT("start %08x, next %08x, count %d\n",
500 StartCluster
, CurrentCluster
, ClusterCount
);
502 ExAcquireFastMutex(&Fcb
->LastMutex
);
503 Fcb
->LastCluster
= StartCluster
+ (ClusterCount
- 1);
504 Fcb
->LastOffset
= ROUND_DOWN(WriteOffset
.u
.LowPart
, BytesPerCluster
) + (ClusterCount
- 1) * BytesPerCluster
;
505 ExReleaseFastMutex(&Fcb
->LastMutex
);
507 // Fire up the write command
508 Status
= VfatWriteDiskPartial (IrpContext
, &StartOffset
, BytesDone
, BufferOffset
, FALSE
);
509 if (!NT_SUCCESS(Status
) && Status
!= STATUS_PENDING
)
513 BufferOffset
+= BytesDone
;
515 WriteOffset
.u
.LowPart
+= BytesDone
;
517 if (0 != InterlockedDecrement((PLONG
)&IrpContext
->RefCount
))
519 KeWaitForSingleObject(&IrpContext
->Event
, Executive
, KernelMode
, FALSE
, NULL
);
521 if (NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
525 Status
= STATUS_UNSUCCESSFUL
;
529 Status
= IrpContext
->Irp
->IoStatus
.Status
;
536 VfatRead(PVFAT_IRP_CONTEXT IrpContext
)
541 ULONG ReturnedLength
= 0;
542 PERESOURCE Resource
= NULL
;
543 LARGE_INTEGER ByteOffset
;
545 PDEVICE_OBJECT DeviceToVerify
;
546 ULONG BytesPerSector
;
550 DPRINT("VfatRead(IrpContext %p)\n", IrpContext
);
552 ASSERT(IrpContext
->DeviceObject
);
554 // This request is not allowed on the main device object
555 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
557 DPRINT("VfatRead is called with the main device object.\n");
558 Status
= STATUS_INVALID_DEVICE_REQUEST
;
562 ASSERT(IrpContext
->DeviceExt
);
563 ASSERT(IrpContext
->FileObject
);
564 Fcb
= IrpContext
->FileObject
->FsContext
;
567 DPRINT("<%wZ>\n", &Fcb
->PathNameU
);
569 if (Fcb
->Flags
& FCB_IS_PAGE_FILE
)
571 PFATINFO FatInfo
= &IrpContext
->DeviceExt
->FatInfo
;
572 IrpContext
->Stack
->Parameters
.Read
.ByteOffset
.QuadPart
+= FatInfo
->dataStart
* FatInfo
->BytesPerSector
;
573 IoSkipCurrentIrpStackLocation(IrpContext
->Irp
);
574 DPRINT("Read from page file, disk offset %I64x\n", IrpContext
->Stack
->Parameters
.Read
.ByteOffset
.QuadPart
);
575 Status
= IoCallDriver(IrpContext
->DeviceExt
->StorageDevice
, IrpContext
->Irp
);
576 VfatFreeIrpContext(IrpContext
);
580 ByteOffset
= IrpContext
->Stack
->Parameters
.Read
.ByteOffset
;
581 Length
= IrpContext
->Stack
->Parameters
.Read
.Length
;
582 BytesPerSector
= IrpContext
->DeviceExt
->FatInfo
.BytesPerSector
;
584 /* fail if file is a directory and no paged read */
585 if (*Fcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
&& !(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
))
587 Status
= STATUS_INVALID_PARAMETER
;
592 DPRINT("'%wZ', Offset: %d, Length %d\n", &Fcb
->PathNameU
, ByteOffset
.u
.LowPart
, Length
);
594 if (ByteOffset
.u
.HighPart
&& !(Fcb
->Flags
& FCB_IS_VOLUME
))
596 Status
= STATUS_INVALID_PARAMETER
;
599 if (ByteOffset
.QuadPart
>= Fcb
->RFCB
.FileSize
.QuadPart
)
601 IrpContext
->Irp
->IoStatus
.Information
= 0;
602 Status
= STATUS_END_OF_FILE
;
605 if (IrpContext
->Irp
->Flags
& (IRP_PAGING_IO
| IRP_NOCACHE
) || (Fcb
->Flags
& FCB_IS_VOLUME
))
607 if (ByteOffset
.u
.LowPart
% BytesPerSector
!= 0 || Length
% BytesPerSector
!= 0)
609 DPRINT("%d %d\n", ByteOffset
.u
.LowPart
, Length
);
610 // non cached read must be sector aligned
611 Status
= STATUS_INVALID_PARAMETER
;
617 IrpContext
->Irp
->IoStatus
.Information
= 0;
618 Status
= STATUS_SUCCESS
;
622 if (Fcb
->Flags
& FCB_IS_VOLUME
)
624 Resource
= &IrpContext
->DeviceExt
->DirResource
;
626 else if (IrpContext
->Irp
->Flags
& IRP_PAGING_IO
)
628 Resource
= &Fcb
->PagingIoResource
;
632 Resource
= &Fcb
->MainResource
;
634 if (!ExAcquireResourceSharedLite(Resource
,
635 IrpContext
->Flags
& IRPCONTEXT_CANWAIT
? TRUE
: FALSE
))
638 Status
= STATUS_PENDING
;
642 if (!(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
) &&
643 FsRtlAreThereCurrentFileLocks(&Fcb
->FileLock
))
645 if (!FsRtlCheckLockForReadAccess(&Fcb
->FileLock
, IrpContext
->Irp
))
647 Status
= STATUS_FILE_LOCK_CONFLICT
;
652 Buffer
= VfatGetUserBuffer(IrpContext
->Irp
);
655 Status
= STATUS_INVALID_USER_BUFFER
;
659 if (!(IrpContext
->Irp
->Flags
& (IRP_NOCACHE
|IRP_PAGING_IO
)) &&
660 !(Fcb
->Flags
& (FCB_IS_PAGE_FILE
|FCB_IS_VOLUME
)))
664 Status
= STATUS_SUCCESS
;
665 if (ByteOffset
.u
.LowPart
+ Length
> Fcb
->RFCB
.FileSize
.u
.LowPart
)
667 Length
= Fcb
->RFCB
.FileSize
.u
.LowPart
- ByteOffset
.u
.LowPart
;
668 Status
= /*STATUS_END_OF_FILE*/STATUS_SUCCESS
;
672 if (IrpContext
->FileObject
->PrivateCacheMap
== NULL
)
674 /* FIXME: Guard by SEH. */
675 CcInitializeCacheMap(IrpContext
->FileObject
,
676 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
678 &(VfatGlobalData
->CacheMgrCallbacks
),
681 if (!CcCopyRead(IrpContext
->FileObject
, &ByteOffset
, Length
,
682 (BOOLEAN
)(IrpContext
->Flags
& IRPCONTEXT_CANWAIT
), Buffer
,
683 &IrpContext
->Irp
->IoStatus
))
685 Status
= IrpContext
->Irp
->IoStatus
.Status
;//STATUS_PENDING;
689 if (!NT_SUCCESS(IrpContext
->Irp
->IoStatus
.Status
))
691 Status
= IrpContext
->Irp
->IoStatus
.Status
;
698 if (ByteOffset
.QuadPart
+ Length
> ROUND_UP(Fcb
->RFCB
.FileSize
.QuadPart
, BytesPerSector
))
700 Length
= (ULONG
)(ROUND_UP(Fcb
->RFCB
.FileSize
.QuadPart
, BytesPerSector
) - ByteOffset
.QuadPart
);
703 Status
= VfatLockUserBuffer(IrpContext
->Irp
, Length
, IoWriteAccess
);
704 if (!NT_SUCCESS(Status
))
709 Status
= VfatReadFileData(IrpContext
, Length
, ByteOffset
, &ReturnedLength
);
711 if (Status
== STATUS_VERIFY_REQUIRED
)
713 DPRINT("VfatReadFile returned STATUS_VERIFY_REQUIRED\n");
714 DeviceToVerify
= IoGetDeviceToVerify(PsGetCurrentThread());
715 IoSetDeviceToVerify(PsGetCurrentThread(), DeviceToVerify
);
716 Status
= IoVerifyVolume (DeviceToVerify
, FALSE
);
718 if (NT_SUCCESS(Status
))
720 Status
= VfatReadFileData(IrpContext
, Length
,
721 ByteOffset
, &ReturnedLength
);
726 if (NT_SUCCESS(Status
))
728 IrpContext
->Irp
->IoStatus
.Information
= ReturnedLength
;
735 ExReleaseResourceLite(Resource
);
738 if (Status
== STATUS_PENDING
)
740 Status
= VfatLockUserBuffer(IrpContext
->Irp
, Length
, IoWriteAccess
);
741 if (NT_SUCCESS(Status
))
743 Status
= VfatQueueRequest(IrpContext
);
747 IrpContext
->Irp
->IoStatus
.Status
= Status
;
748 IoCompleteRequest(IrpContext
->Irp
, IO_NO_INCREMENT
);
749 VfatFreeIrpContext(IrpContext
);
754 IrpContext
->Irp
->IoStatus
.Status
= Status
;
755 if (IrpContext
->FileObject
->Flags
& FO_SYNCHRONOUS_IO
&&
756 !(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
) &&
757 (NT_SUCCESS(Status
) || Status
==STATUS_END_OF_FILE
))
759 IrpContext
->FileObject
->CurrentByteOffset
.QuadPart
=
760 ByteOffset
.QuadPart
+ IrpContext
->Irp
->IoStatus
.Information
;
763 IoCompleteRequest(IrpContext
->Irp
,
764 (CCHAR
)(NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
));
765 VfatFreeIrpContext(IrpContext
);
767 DPRINT("%x\n", Status
);
771 NTSTATUS
VfatWrite (PVFAT_IRP_CONTEXT IrpContext
)
774 PERESOURCE Resource
= NULL
;
775 LARGE_INTEGER ByteOffset
;
776 LARGE_INTEGER OldFileSize
;
777 NTSTATUS Status
= STATUS_SUCCESS
;
779 ULONG OldAllocationSize
;
781 ULONG BytesPerSector
;
785 DPRINT("VfatWrite(IrpContext %p)\n", IrpContext
);
787 ASSERT(IrpContext
->DeviceObject
);
789 // This request is not allowed on the main device object
790 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
792 DPRINT("VfatWrite is called with the main device object.\n");
793 Status
= STATUS_INVALID_DEVICE_REQUEST
;
797 ASSERT(IrpContext
->DeviceExt
);
798 ASSERT(IrpContext
->FileObject
);
799 Fcb
= IrpContext
->FileObject
->FsContext
;
802 DPRINT("<%wZ>\n", &Fcb
->PathNameU
);
804 if (Fcb
->Flags
& FCB_IS_PAGE_FILE
)
806 PFATINFO FatInfo
= &IrpContext
->DeviceExt
->FatInfo
;
807 IrpContext
->Stack
->Parameters
.Write
.ByteOffset
.QuadPart
+= FatInfo
->dataStart
* FatInfo
->BytesPerSector
;
808 IoSkipCurrentIrpStackLocation(IrpContext
->Irp
);
809 DPRINT("Write to page file, disk offset %I64x\n", IrpContext
->Stack
->Parameters
.Write
.ByteOffset
.QuadPart
);
810 Status
= IoCallDriver(IrpContext
->DeviceExt
->StorageDevice
, IrpContext
->Irp
);
811 VfatFreeIrpContext(IrpContext
);
815 /* fail if file is a directory and no paged read */
816 if (*Fcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
&& !(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
))
818 Status
= STATUS_INVALID_PARAMETER
;
822 ByteOffset
= IrpContext
->Stack
->Parameters
.Write
.ByteOffset
;
823 if (ByteOffset
.u
.LowPart
== FILE_WRITE_TO_END_OF_FILE
&&
824 ByteOffset
.u
.HighPart
== -1)
826 ByteOffset
.QuadPart
= Fcb
->RFCB
.FileSize
.QuadPart
;
828 Length
= IrpContext
->Stack
->Parameters
.Write
.Length
;
829 BytesPerSector
= IrpContext
->DeviceExt
->FatInfo
.BytesPerSector
;
831 if (ByteOffset
.u
.HighPart
&& !(Fcb
->Flags
& FCB_IS_VOLUME
))
833 Status
= STATUS_INVALID_PARAMETER
;
837 if (Fcb
->Flags
& (FCB_IS_FAT
| FCB_IS_VOLUME
) ||
838 1 == vfatDirEntryGetFirstCluster (IrpContext
->DeviceExt
, &Fcb
->entry
))
840 if (ByteOffset
.QuadPart
+ Length
> Fcb
->RFCB
.FileSize
.QuadPart
)
842 // we can't extend the FAT, the volume or the root on FAT12/FAT16
843 Status
= STATUS_END_OF_FILE
;
848 if (IrpContext
->Irp
->Flags
& (IRP_PAGING_IO
|IRP_NOCACHE
) || (Fcb
->Flags
& FCB_IS_VOLUME
))
850 if (ByteOffset
.u
.LowPart
% BytesPerSector
!= 0 || Length
% BytesPerSector
!= 0)
852 // non cached write must be sector aligned
853 Status
= STATUS_INVALID_PARAMETER
;
861 * Update last write time
863 IrpContext
->Irp
->IoStatus
.Information
= 0;
864 Status
= STATUS_SUCCESS
;
868 if (IrpContext
->Irp
->Flags
& IRP_PAGING_IO
)
870 if (ByteOffset
.u
.LowPart
+ Length
> Fcb
->RFCB
.AllocationSize
.u
.LowPart
)
872 Status
= STATUS_INVALID_PARAMETER
;
875 if (ByteOffset
.u
.LowPart
+ Length
> ROUND_UP(Fcb
->RFCB
.AllocationSize
.u
.LowPart
, BytesPerSector
))
877 Length
= ROUND_UP(Fcb
->RFCB
.FileSize
.u
.LowPart
, BytesPerSector
) - ByteOffset
.u
.LowPart
;
881 if (Fcb
->Flags
& FCB_IS_VOLUME
)
883 Resource
= &IrpContext
->DeviceExt
->DirResource
;
885 else if (IrpContext
->Irp
->Flags
& IRP_PAGING_IO
)
887 Resource
= &Fcb
->PagingIoResource
;
891 Resource
= &Fcb
->MainResource
;
894 if (Fcb
->Flags
& FCB_IS_PAGE_FILE
)
896 if (!ExAcquireResourceSharedLite(Resource
,
897 (BOOLEAN
)(IrpContext
->Flags
& IRPCONTEXT_CANWAIT
)))
900 Status
= STATUS_PENDING
;
906 if (!ExAcquireResourceExclusiveLite(Resource
,
907 (BOOLEAN
)(IrpContext
->Flags
& IRPCONTEXT_CANWAIT
)))
910 Status
= STATUS_PENDING
;
915 if (!(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
) &&
916 FsRtlAreThereCurrentFileLocks(&Fcb
->FileLock
))
918 if (!FsRtlCheckLockForWriteAccess(&Fcb
->FileLock
, IrpContext
->Irp
))
920 Status
= STATUS_FILE_LOCK_CONFLICT
;
925 if (!(IrpContext
->Flags
& IRPCONTEXT_CANWAIT
) && !(Fcb
->Flags
& FCB_IS_VOLUME
))
927 if (ByteOffset
.u
.LowPart
+ Length
> Fcb
->RFCB
.AllocationSize
.u
.LowPart
)
929 Status
= STATUS_PENDING
;
934 OldFileSize
= Fcb
->RFCB
.FileSize
;
935 OldAllocationSize
= Fcb
->RFCB
.AllocationSize
.u
.LowPart
;
937 Buffer
= VfatGetUserBuffer(IrpContext
->Irp
);
940 Status
= STATUS_INVALID_USER_BUFFER
;
945 if (!(Fcb
->Flags
& (FCB_IS_FAT
|FCB_IS_VOLUME
)) &&
946 !(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
) &&
947 ByteOffset
.u
.LowPart
+ Length
> Fcb
->RFCB
.FileSize
.u
.LowPart
)
949 LARGE_INTEGER AllocationSize
;
950 AllocationSize
.QuadPart
= ByteOffset
.u
.LowPart
+ Length
;
951 Status
= VfatSetAllocationSizeInformation(IrpContext
->FileObject
, Fcb
,
952 IrpContext
->DeviceExt
, &AllocationSize
);
953 if (!NT_SUCCESS (Status
))
960 if (!(IrpContext
->Irp
->Flags
& (IRP_NOCACHE
|IRP_PAGING_IO
)) &&
961 !(Fcb
->Flags
& (FCB_IS_PAGE_FILE
|FCB_IS_VOLUME
)))
966 if (IrpContext
->FileObject
->PrivateCacheMap
== NULL
)
968 /* FIXME: Guard by SEH. */
969 CcInitializeCacheMap(IrpContext
->FileObject
,
970 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
972 &VfatGlobalData
->CacheMgrCallbacks
,
975 if (ByteOffset
.QuadPart
> OldFileSize
.QuadPart
)
977 CcZeroData(IrpContext
->FileObject
, &OldFileSize
, &ByteOffset
, TRUE
);
979 if (CcCopyWrite(IrpContext
->FileObject
, &ByteOffset
, Length
,
980 1 /*IrpContext->Flags & IRPCONTEXT_CANWAIT*/, Buffer
))
982 IrpContext
->Irp
->IoStatus
.Information
= Length
;
983 Status
= STATUS_SUCCESS
;
987 Status
= STATUS_UNSUCCESSFUL
;
996 if (ByteOffset
.QuadPart
> OldFileSize
.QuadPart
)
998 CcZeroData(IrpContext
->FileObject
, &OldFileSize
, &ByteOffset
, TRUE
);
1001 Status
= VfatLockUserBuffer(IrpContext
->Irp
, Length
, IoReadAccess
);
1002 if (!NT_SUCCESS(Status
))
1007 Status
= VfatWriteFileData(IrpContext
, Length
, ByteOffset
);
1008 if (NT_SUCCESS(Status
))
1010 IrpContext
->Irp
->IoStatus
.Information
= Length
;
1014 if (!(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
) &&
1015 !(Fcb
->Flags
& (FCB_IS_FAT
|FCB_IS_VOLUME
)))
1017 if(!(*Fcb
->Attributes
& FILE_ATTRIBUTE_DIRECTORY
))
1019 LARGE_INTEGER SystemTime
;
1020 // set dates and times
1021 KeQuerySystemTime (&SystemTime
);
1022 if (Fcb
->Flags
& FCB_IS_FATX_ENTRY
)
1024 FsdSystemTimeToDosDateTime (IrpContext
->DeviceExt
,
1025 &SystemTime
, &Fcb
->entry
.FatX
.UpdateDate
,
1026 &Fcb
->entry
.FatX
.UpdateTime
);
1027 Fcb
->entry
.FatX
.AccessDate
= Fcb
->entry
.FatX
.UpdateDate
;
1028 Fcb
->entry
.FatX
.AccessTime
= Fcb
->entry
.FatX
.UpdateTime
;
1032 FsdSystemTimeToDosDateTime (IrpContext
->DeviceExt
,
1033 &SystemTime
, &Fcb
->entry
.Fat
.UpdateDate
,
1034 &Fcb
->entry
.Fat
.UpdateTime
);
1035 Fcb
->entry
.Fat
.AccessDate
= Fcb
->entry
.Fat
.UpdateDate
;
1037 /* set date and times to dirty */
1038 Fcb
->Flags
|= FCB_IS_DIRTY
;
1045 ExReleaseResourceLite(Resource
);
1048 if (Status
== STATUS_PENDING
)
1050 Status
= VfatLockUserBuffer(IrpContext
->Irp
, Length
, IoReadAccess
);
1051 if (NT_SUCCESS(Status
))
1053 Status
= VfatQueueRequest(IrpContext
);
1057 IrpContext
->Irp
->IoStatus
.Status
= Status
;
1058 IoCompleteRequest(IrpContext
->Irp
, IO_NO_INCREMENT
);
1059 VfatFreeIrpContext(IrpContext
);
1064 IrpContext
->Irp
->IoStatus
.Status
= Status
;
1065 if (IrpContext
->FileObject
->Flags
& FO_SYNCHRONOUS_IO
&&
1066 !(IrpContext
->Irp
->Flags
& IRP_PAGING_IO
) && NT_SUCCESS(Status
))
1068 IrpContext
->FileObject
->CurrentByteOffset
.QuadPart
=
1069 ByteOffset
.QuadPart
+ IrpContext
->Irp
->IoStatus
.Information
;
1072 IoCompleteRequest(IrpContext
->Irp
,
1073 (CCHAR
)(NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
));
1074 VfatFreeIrpContext(IrpContext
);
1076 DPRINT("%x\n", Status
);