af23e51c02d39c8cae3f12a875efce8180c6f941
[reactos.git] / reactos / drivers / fs / vfat / rw.c
1
2 /* $Id: rw.c,v 1.38 2002/01/27 03:25:44 dwelch Exp $
3 *
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS kernel
6 * FILE: services/fs/vfat/rw.c
7 * PURPOSE: VFAT Filesystem
8 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
9 *
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <wchar.h>
16 #include <ntos/minmax.h>
17
18 #define NDEBUG
19 #include <debug.h>
20
21 #include "vfat.h"
22
23 /* GLOBALS *******************************************************************/
24
25 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
26 #define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
27
28 /* FUNCTIONS *****************************************************************/
29
30 NTSTATUS
31 NextCluster(PDEVICE_EXTENSION DeviceExt,
32 PVFATFCB Fcb,
33 ULONG FirstCluster,
34 PULONG CurrentCluster,
35 BOOLEAN Extend)
36 /*
37 * Return the next cluster in a FAT chain, possibly extending the chain if
38 * necessary
39 */
40 {
41 if (Fcb != NULL && Fcb->Flags & FCB_IS_PAGE_FILE)
42 {
43 ULONG i;
44 PULONG FatChain;
45 NTSTATUS Status;
46 DPRINT("NextCluster(Fcb %x, FirstCluster %x, Extend %d)\n", Fcb,
47 FirstCluster, Extend);
48 if (Fcb->FatChainSize == 0)
49 {
50 /* Paging file with zero length. */
51 *CurrentCluster = 0xffffffff;
52 if (Extend)
53 {
54 Fcb->FatChain = ExAllocatePool(NonPagedPool, sizeof(ULONG));
55 if (!Fcb->FatChain)
56 {
57 return STATUS_NO_MEMORY;
58 }
59 Status = GetNextCluster(DeviceExt, 0, CurrentCluster, TRUE);
60 if (!NT_SUCCESS(Status))
61 {
62 ExFreePool(Fcb->FatChain);
63 return Status;
64 }
65 Fcb->FatChain[0] = *CurrentCluster;
66 Fcb->FatChainSize = 1;
67 return Status;
68 }
69 else
70 {
71 return STATUS_UNSUCCESSFUL;
72 }
73 }
74 else
75 {
76 for (i = 0; i < Fcb->FatChainSize; i++)
77 {
78 if (Fcb->FatChain[i] == *CurrentCluster)
79 break;
80 }
81 if (i >= Fcb->FatChainSize)
82 {
83 return STATUS_UNSUCCESSFUL;
84 }
85 if (i == Fcb->FatChainSize - 1)
86 {
87 if (Extend)
88 {
89 FatChain = ExAllocatePool(NonPagedPool,
90 (i + 2) * sizeof(ULONG));
91 if (!FatChain)
92 {
93 *CurrentCluster = 0xffffffff;
94 return STATUS_NO_MEMORY;
95 }
96 Status = GetNextCluster(DeviceExt, *CurrentCluster,
97 CurrentCluster, TRUE);
98 if (NT_SUCCESS(Status) && *CurrentCluster != 0xffffffff)
99 {
100 memcpy(FatChain, Fcb->FatChain, (i + 1) * sizeof(ULONG));
101 FatChain[i + 1] = *CurrentCluster;
102 ExFreePool(Fcb->FatChain);
103 Fcb->FatChain = FatChain;
104 Fcb->FatChainSize = i + 2;
105 }
106 else
107 {
108 ExFreePool(FatChain);
109 }
110 return Status;
111 }
112 else
113 {
114 *CurrentCluster = 0xffffffff;
115 return STATUS_UNSUCCESSFUL;
116 }
117 }
118 *CurrentCluster = Fcb->FatChain[i + 1];
119 return STATUS_SUCCESS;
120 }
121 }
122 if (FirstCluster == 1)
123 {
124 (*CurrentCluster) += DeviceExt->Boot->SectorsPerCluster;
125 return(STATUS_SUCCESS);
126 }
127 else
128 {
129 /*
130 * CN: FIXME: Real bug here or in dirwr, where CurrentCluster isn't
131 * initialized when 0
132 */
133 if (FirstCluster == 0)
134 {
135 NTSTATUS Status;
136
137 Status = GetNextCluster(DeviceExt, 0, CurrentCluster,
138 Extend);
139 return(Status);
140 }
141 else
142 {
143 NTSTATUS Status;
144
145 Status = GetNextCluster(DeviceExt, (*CurrentCluster), CurrentCluster,
146 Extend);
147 return(Status);
148 }
149 }
150 }
151
152 NTSTATUS
153 OffsetToCluster(PDEVICE_EXTENSION DeviceExt,
154 PVFATFCB Fcb,
155 ULONG FirstCluster,
156 ULONG FileOffset,
157 PULONG Cluster,
158 BOOLEAN Extend)
159 /*
160 * Return the cluster corresponding to an offset within a file,
161 * possibly extending the file if necessary
162 */
163 {
164 ULONG CurrentCluster;
165 ULONG i;
166 NTSTATUS Status;
167 DPRINT("OffsetToCluster(DeviceExt %x, Fcb %x, FirstCluster %x,"
168 " FileOffset %x, Cluster %x, Extend %d)\n", DeviceExt,
169 Fcb, FirstCluster, FileOffset, Cluster, Extend);
170 if (FirstCluster == 0)
171 {
172 DbgPrint("OffsetToCluster is called with FirstCluster = 0!\n");
173 KeBugCheck(0);
174 }
175
176 if (Fcb != NULL && Fcb->Flags & FCB_IS_PAGE_FILE)
177 {
178 ULONG NCluster;
179 ULONG Offset = FileOffset / DeviceExt->BytesPerCluster;
180 PULONG FatChain;
181 int i;
182 if (Fcb->FatChainSize == 0)
183 {
184 DbgPrint("OffsetToCluster is called with FirstCluster = %x"
185 " and Fcb->FatChainSize = 0!\n", FirstCluster);
186 KeBugCheck(0);
187 }
188 if (Offset < Fcb->FatChainSize)
189 {
190 *Cluster = Fcb->FatChain[Offset];
191 return STATUS_SUCCESS;
192 }
193 else
194 {
195 if (!Extend)
196 {
197 *Cluster = 0xffffffff;
198 return STATUS_UNSUCCESSFUL;
199 }
200 else
201 {
202 FatChain = ExAllocatePool(NonPagedPool, (Offset + 1) * sizeof(ULONG));
203 if (!FatChain)
204 {
205 *Cluster = 0xffffffff;
206 return STATUS_UNSUCCESSFUL;
207 }
208
209 CurrentCluster = Fcb->FatChain[Fcb->FatChainSize - 1];
210 FatChain[Fcb->FatChainSize - 1] = CurrentCluster;
211 for (i = Fcb->FatChainSize; i < Offset + 1; i++)
212 {
213 Status = GetNextCluster(DeviceExt, CurrentCluster, &CurrentCluster, TRUE);
214 if (!NT_SUCCESS(Status) || CurrentCluster == 0xFFFFFFFF)
215 {
216 while (i >= Fcb->FatChainSize)
217 {
218 WriteCluster(DeviceExt, FatChain[i - 1], 0xFFFFFFFF);
219 i--;
220 }
221 *Cluster = 0xffffffff;
222 ExFreePool(FatChain);
223 if (!NT_SUCCESS(Status))
224 return Status;
225 return STATUS_UNSUCCESSFUL;
226 }
227 FatChain[i] = CurrentCluster;
228 }
229 memcpy (FatChain, Fcb->FatChain, Fcb->FatChainSize * sizeof(ULONG));
230 ExFreePool(Fcb->FatChain);
231 Fcb->FatChain = FatChain;
232 Fcb->FatChainSize = Offset + 1;
233 }
234 }
235 *Cluster = CurrentCluster;
236 return(STATUS_SUCCESS);
237 }
238 if (FirstCluster == 1)
239 {
240 /* root of FAT16 or FAT12 */
241 *Cluster = DeviceExt->rootStart + FileOffset
242 / (DeviceExt->BytesPerCluster) * DeviceExt->Boot->SectorsPerCluster;
243 return(STATUS_SUCCESS);
244 }
245 else
246 {
247 CurrentCluster = FirstCluster;
248 for (i = 0; i < FileOffset / DeviceExt->BytesPerCluster; i++)
249 {
250 Status = GetNextCluster (DeviceExt, CurrentCluster, &CurrentCluster,
251 Extend);
252 if (!NT_SUCCESS(Status))
253 {
254 return(Status);
255 }
256 }
257 *Cluster = CurrentCluster;
258 return(STATUS_SUCCESS);
259 }
260 }
261
262 NTSTATUS
263 VfatReadCluster(PDEVICE_EXTENSION DeviceExt,
264 PVFATFCB Fcb,
265 ULONG FirstCluster,
266 PULONG CurrentCluster,
267 PVOID Destination,
268 ULONG InternalOffset,
269 ULONG InternalLength)
270 {
271 PVOID BaseAddress = NULL;
272 NTSTATUS Status;
273
274 if (InternalLength == DeviceExt->BytesPerCluster)
275 {
276 Status = VfatRawReadCluster(DeviceExt, FirstCluster,
277 Destination, *CurrentCluster, 1);
278 }
279 else
280 {
281 BaseAddress = ExAllocatePool(NonPagedPool, DeviceExt->BytesPerCluster);
282 if (BaseAddress == NULL)
283 {
284 return(STATUS_NO_MEMORY);
285 }
286 Status = VfatRawReadCluster(DeviceExt, FirstCluster,
287 BaseAddress, *CurrentCluster, 1);
288 memcpy(Destination, BaseAddress + InternalOffset, InternalLength);
289 ExFreePool(BaseAddress);
290 }
291 if (!NT_SUCCESS(Status))
292 {
293 return(Status);
294 }
295 Status = NextCluster(DeviceExt, Fcb, FirstCluster, CurrentCluster, FALSE);
296 return(Status);
297 }
298
299 NTSTATUS
300 VfatReadFile (PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject,
301 PVOID Buffer, ULONG Length, ULONG ReadOffset,
302 PULONG LengthRead, ULONG NoCache)
303 /*
304 * FUNCTION: Reads data from a file
305 */
306 {
307 ULONG CurrentCluster;
308 ULONG FirstCluster;
309 ULONG StartCluster;
310 ULONG ClusterCount;
311 PVFATFCB Fcb;
312 PVFATCCB Ccb;
313 NTSTATUS Status;
314 ULONG TempLength;
315 LARGE_INTEGER FileOffset;
316 IO_STATUS_BLOCK IoStatus;
317 ULONG BytesDone;
318
319 /* PRECONDITION */
320 assert (DeviceExt != NULL);
321 assert (DeviceExt->BytesPerCluster != 0);
322 assert (FileObject != NULL);
323 assert (FileObject->FsContext2 != NULL);
324
325 DPRINT("VfatReadFile(DeviceExt %x, FileObject %x, Buffer %x, "
326 "Length %d, ReadOffset 0x%x)\n", DeviceExt, FileObject, Buffer,
327 Length, ReadOffset);
328
329 *LengthRead = 0;
330
331 Ccb = (PVFATCCB)FileObject->FsContext2;
332 Fcb = Ccb->pFcb;
333
334 /* Is this a read of the FAT? */
335 if (Fcb->Flags & FCB_IS_FAT)
336 {
337 if (!NoCache)
338 {
339 DbgPrint ("Cached FAT read outside from VFATFS.SYS\n");
340 KeBugCheck (0);
341 }
342 if (ReadOffset >= Fcb->RFCB.FileSize.QuadPart ||
343 ReadOffset % BLOCKSIZE != 0 || Length % BLOCKSIZE != 0)
344 {
345 DbgPrint ("Start or end of FAT read is not on a sector boundary\n");
346 KeBugCheck (0);
347 }
348 if (ReadOffset + Length > Fcb->RFCB.FileSize.QuadPart)
349 {
350 Length = Fcb->RFCB.FileSize.QuadPart - ReadOffset;
351 }
352
353 Status = VfatReadSectors(DeviceExt->StorageDevice,
354 DeviceExt->FATStart + ReadOffset / BLOCKSIZE,
355 Length / BLOCKSIZE, Buffer);
356 if (NT_SUCCESS(Status))
357 {
358 *LengthRead = Length;
359 }
360 else
361 {
362 DPRINT1("FAT reading failed, Status %x\n", Status);
363 }
364 return Status;
365 }
366
367 /*
368 * Find the first cluster
369 */
370 FirstCluster = CurrentCluster =
371 vfatDirEntryGetFirstCluster (DeviceExt, &Fcb->entry);
372
373 /*
374 * Truncate the read if necessary
375 */
376 if (!(Fcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY))
377 {
378 if (ReadOffset >= Fcb->entry.FileSize)
379 {
380 return (STATUS_END_OF_FILE);
381 }
382 if ((ReadOffset + Length) > Fcb->entry.FileSize)
383 {
384 Length = Fcb->entry.FileSize - ReadOffset;
385 }
386 }
387
388 if (FirstCluster == 1)
389 {
390 /* root directory of FAT12 or FAT16 */
391 if (ReadOffset + Length > DeviceExt->rootDirectorySectors * BLOCKSIZE)
392 {
393 Length = DeviceExt->rootDirectorySectors * BLOCKSIZE - ReadOffset;
394 }
395 }
396
397 /* Using the Cc-interface if possible. */
398 if (!NoCache)
399 {
400 FileOffset.QuadPart = ReadOffset;
401 CcCopyRead(FileObject, &FileOffset, Length, TRUE, Buffer, &IoStatus);
402 *LengthRead = IoStatus.Information;
403 return IoStatus.Status;
404 }
405
406 /*
407 * Find the cluster to start the read from
408 */
409 if (Ccb->LastCluster > 0 && ReadOffset > Ccb->LastOffset)
410 {
411 CurrentCluster = Ccb->LastCluster;
412 }
413 Status = OffsetToCluster(DeviceExt,
414 Fcb,
415 FirstCluster,
416 ROUND_DOWN(ReadOffset, DeviceExt->BytesPerCluster),
417 &CurrentCluster,
418 FALSE);
419 if (!NT_SUCCESS(Status))
420 {
421 return(Status);
422 }
423 /*
424 * If the read doesn't begin on a chunk boundary then we need special
425 * handling
426 */
427 if ((ReadOffset % DeviceExt->BytesPerCluster) != 0 )
428 {
429 TempLength = min (Length, DeviceExt->BytesPerCluster -
430 (ReadOffset % DeviceExt->BytesPerCluster));
431 Ccb->LastCluster = CurrentCluster;
432 Ccb->LastOffset = ROUND_DOWN(ReadOffset, DeviceExt->BytesPerCluster);
433 Status = VfatReadCluster(DeviceExt, Fcb, FirstCluster, &CurrentCluster,
434 Buffer,
435 ReadOffset % DeviceExt->BytesPerCluster,
436 TempLength);
437 if (NT_SUCCESS(Status))
438 {
439 (*LengthRead) = (*LengthRead) + TempLength;
440 Length = Length - TempLength;
441 Buffer = Buffer + TempLength;
442 ReadOffset = ReadOffset + TempLength;
443 }
444 }
445
446 while (Length >= DeviceExt->BytesPerCluster &&
447 CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
448 {
449 StartCluster = CurrentCluster;
450 ClusterCount = 0;
451 BytesDone = 0;
452 /* Search for continous clusters. */
453 do
454 {
455 ClusterCount++;
456 BytesDone += DeviceExt->BytesPerCluster;
457 Status = NextCluster(DeviceExt, Fcb, FirstCluster, &CurrentCluster,
458 FALSE);
459 }
460 while (StartCluster + ClusterCount == CurrentCluster &&
461 NT_SUCCESS(Status) &&
462 Length - BytesDone >= DeviceExt->BytesPerCluster);
463
464 DPRINT("Count %d, Start %x Next %x\n", ClusterCount, StartCluster,
465 CurrentCluster);
466 Ccb->LastCluster = StartCluster + (ClusterCount - 1);
467 Ccb->LastOffset = ReadOffset +
468 (ClusterCount - 1) * DeviceExt->BytesPerCluster;
469
470 Status = VfatRawReadCluster(DeviceExt, FirstCluster, Buffer,
471 StartCluster, ClusterCount);
472 if (NT_SUCCESS(Status))
473 {
474 ClusterCount *= DeviceExt->BytesPerCluster;
475 (*LengthRead) = (*LengthRead) + ClusterCount;
476 Buffer += ClusterCount;
477 Length -= ClusterCount;
478 ReadOffset += ClusterCount;
479 }
480 }
481 /*
482 * If the read doesn't end on a chunk boundary then we need special
483 * handling
484 */
485 if (Length > 0 && CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
486 {
487 Ccb->LastCluster = CurrentCluster;
488 Ccb->LastOffset = ReadOffset + DeviceExt->BytesPerCluster;
489
490 Status = VfatReadCluster(DeviceExt, Fcb, FirstCluster, &CurrentCluster,
491 Buffer, 0, Length);
492 if (NT_SUCCESS(Status))
493 {
494 (*LengthRead) = (*LengthRead) + Length;
495 }
496 }
497 return Status;
498 }
499
500 NTSTATUS
501 VfatWriteCluster(PDEVICE_EXTENSION DeviceExt,
502 PVFATFCB Fcb,
503 ULONG StartOffset,
504 ULONG FirstCluster,
505 PULONG CurrentCluster,
506 PVOID Source,
507 ULONG InternalOffset,
508 ULONG InternalLength)
509 {
510 PVOID BaseAddress;
511 NTSTATUS Status;
512
513 if (InternalLength != DeviceExt->BytesPerCluster)
514 {
515 BaseAddress = ExAllocatePool(NonPagedPool, DeviceExt->BytesPerCluster);
516 if (BaseAddress == NULL)
517 {
518 return(STATUS_NO_MEMORY);
519 }
520 }
521 else
522 BaseAddress = Source;
523 if (InternalLength != DeviceExt->BytesPerCluster)
524 {
525 /*
526 * If the data in the cache isn't valid or we are bypassing the
527 * cache and not writing a cluster aligned, cluster sized region
528 * then read data in to base address
529 */
530 Status = VfatRawReadCluster(DeviceExt, FirstCluster, BaseAddress,
531 *CurrentCluster, 1);
532 if (!NT_SUCCESS(Status))
533 {
534 if (InternalLength != DeviceExt->BytesPerCluster)
535 {
536 ExFreePool(BaseAddress);
537 }
538 return(Status);
539 }
540 memcpy(BaseAddress + InternalOffset, Source, InternalLength);
541 }
542 /*
543 * Write the data back to disk
544 */
545 DPRINT("Writing 0x%x\n", *CurrentCluster);
546 Status = VfatRawWriteCluster(DeviceExt, FirstCluster, BaseAddress,
547 *CurrentCluster, 1);
548 if (InternalLength != DeviceExt->BytesPerCluster)
549 {
550 ExFreePool(BaseAddress);
551 }
552 if (!NT_SUCCESS(Status))
553 {
554 return Status;
555 }
556 Status = NextCluster(DeviceExt, Fcb, FirstCluster, CurrentCluster, FALSE);
557 return(Status);
558 }
559
560 NTSTATUS
561 VfatWriteFile (PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject,
562 PVOID Buffer, ULONG Length, ULONG WriteOffset,
563 BOOLEAN NoCache, BOOLEAN PageIo)
564 /*
565 * FUNCTION: Writes data to file
566 */
567 {
568 ULONG CurrentCluster;
569 ULONG FirstCluster;
570 ULONG StartCluster;
571 ULONG Count;
572 PVFATFCB Fcb;
573 PVFATCCB pCcb;
574 ULONG TempLength;
575 LARGE_INTEGER SystemTime, LocalTime;
576 NTSTATUS Status;
577 BOOLEAN Extend;
578 LARGE_INTEGER FileOffset;
579
580 DPRINT ("VfatWriteFile(FileObject %x, Buffer %x, Length %x, "
581 "WriteOffset %x\n", FileObject, Buffer, Length, WriteOffset);
582
583 assert (FileObject);
584 pCcb = (PVFATCCB) (FileObject->FsContext2);
585 assert (pCcb);
586 Fcb = pCcb->pFcb;
587 assert (Fcb);
588
589 // DPRINT1("%S\n", Fcb->PathName);
590
591 if (Length == 0)
592 {
593 return STATUS_SUCCESS;
594 }
595
596 // Is this a write to the FAT ?
597 if (Fcb->Flags & FCB_IS_FAT)
598 {
599 if (!NoCache && !PageIo)
600 {
601 DbgPrint ("Cached FAT write outside from VFATFS.SYS\n");
602 KeBugCheck (0);
603 }
604 if (WriteOffset >= Fcb->RFCB.FileSize.QuadPart || WriteOffset % BLOCKSIZE != 0 || Length % BLOCKSIZE != 0)
605 {
606 DbgPrint ("Start or end of FAT write is not on a sector boundary\n");
607 KeBugCheck (0);
608 }
609 if (WriteOffset + Length > (ULONG)Fcb->RFCB.FileSize.QuadPart)
610 {
611 Length = (ULONG)Fcb->RFCB.FileSize.QuadPart - WriteOffset;
612 }
613
614 for (Count = 0; Count < DeviceExt->Boot->FATCount; Count++)
615 {
616 Status = VfatWriteSectors(DeviceExt->StorageDevice,
617 DeviceExt->FATStart + (Count * (ULONG)Fcb->RFCB.FileSize.QuadPart + WriteOffset) / BLOCKSIZE,
618 Length / BLOCKSIZE, Buffer);
619 if (!NT_SUCCESS(Status))
620 {
621 DPRINT1("FAT writing failed, Status %x\n", Status);
622 }
623 }
624 return Status;
625 }
626
627 /* Locate the first cluster of the file */
628 FirstCluster = CurrentCluster = vfatDirEntryGetFirstCluster (DeviceExt, &Fcb->entry);
629
630 if (PageIo)
631 {
632 if (FirstCluster == 0)
633 {
634 return STATUS_UNSUCCESSFUL;
635 }
636 }
637 else
638 {
639 if (FirstCluster == 1)
640 {
641 // root directory of FAT12 od FAT16
642 if (WriteOffset + Length > DeviceExt->rootDirectorySectors * BLOCKSIZE)
643 {
644 DPRINT("Writing over the end of the root directory on FAT12/16\n");
645 return STATUS_END_OF_FILE;
646 }
647 }
648
649 Status = vfatExtendSpace(DeviceExt, FileObject, WriteOffset + Length);
650 if (!NT_SUCCESS (Status))
651 {
652 return Status;
653 }
654 }
655
656 if (NoCache || PageIo)
657 {
658
659 FirstCluster = CurrentCluster = vfatDirEntryGetFirstCluster (DeviceExt, &Fcb->entry);
660 if (pCcb->LastCluster > 0 && WriteOffset > pCcb->LastOffset)
661 {
662 CurrentCluster = pCcb->LastCluster;
663 }
664 Status = OffsetToCluster(DeviceExt,
665 Fcb,
666 FirstCluster,
667 ROUND_DOWN(WriteOffset, DeviceExt->BytesPerCluster),
668 &CurrentCluster,
669 FALSE);
670 if (!NT_SUCCESS(Status) || CurrentCluster == 0xffffffff)
671 {
672 DPRINT1("????\n");
673 return(Status);
674 }
675 pCcb->LastCluster = CurrentCluster;
676 pCcb->LastOffset = ROUND_DOWN(WriteOffset, DeviceExt->BytesPerCluster);
677
678 /*
679 * If the offset in the cluster doesn't fall on the cluster boundary
680 * then we have to write only from the specified offset
681 */
682 Status = STATUS_SUCCESS;
683 if ((WriteOffset % DeviceExt->BytesPerCluster) != 0)
684 {
685 TempLength = min (Length, DeviceExt->BytesPerCluster - (WriteOffset % DeviceExt->BytesPerCluster));
686 Status = VfatWriteCluster(DeviceExt,
687 Fcb,
688 ROUND_DOWN(WriteOffset, DeviceExt->BytesPerCluster),
689 FirstCluster,
690 &CurrentCluster,
691 Buffer,
692 WriteOffset % DeviceExt->BytesPerCluster,
693 TempLength);
694 if (NT_SUCCESS(Status))
695 {
696 Buffer = Buffer + TempLength;
697 Length = Length - TempLength;
698 WriteOffset = WriteOffset + TempLength;
699 }
700 }
701
702 while (Length >= DeviceExt->BytesPerCluster && CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
703 {
704 StartCluster = CurrentCluster;
705 Count = 0;
706 // search for continous clusters
707 do
708 {
709 Count++;
710 Status = NextCluster(DeviceExt, Fcb, FirstCluster, &CurrentCluster, FALSE);
711 }
712 while (StartCluster + Count == CurrentCluster && NT_SUCCESS(Status) &&
713 Length - Count * DeviceExt->BytesPerCluster >= DeviceExt->BytesPerCluster);
714
715 pCcb->LastCluster = StartCluster + (Count - 1);
716 pCcb->LastOffset = WriteOffset + (Count - 1) * DeviceExt->BytesPerCluster;
717
718 Status = VfatRawWriteCluster(DeviceExt, FirstCluster, Buffer, StartCluster, Count);
719 if (NT_SUCCESS(Status))
720 {
721 Count *= DeviceExt->BytesPerCluster;
722 Buffer += Count;
723 Length -= Count;
724 WriteOffset += Count;
725 }
726 }
727
728 /* Write the remainder */
729 if (Length > 0 && CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
730 {
731 Status = VfatWriteCluster(DeviceExt,
732 Fcb,
733 WriteOffset,
734 FirstCluster,
735 &CurrentCluster,
736 Buffer,
737 0,
738 Length);
739 if (NT_SUCCESS(Status))
740 {
741 Length = 0;
742 }
743 }
744 if (NT_SUCCESS(Status) && Length)
745 {
746 if (WriteOffset < Fcb->RFCB.AllocationSize.QuadPart)
747 {
748 DPRINT1("%d %d\n", WriteOffset, (ULONG)Fcb->RFCB.AllocationSize.QuadPart);
749 Status = STATUS_DISK_FULL; // ???????????
750 }
751 }
752 }
753 else
754 {
755 // using the Cc-interface if possible
756 FileOffset.QuadPart = WriteOffset;
757 if(CcCopyWrite(FileObject, &FileOffset, Length, TRUE, Buffer))
758 {
759 Status = STATUS_SUCCESS;
760 }
761 else
762 {
763 Status = STATUS_UNSUCCESSFUL;
764 }
765 }
766
767
768 if (!PageIo)
769 {
770 if(!(Fcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY))
771 {
772 /* set dates and times */
773 KeQuerySystemTime (&SystemTime);
774 ExSystemTimeToLocalTime (&SystemTime, &LocalTime);
775 FsdFileTimeToDosDateTime ((TIME*)&LocalTime,
776 &Fcb->entry.UpdateDate,
777 &Fcb->entry.UpdateTime);
778 Fcb->entry.AccessDate = Fcb->entry.UpdateDate;
779 // update dates/times and length
780 updEntry (DeviceExt, FileObject);
781 }
782 }
783
784 return Status;
785 }
786
787 NTSTATUS vfatExtendSpace (PDEVICE_EXTENSION pDeviceExt, PFILE_OBJECT pFileObject, ULONG NewSize)
788 {
789 ULONG FirstCluster;
790 ULONG CurrentCluster;
791 ULONG NewCluster;
792 NTSTATUS Status;
793 PVFATFCB pFcb;
794
795
796 pFcb = ((PVFATCCB) (pFileObject->FsContext2))->pFcb;
797
798 DPRINT ("New Size %d, AllocationSize %d, BytesPerCluster %d\n", NewSize,
799 (ULONG)pFcb->RFCB.AllocationSize.QuadPart, pDeviceExt->BytesPerCluster);
800
801 FirstCluster = CurrentCluster = vfatDirEntryGetFirstCluster (pDeviceExt, &pFcb->entry);
802
803 if (NewSize > pFcb->RFCB.AllocationSize.QuadPart || FirstCluster==0)
804 {
805 // size on disk must be extended
806 if (FirstCluster == 0)
807 {
808 // file of size zero
809 Status = NextCluster (pDeviceExt, pFcb, FirstCluster, &CurrentCluster, TRUE);
810 if (!NT_SUCCESS(Status))
811 {
812 DPRINT1("NextCluster failed, Status %x\n", Status);
813 return Status;
814 }
815 NewCluster = FirstCluster = CurrentCluster;
816 }
817 else
818 {
819 Status = OffsetToCluster(pDeviceExt, pFcb, FirstCluster,
820 pFcb->RFCB.AllocationSize.QuadPart - pDeviceExt->BytesPerCluster,
821 &CurrentCluster, FALSE);
822 if (!NT_SUCCESS(Status))
823 {
824 DPRINT1("OffsetToCluster failed, Status %x\n", Status);
825 return Status;
826 }
827 if (CurrentCluster == 0xffffffff)
828 {
829 DPRINT1("Not enough disk space.\n");
830 return STATUS_DISK_FULL;
831 }
832 // CurrentCluster zeigt jetzt auf den letzten Cluster in der Kette
833 NewCluster = CurrentCluster;
834 Status = NextCluster(pDeviceExt, pFcb, FirstCluster, &NewCluster, FALSE);
835 if (NewCluster != 0xffffffff)
836 {
837 DPRINT1("Difference between size from direntry and the FAT.\n");
838 }
839 }
840
841 Status = OffsetToCluster(pDeviceExt, pFcb, FirstCluster,
842 ROUND_DOWN(NewSize-1, pDeviceExt->BytesPerCluster),
843 &NewCluster, TRUE);
844 if (!NT_SUCCESS(Status) || NewCluster == 0xffffffff)
845 {
846 DPRINT1("Not enough free space on disk\n");
847 if (pFcb->RFCB.AllocationSize.QuadPart > 0)
848 {
849 NewCluster = CurrentCluster;
850 // FIXME: check status
851 NextCluster(pDeviceExt, pFcb, FirstCluster, &NewCluster, FALSE);
852 WriteCluster(pDeviceExt, CurrentCluster, 0xffffffff);
853 }
854 // free the allocated space
855 while (NewCluster != 0xffffffff)
856 {
857 CurrentCluster = NewCluster;
858 // FIXME: check status
859 NextCluster (pDeviceExt, pFcb, FirstCluster, &NewCluster, FALSE);
860 WriteCluster (pDeviceExt, CurrentCluster, 0);
861 }
862 return STATUS_DISK_FULL;
863 }
864 if (pFcb->RFCB.AllocationSize.QuadPart == 0)
865 {
866 pFcb->entry.FirstCluster = FirstCluster;
867 if(pDeviceExt->FatType == FAT32)
868 pFcb->entry.FirstClusterHigh = FirstCluster >> 16;
869 }
870 pFcb->RFCB.AllocationSize.QuadPart = ROUND_UP(NewSize, pDeviceExt->BytesPerCluster);
871 if (pFcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY)
872 {
873 pFcb->RFCB.FileSize.QuadPart = pFcb->RFCB.AllocationSize.QuadPart;
874 pFcb->RFCB.ValidDataLength.QuadPart = pFcb->RFCB.AllocationSize.QuadPart;
875 }
876 else
877 {
878 pFcb->entry.FileSize = NewSize;
879 pFcb->RFCB.FileSize.QuadPart = NewSize;
880 pFcb->RFCB.ValidDataLength.QuadPart = NewSize;
881 }
882 CcSetFileSizes(pFileObject, (PCC_FILE_SIZES)&pFcb->RFCB.AllocationSize);
883 }
884 else
885 {
886 if (NewSize > pFcb->RFCB.FileSize.QuadPart)
887 {
888 // size on disk must not be extended
889 if (!(pFcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY))
890 {
891 pFcb->entry.FileSize = NewSize;
892 pFcb->RFCB.FileSize.QuadPart = NewSize;
893 CcSetFileSizes(pFileObject, (PCC_FILE_SIZES)&pFcb->RFCB.AllocationSize);
894 }
895 }
896 else
897 {
898 // nothing to do
899 }
900 }
901 return STATUS_SUCCESS;
902 }
903
904 NTSTATUS
905 VfatRead(PVFAT_IRP_CONTEXT IrpContext)
906 {
907 PVFATFCB Fcb;
908 PVFATCCB Ccb;
909 NTSTATUS Status = STATUS_SUCCESS;
910 ULONG ReadLength;
911 ULONG ReturnedReadLength = 0;
912 LARGE_INTEGER ReadOffset;
913 PVOID Buffer;
914
915 DPRINT ("VfatRead(IrpContext %x)\n", IrpContext);
916 assert (IrpContext);
917 Ccb = (PVFATCCB) IrpContext->FileObject->FsContext2;
918 assert (Ccb);
919 Fcb = Ccb->pFcb;
920 assert (Fcb);
921
922 if (IrpContext->Irp->Flags & IRP_PAGING_IO)
923 {
924 if (!ExAcquireResourceSharedLite(&Fcb->PagingIoResource,
925 IrpContext->Flags & IRPCONTEXT_CANWAIT))
926 {
927 return VfatQueueRequest (IrpContext);
928 }
929 }
930 else
931 {
932 if (!ExAcquireResourceSharedLite(&Fcb->MainResource,
933 IrpContext->Flags & IRPCONTEXT_CANWAIT))
934 {
935 return VfatQueueRequest (IrpContext);
936 }
937 }
938
939 ReadLength = IrpContext->Stack->Parameters.Read.Length;
940 ReadOffset = IrpContext->Stack->Parameters.Read.ByteOffset;
941 Buffer = MmGetSystemAddressForMdl (IrpContext->Irp->MdlAddress);
942
943 /* fail if file is a directory and no paged read */
944 if (Fcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY &&
945 !(IrpContext->Irp->Flags & IRP_PAGING_IO))
946 {
947 Status = STATUS_FILE_IS_A_DIRECTORY;
948 }
949 else
950 {
951 BOOLEAN NoCache;
952 NoCache = IrpContext->FileObject->Flags & FO_NO_INTERMEDIATE_BUFFERING
953 || IrpContext->Irp->Flags & IRP_PAGING_IO;
954 Status = VfatReadFile (IrpContext->DeviceExt, IrpContext->FileObject,
955 Buffer, ReadLength, ReadOffset.u.LowPart,
956 &ReturnedReadLength,
957 NoCache);
958 }
959
960 if (IrpContext->Irp->Flags & IRP_PAGING_IO)
961 {
962 ExReleaseResourceLite(&Fcb->PagingIoResource);
963 }
964 else
965 {
966 ExReleaseResourceLite(&Fcb->MainResource);
967 }
968
969 if (NT_SUCCESS(Status))
970 {
971 if (IrpContext->FileObject->Flags & FO_SYNCHRONOUS_IO &&
972 !(IrpContext->Irp->Flags & IRP_PAGING_IO))
973 {
974 IrpContext->FileObject->CurrentByteOffset.QuadPart =
975 ReadOffset.QuadPart + ReturnedReadLength;
976 }
977 IrpContext->Irp->IoStatus.Information = ReturnedReadLength;
978 }
979 else
980 {
981 IrpContext->Irp->IoStatus.Information = 0;
982 }
983
984 IrpContext->Irp->IoStatus.Status = Status;
985 IoCompleteRequest (IrpContext->Irp, IO_NO_INCREMENT);
986 VfatFreeIrpContext (IrpContext);
987
988 return Status;
989 }
990
991 NTSTATUS VfatWrite(PVFAT_IRP_CONTEXT IrpContext)
992 {
993 PVFATFCB Fcb;
994 PVFATCCB Ccb;
995 NTSTATUS Status = STATUS_SUCCESS;
996 ULONG WriteLength;
997 LARGE_INTEGER WriteOffset;
998 PVOID Buffer;
999
1000 DPRINT ("VfatWrite(), %S\n", ((PVFATCCB) IrpContext->FileObject->FsContext2)->pFcb->FileName);
1001 assert (IrpContext);
1002 Ccb = (PVFATCCB) IrpContext->FileObject->FsContext2;
1003 assert (Ccb);
1004 Fcb = Ccb->pFcb;
1005 assert (Fcb);
1006
1007 if (IrpContext->Irp->Flags & IRP_PAGING_IO)
1008 {
1009 if (!ExAcquireResourceExclusiveLite(&Fcb->PagingIoResource, IrpContext->Flags & IRPCONTEXT_CANWAIT))
1010 {
1011 return VfatQueueRequest (IrpContext);
1012 }
1013 }
1014 else
1015 {
1016 if (!ExAcquireResourceExclusiveLite(&Fcb->MainResource, IrpContext->Flags & IRPCONTEXT_CANWAIT))
1017 {
1018 return VfatQueueRequest (IrpContext);
1019 }
1020 }
1021
1022 WriteLength = IrpContext->Stack->Parameters.Write.Length;
1023 WriteOffset = IrpContext->Stack->Parameters.Write.ByteOffset;
1024 Buffer = MmGetSystemAddressForMdl (IrpContext->Irp->MdlAddress);
1025
1026 /* fail if file is a directory and no paged read */
1027 if (Fcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY && !(IrpContext->Irp->Flags & IRP_PAGING_IO))
1028 {
1029 Status = STATUS_FILE_IS_A_DIRECTORY;
1030 }
1031 else
1032 {
1033 Status = VfatWriteFile (IrpContext->DeviceExt, IrpContext->FileObject,
1034 Buffer, WriteLength, WriteOffset.u.LowPart,
1035 IrpContext->FileObject->Flags & FO_NO_INTERMEDIATE_BUFFERING,
1036 IrpContext->Irp->Flags & IRP_PAGING_IO);
1037 }
1038
1039 if (IrpContext->Irp->Flags & IRP_PAGING_IO)
1040 {
1041 ExReleaseResourceLite(&Fcb->PagingIoResource);
1042 }
1043 else
1044 {
1045 ExReleaseResourceLite(&Fcb->MainResource);
1046 }
1047
1048 if (NT_SUCCESS(Status))
1049 {
1050 if (IrpContext->FileObject->Flags & FO_SYNCHRONOUS_IO && !(IrpContext->Irp->Flags & IRP_PAGING_IO))
1051 {
1052 IrpContext->FileObject->CurrentByteOffset.QuadPart = WriteOffset.QuadPart + WriteLength;
1053 }
1054 IrpContext->Irp->IoStatus.Information = WriteLength;
1055 }
1056 else
1057 {
1058 IrpContext->Irp->IoStatus.Information = 0;
1059 }
1060
1061 IrpContext->Irp->IoStatus.Status = Status;
1062 IoCompleteRequest (IrpContext->Irp, IO_NO_INCREMENT);
1063 VfatFreeIrpContext (IrpContext);
1064 return Status;
1065 }
1066
1067