[FASTFAT]
[reactos.git] / reactos / drivers / filesystems / fastfat / fsctl.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: drivers/fs/vfat/fsctl.c
23 * PURPOSE: VFAT Filesystem
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #include "vfat.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 /* FUNCTIONS ****************************************************************/
34
35 #define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
36 (pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
37
38 static
39 NTSTATUS
40 VfatHasFileSystem(
41 PDEVICE_OBJECT DeviceToMount,
42 PBOOLEAN RecognizedFS,
43 PFATINFO pFatInfo,
44 BOOLEAN Override)
45 {
46 NTSTATUS Status;
47 PARTITION_INFORMATION PartitionInfo;
48 DISK_GEOMETRY DiskGeometry;
49 FATINFO FatInfo;
50 ULONG Size;
51 ULONG Sectors;
52 LARGE_INTEGER Offset;
53 struct _BootSector* Boot;
54 struct _BootSectorFatX* BootFatX;
55 BOOLEAN PartitionInfoIsValid = FALSE;
56
57 DPRINT("VfatHasFileSystem\n");
58
59 *RecognizedFS = FALSE;
60
61 Size = sizeof(DISK_GEOMETRY);
62 Status = VfatBlockDeviceIoControl(DeviceToMount,
63 IOCTL_DISK_GET_DRIVE_GEOMETRY,
64 NULL,
65 0,
66 &DiskGeometry,
67 &Size,
68 Override);
69 if (!NT_SUCCESS(Status))
70 {
71 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status);
72 return Status;
73 }
74
75 FatInfo.FixedMedia = DiskGeometry.MediaType == FixedMedia ? TRUE : FALSE;
76 if (DiskGeometry.MediaType == FixedMedia || DiskGeometry.MediaType == RemovableMedia)
77 {
78 // We have found a hard disk
79 Size = sizeof(PARTITION_INFORMATION);
80 Status = VfatBlockDeviceIoControl(DeviceToMount,
81 IOCTL_DISK_GET_PARTITION_INFO,
82 NULL,
83 0,
84 &PartitionInfo,
85 &Size,
86 Override);
87 if (!NT_SUCCESS(Status))
88 {
89 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status);
90 return Status;
91 }
92
93 DPRINT("Partition Information:\n");
94 DPRINT("StartingOffset %I64x\n", PartitionInfo.StartingOffset.QuadPart / 512);
95 DPRINT("PartitionLength %I64x\n", PartitionInfo.PartitionLength.QuadPart / 512);
96 DPRINT("HiddenSectors %u\n", PartitionInfo.HiddenSectors);
97 DPRINT("PartitionNumber %u\n", PartitionInfo.PartitionNumber);
98 DPRINT("PartitionType %u\n", PartitionInfo.PartitionType);
99 DPRINT("BootIndicator %u\n", PartitionInfo.BootIndicator);
100 DPRINT("RecognizedPartition %u\n", PartitionInfo.RecognizedPartition);
101 DPRINT("RewritePartition %u\n", PartitionInfo.RewritePartition);
102 if (PartitionInfo.PartitionType)
103 {
104 if (PartitionInfo.PartitionType == PARTITION_FAT_12 ||
105 PartitionInfo.PartitionType == PARTITION_FAT_16 ||
106 PartitionInfo.PartitionType == PARTITION_HUGE ||
107 PartitionInfo.PartitionType == PARTITION_FAT32 ||
108 PartitionInfo.PartitionType == PARTITION_FAT32_XINT13 ||
109 PartitionInfo.PartitionType == PARTITION_XINT13)
110 {
111 PartitionInfoIsValid = TRUE;
112 *RecognizedFS = TRUE;
113 }
114 }
115 else if (DiskGeometry.MediaType == RemovableMedia &&
116 PartitionInfo.PartitionNumber > 0 &&
117 PartitionInfo.StartingOffset.QuadPart == 0 &&
118 PartitionInfo.PartitionLength.QuadPart > 0)
119 {
120 /* This is possible a removable media formated as super floppy */
121 PartitionInfoIsValid = TRUE;
122 *RecognizedFS = TRUE;
123 }
124 }
125 else
126 {
127 *RecognizedFS = TRUE;
128 }
129
130 if (*RecognizedFS)
131 {
132 Boot = ExAllocatePoolWithTag(NonPagedPool, DiskGeometry.BytesPerSector, TAG_VFAT);
133 if (Boot == NULL)
134 {
135 return STATUS_INSUFFICIENT_RESOURCES;
136 }
137
138 Offset.QuadPart = 0;
139
140 /* Try to recognize FAT12/FAT16/FAT32 partitions */
141 Status = VfatReadDisk(DeviceToMount, &Offset, DiskGeometry.BytesPerSector, (PUCHAR) Boot, Override);
142 if (NT_SUCCESS(Status))
143 {
144 if (Boot->Signatur1 != 0xaa55)
145 {
146 *RecognizedFS = FALSE;
147 }
148
149 if (*RecognizedFS &&
150 Boot->BytesPerSector != 512 &&
151 Boot->BytesPerSector != 1024 &&
152 Boot->BytesPerSector != 2048 &&
153 Boot->BytesPerSector != 4096)
154 {
155 DPRINT1("BytesPerSector %u\n", Boot->BytesPerSector);
156 *RecognizedFS = FALSE;
157 }
158
159 if (*RecognizedFS &&
160 Boot->FATCount != 1 &&
161 Boot->FATCount != 2)
162 {
163 DPRINT1("FATCount %u\n", Boot->FATCount);
164 *RecognizedFS = FALSE;
165 }
166
167 if (*RecognizedFS &&
168 Boot->Media != 0xf0 &&
169 Boot->Media != 0xf8 &&
170 Boot->Media != 0xf9 &&
171 Boot->Media != 0xfa &&
172 Boot->Media != 0xfb &&
173 Boot->Media != 0xfc &&
174 Boot->Media != 0xfd &&
175 Boot->Media != 0xfe &&
176 Boot->Media != 0xff)
177 {
178 DPRINT1("Media %02x\n", Boot->Media);
179 *RecognizedFS = FALSE;
180 }
181
182 if (*RecognizedFS &&
183 Boot->SectorsPerCluster != 1 &&
184 Boot->SectorsPerCluster != 2 &&
185 Boot->SectorsPerCluster != 4 &&
186 Boot->SectorsPerCluster != 8 &&
187 Boot->SectorsPerCluster != 16 &&
188 Boot->SectorsPerCluster != 32 &&
189 Boot->SectorsPerCluster != 64 &&
190 Boot->SectorsPerCluster != 128)
191 {
192 DPRINT1("SectorsPerCluster %02x\n", Boot->SectorsPerCluster);
193 *RecognizedFS = FALSE;
194 }
195
196 if (*RecognizedFS &&
197 Boot->BytesPerSector * Boot->SectorsPerCluster > 32 * 1024)
198 {
199 DPRINT1("ClusterSize %dx\n", Boot->BytesPerSector * Boot->SectorsPerCluster);
200 *RecognizedFS = FALSE;
201 }
202
203 if (*RecognizedFS)
204 {
205 FatInfo.VolumeID = Boot->VolumeID;
206 FatInfo.FATStart = Boot->ReservedSectors;
207 FatInfo.FATCount = Boot->FATCount;
208 FatInfo.FATSectors = Boot->FATSectors ? Boot->FATSectors : ((struct _BootSector32*) Boot)->FATSectors32;
209 FatInfo.BytesPerSector = Boot->BytesPerSector;
210 FatInfo.SectorsPerCluster = Boot->SectorsPerCluster;
211 FatInfo.BytesPerCluster = FatInfo.BytesPerSector * FatInfo.SectorsPerCluster;
212 FatInfo.rootDirectorySectors = ((Boot->RootEntries * 32) + Boot->BytesPerSector - 1) / Boot->BytesPerSector;
213 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
214 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
215 FatInfo.Sectors = Sectors = Boot->Sectors ? Boot->Sectors : Boot->SectorsHuge;
216 Sectors -= Boot->ReservedSectors + FatInfo.FATCount * FatInfo.FATSectors + FatInfo.rootDirectorySectors;
217 FatInfo.NumberOfClusters = Sectors / Boot->SectorsPerCluster;
218 if (FatInfo.NumberOfClusters < 4085)
219 {
220 DPRINT("FAT12\n");
221 FatInfo.FatType = FAT12;
222 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
223 }
224 else if (FatInfo.NumberOfClusters >= 65525)
225 {
226 DPRINT("FAT32\n");
227 FatInfo.FatType = FAT32;
228 FatInfo.RootCluster = ((struct _BootSector32*) Boot)->RootCluster;
229 FatInfo.rootStart = FatInfo.dataStart + ((FatInfo.RootCluster - 2) * FatInfo.SectorsPerCluster);
230 FatInfo.VolumeID = ((struct _BootSector32*) Boot)->VolumeID;
231 }
232 else
233 {
234 DPRINT("FAT16\n");
235 FatInfo.FatType = FAT16;
236 FatInfo.RootCluster = FatInfo.rootStart / FatInfo.SectorsPerCluster;
237 }
238
239 if (PartitionInfoIsValid &&
240 FatInfo.Sectors > PartitionInfo.PartitionLength.QuadPart / FatInfo.BytesPerSector)
241 {
242 *RecognizedFS = FALSE;
243 }
244
245 if (pFatInfo && *RecognizedFS)
246 {
247 *pFatInfo = FatInfo;
248 }
249 }
250 }
251
252 ExFreePool(Boot);
253 }
254
255 if (!*RecognizedFS && PartitionInfoIsValid)
256 {
257 BootFatX = ExAllocatePoolWithTag(NonPagedPool, sizeof(struct _BootSectorFatX), TAG_VFAT);
258 if (BootFatX == NULL)
259 {
260 *RecognizedFS=FALSE;
261 return STATUS_INSUFFICIENT_RESOURCES;
262 }
263
264 Offset.QuadPart = 0;
265
266 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
267 Status = VfatReadDisk(DeviceToMount, &Offset, sizeof(struct _BootSectorFatX), (PUCHAR) BootFatX, Override);
268 if (NT_SUCCESS(Status))
269 {
270 *RecognizedFS = TRUE;
271 if (BootFatX->SysType[0] != 'F' ||
272 BootFatX->SysType[1] != 'A' ||
273 BootFatX->SysType[2] != 'T' ||
274 BootFatX->SysType[3] != 'X')
275 {
276 DPRINT1("SysType %c%c%c%c\n", BootFatX->SysType[0], BootFatX->SysType[1], BootFatX->SysType[2], BootFatX->SysType[3]);
277 *RecognizedFS=FALSE;
278 }
279
280 if (*RecognizedFS &&
281 BootFatX->SectorsPerCluster != 1 &&
282 BootFatX->SectorsPerCluster != 2 &&
283 BootFatX->SectorsPerCluster != 4 &&
284 BootFatX->SectorsPerCluster != 8 &&
285 BootFatX->SectorsPerCluster != 16 &&
286 BootFatX->SectorsPerCluster != 32 &&
287 BootFatX->SectorsPerCluster != 64 &&
288 BootFatX->SectorsPerCluster != 128)
289 {
290 DPRINT1("SectorsPerCluster %lu\n", BootFatX->SectorsPerCluster);
291 *RecognizedFS=FALSE;
292 }
293
294 if (*RecognizedFS)
295 {
296 FatInfo.BytesPerSector = DiskGeometry.BytesPerSector;
297 FatInfo.SectorsPerCluster = BootFatX->SectorsPerCluster;
298 FatInfo.rootDirectorySectors = BootFatX->SectorsPerCluster;
299 FatInfo.BytesPerCluster = BootFatX->SectorsPerCluster * DiskGeometry.BytesPerSector;
300 FatInfo.Sectors = (ULONG)(PartitionInfo.PartitionLength.QuadPart / DiskGeometry.BytesPerSector);
301 if (FatInfo.Sectors / FatInfo.SectorsPerCluster < 65525)
302 {
303 DPRINT("FATX16\n");
304 FatInfo.FatType = FATX16;
305 }
306 else
307 {
308 DPRINT("FATX32\n");
309 FatInfo.FatType = FATX32;
310 }
311 FatInfo.VolumeID = BootFatX->VolumeID;
312 FatInfo.FATStart = sizeof(struct _BootSectorFatX) / DiskGeometry.BytesPerSector;
313 FatInfo.FATCount = BootFatX->FATCount;
314 FatInfo.FATSectors =
315 ROUND_UP(FatInfo.Sectors / FatInfo.SectorsPerCluster * (FatInfo.FatType == FATX16 ? 2 : 4), 4096) /
316 FatInfo.BytesPerSector;
317 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
318 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
319 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
320 FatInfo.NumberOfClusters = (FatInfo.Sectors - FatInfo.dataStart) / FatInfo.SectorsPerCluster;
321
322 if (pFatInfo && *RecognizedFS)
323 {
324 *pFatInfo = FatInfo;
325 }
326 }
327 }
328 ExFreePool(BootFatX);
329 }
330
331 DPRINT("VfatHasFileSystem done\n");
332 return Status;
333 }
334
335 /*
336 * FUNCTION: Mounts the device
337 */
338 static
339 NTSTATUS
340 VfatMountDevice(
341 PDEVICE_EXTENSION DeviceExt,
342 PDEVICE_OBJECT DeviceToMount)
343 {
344 NTSTATUS Status;
345 BOOLEAN RecognizedFS;
346
347 DPRINT("Mounting VFAT device...\n");
348
349 Status = VfatHasFileSystem(DeviceToMount, &RecognizedFS, &DeviceExt->FatInfo, FALSE);
350 if (!NT_SUCCESS(Status))
351 {
352 return Status;
353 }
354 DPRINT("MountVfatdev %u, PAGE_SIZE = %d\n", DeviceExt->FatInfo.BytesPerCluster, PAGE_SIZE);
355
356 return STATUS_SUCCESS;
357 }
358
359
360 /*
361 * FUNCTION: Mount the filesystem
362 */
363 static
364 NTSTATUS
365 VfatMount(
366 PVFAT_IRP_CONTEXT IrpContext)
367 {
368 PDEVICE_OBJECT DeviceObject = NULL;
369 PDEVICE_EXTENSION DeviceExt = NULL;
370 BOOLEAN RecognizedFS;
371 NTSTATUS Status;
372 PVFATFCB Fcb = NULL;
373 PVFATFCB VolumeFcb = NULL;
374 PVFATCCB Ccb = NULL;
375 PDEVICE_OBJECT DeviceToMount;
376 PVPB Vpb;
377 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\$$Fat$$");
378 UNICODE_STRING VolumeNameU = RTL_CONSTANT_STRING(L"\\$$Volume$$");
379 ULONG HashTableSize;
380 ULONG eocMark;
381 FATINFO FatInfo;
382
383 DPRINT("VfatMount(IrpContext %p)\n", IrpContext);
384
385 ASSERT(IrpContext);
386
387 if (IrpContext->DeviceObject != VfatGlobalData->DeviceObject)
388 {
389 Status = STATUS_INVALID_DEVICE_REQUEST;
390 goto ByeBye;
391 }
392
393 DeviceToMount = IrpContext->Stack->Parameters.MountVolume.DeviceObject;
394 Vpb = IrpContext->Stack->Parameters.MountVolume.Vpb;
395
396 Status = VfatHasFileSystem(DeviceToMount, &RecognizedFS, &FatInfo, FALSE);
397 if (!NT_SUCCESS(Status))
398 {
399 goto ByeBye;
400 }
401
402 if (RecognizedFS == FALSE)
403 {
404 DPRINT("VFAT: Unrecognized Volume\n");
405 Status = STATUS_UNRECOGNIZED_VOLUME;
406 goto ByeBye;
407 }
408
409 /* Use prime numbers for the table size */
410 if (FatInfo.FatType == FAT12)
411 {
412 HashTableSize = 4099; // 4096 = 4 * 1024
413 }
414 else if (FatInfo.FatType == FAT16 ||
415 FatInfo.FatType == FATX16)
416 {
417 HashTableSize = 16411; // 16384 = 16 * 1024
418 }
419 else
420 {
421 HashTableSize = 65537; // 65536 = 64 * 1024;
422 }
423 DPRINT("VFAT: Recognized volume\n");
424 Status = IoCreateDevice(VfatGlobalData->DriverObject,
425 ROUND_UP(sizeof (DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize,
426 NULL,
427 FILE_DEVICE_DISK_FILE_SYSTEM,
428 DeviceToMount->Characteristics,
429 FALSE,
430 &DeviceObject);
431 if (!NT_SUCCESS(Status))
432 {
433 goto ByeBye;
434 }
435
436 DeviceExt = DeviceObject->DeviceExtension;
437 RtlZeroMemory(DeviceExt, ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize);
438 DeviceExt->FcbHashTable = (HASHENTRY**)((ULONG_PTR)DeviceExt + ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)));
439 DeviceExt->HashTableSize = HashTableSize;
440 DeviceExt->VolumeDevice = DeviceObject;
441
442 /* use same vpb as device disk */
443 DeviceObject->Vpb = Vpb;
444 DeviceToMount->Vpb = Vpb;
445
446 Status = VfatMountDevice(DeviceExt, DeviceToMount);
447 if (!NT_SUCCESS(Status))
448 {
449 goto ByeBye;
450 }
451
452 DPRINT("BytesPerSector: %u\n", DeviceExt->FatInfo.BytesPerSector);
453 DPRINT("SectorsPerCluster: %u\n", DeviceExt->FatInfo.SectorsPerCluster);
454 DPRINT("FATCount: %u\n", DeviceExt->FatInfo.FATCount);
455 DPRINT("FATSectors: %u\n", DeviceExt->FatInfo.FATSectors);
456 DPRINT("RootStart: %u\n", DeviceExt->FatInfo.rootStart);
457 DPRINT("DataStart: %u\n", DeviceExt->FatInfo.dataStart);
458 if (DeviceExt->FatInfo.FatType == FAT32)
459 {
460 DPRINT("RootCluster: %u\n", DeviceExt->FatInfo.RootCluster);
461 }
462
463 switch (DeviceExt->FatInfo.FatType)
464 {
465 case FAT12:
466 DeviceExt->GetNextCluster = FAT12GetNextCluster;
467 DeviceExt->FindAndMarkAvailableCluster = FAT12FindAndMarkAvailableCluster;
468 DeviceExt->WriteCluster = FAT12WriteCluster;
469 DeviceExt->CleanShutBitMask = 0;
470 break;
471
472 case FAT16:
473 case FATX16:
474 DeviceExt->GetNextCluster = FAT16GetNextCluster;
475 DeviceExt->FindAndMarkAvailableCluster = FAT16FindAndMarkAvailableCluster;
476 DeviceExt->WriteCluster = FAT16WriteCluster;
477 DeviceExt->CleanShutBitMask = 0x8000;
478 break;
479
480 case FAT32:
481 case FATX32:
482 DeviceExt->GetNextCluster = FAT32GetNextCluster;
483 DeviceExt->FindAndMarkAvailableCluster = FAT32FindAndMarkAvailableCluster;
484 DeviceExt->WriteCluster = FAT32WriteCluster;
485 DeviceExt->CleanShutBitMask = 0x80000000;
486 break;
487 }
488
489 if (DeviceExt->FatInfo.FatType == FATX16 ||
490 DeviceExt->FatInfo.FatType == FATX32)
491 {
492 DeviceExt->Flags |= VCB_IS_FATX;
493 DeviceExt->GetNextDirEntry = FATXGetNextDirEntry;
494 DeviceExt->BaseDateYear = 2000;
495 }
496 else
497 {
498 DeviceExt->GetNextDirEntry = FATGetNextDirEntry;
499 DeviceExt->BaseDateYear = 1980;
500 }
501
502 DeviceExt->StorageDevice = DeviceToMount;
503 DeviceExt->StorageDevice->Vpb->DeviceObject = DeviceObject;
504 DeviceExt->StorageDevice->Vpb->RealDevice = DeviceExt->StorageDevice;
505 DeviceExt->StorageDevice->Vpb->Flags |= VPB_MOUNTED;
506 DeviceObject->StackSize = DeviceExt->StorageDevice->StackSize + 1;
507 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
508
509 DPRINT("FsDeviceObject %p\n", DeviceObject);
510
511 /* Initialize this resource early ... it's used in VfatCleanup */
512 ExInitializeResourceLite(&DeviceExt->DirResource);
513
514 DeviceExt->IoVPB = DeviceObject->Vpb;
515 DeviceExt->SpareVPB = ExAllocatePoolWithTag(NonPagedPool, sizeof(VPB), TAG_VFAT);
516 if (DeviceExt->SpareVPB == NULL)
517 {
518 Status = STATUS_INSUFFICIENT_RESOURCES;
519 goto ByeBye;
520 }
521
522 DeviceExt->FATFileObject = IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
523 Fcb = vfatNewFCB(DeviceExt, &NameU);
524 if (Fcb == NULL)
525 {
526 Status = STATUS_INSUFFICIENT_RESOURCES;
527 goto ByeBye;
528 }
529
530 Ccb = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
531 if (Ccb == NULL)
532 {
533 Status = STATUS_INSUFFICIENT_RESOURCES;
534 goto ByeBye;
535 }
536
537 RtlZeroMemory(Ccb, sizeof (VFATCCB));
538 DeviceExt->FATFileObject->FsContext = Fcb;
539 DeviceExt->FATFileObject->FsContext2 = Ccb;
540 DeviceExt->FATFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
541 DeviceExt->FATFileObject->PrivateCacheMap = NULL;
542 DeviceExt->FATFileObject->Vpb = DeviceObject->Vpb;
543 Fcb->FileObject = DeviceExt->FATFileObject;
544
545 Fcb->Flags |= FCB_IS_FAT;
546
547 Fcb->RFCB.FileSize.QuadPart = DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector;
548 Fcb->RFCB.ValidDataLength = Fcb->RFCB.FileSize;
549 Fcb->RFCB.AllocationSize = Fcb->RFCB.FileSize;
550
551 _SEH2_TRY
552 {
553 CcInitializeCacheMap(DeviceExt->FATFileObject,
554 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
555 TRUE,
556 &VfatGlobalData->CacheMgrCallbacks,
557 Fcb);
558 }
559 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
560 {
561 Status = _SEH2_GetExceptionCode();
562 goto ByeBye;
563 }
564 _SEH2_END;
565
566 DeviceExt->LastAvailableCluster = 2;
567 ExInitializeResourceLite(&DeviceExt->FatResource);
568
569 InitializeListHead(&DeviceExt->FcbListHead);
570
571 VolumeFcb = vfatNewFCB(DeviceExt, &VolumeNameU);
572 if (VolumeFcb == NULL)
573 {
574 Status = STATUS_INSUFFICIENT_RESOURCES;
575 goto ByeBye;
576 }
577
578 VolumeFcb->Flags = FCB_IS_VOLUME;
579 VolumeFcb->RFCB.FileSize.QuadPart = (LONGLONG) DeviceExt->FatInfo.Sectors * DeviceExt->FatInfo.BytesPerSector;
580 VolumeFcb->RFCB.ValidDataLength = VolumeFcb->RFCB.FileSize;
581 VolumeFcb->RFCB.AllocationSize = VolumeFcb->RFCB.FileSize;
582 DeviceExt->VolumeFcb = VolumeFcb;
583
584 ExAcquireResourceExclusiveLite(&VfatGlobalData->VolumeListLock, TRUE);
585 InsertHeadList(&VfatGlobalData->VolumeListHead, &DeviceExt->VolumeListEntry);
586 ExReleaseResourceLite(&VfatGlobalData->VolumeListLock);
587
588 /* read serial number */
589 DeviceObject->Vpb->SerialNumber = DeviceExt->FatInfo.VolumeID;
590
591 /* read volume label */
592 ReadVolumeLabel(DeviceExt, DeviceObject->Vpb);
593
594 /* read clean shutdown bit status */
595 Status = GetNextCluster(DeviceExt, 1, &eocMark);
596 if (NT_SUCCESS(Status))
597 {
598 if (eocMark & DeviceExt->CleanShutBitMask)
599 {
600 /* unset clean shutdown bit */
601 eocMark &= ~DeviceExt->CleanShutBitMask;
602 WriteCluster(DeviceExt, 1, eocMark);
603 VolumeFcb->Flags |= VCB_CLEAR_DIRTY;
604 }
605 }
606
607 VolumeFcb->Flags |= VCB_IS_DIRTY;
608
609 FsRtlNotifyVolumeEvent(DeviceExt->FATFileObject, FSRTL_VOLUME_MOUNT);
610 FsRtlNotifyInitializeSync(&DeviceExt->NotifySync);
611 InitializeListHead(&DeviceExt->NotifyList);
612
613 DPRINT("Mount success\n");
614
615 Status = STATUS_SUCCESS;
616
617 ByeBye:
618 if (!NT_SUCCESS(Status))
619 {
620 /* Cleanup */
621 if (DeviceExt && DeviceExt->FATFileObject)
622 ObDereferenceObject (DeviceExt->FATFileObject);
623 if (DeviceExt && DeviceExt->SpareVPB)
624 ExFreePoolWithTag(DeviceExt->SpareVPB, TAG_VFAT);
625 if (Fcb)
626 vfatDestroyFCB(Fcb);
627 if (Ccb)
628 vfatDestroyCCB(Ccb);
629 if (DeviceObject)
630 IoDeleteDevice(DeviceObject);
631 }
632
633 return Status;
634 }
635
636
637 /*
638 * FUNCTION: Verify the filesystem
639 */
640 static
641 NTSTATUS
642 VfatVerify(
643 PVFAT_IRP_CONTEXT IrpContext)
644 {
645 PDEVICE_OBJECT DeviceToVerify;
646 NTSTATUS Status = STATUS_SUCCESS;
647 FATINFO FatInfo;
648 BOOLEAN RecognizedFS;
649 PDEVICE_EXTENSION DeviceExt;
650
651 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext);
652
653 DeviceToVerify = IrpContext->Stack->Parameters.VerifyVolume.DeviceObject;
654 DeviceExt = DeviceToVerify->DeviceExtension;
655 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
656 IOCTL_DISK_CHECK_VERIFY,
657 NULL,
658 0,
659 NULL,
660 0,
661 TRUE);
662 if (!NT_SUCCESS(Status) && Status != STATUS_VERIFY_REQUIRED)
663 {
664 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status);
665 Status = STATUS_WRONG_VOLUME;
666 }
667 else
668 {
669 Status = VfatHasFileSystem(DeviceExt->StorageDevice, &RecognizedFS, &FatInfo, TRUE);
670 if (!NT_SUCCESS(Status) || RecognizedFS == FALSE)
671 {
672 Status = STATUS_WRONG_VOLUME;
673 }
674 else if (sizeof(FATINFO) == RtlCompareMemory(&FatInfo, &DeviceExt->FatInfo, sizeof(FATINFO)))
675 {
676 DPRINT1("Same volume\n");
677 /*
678 * FIXME:
679 * Preformatted floppy disks have very often a serial number of 0000:0000.
680 * We should calculate a crc sum over the sectors from the root directory as secondary volume number.
681 * Each write to the root directory must update this crc sum.
682 */
683 /* HACK */
684 if (!FatInfo.FixedMedia)
685 {
686 Status = STATUS_WRONG_VOLUME;
687 }
688 }
689 else
690 {
691 Status = STATUS_WRONG_VOLUME;
692 }
693 }
694
695 IrpContext->Stack->Parameters.VerifyVolume.Vpb->RealDevice->Flags &= ~DO_VERIFY_VOLUME;
696
697 return Status;
698 }
699
700
701 static
702 NTSTATUS
703 VfatGetVolumeBitmap(
704 PVFAT_IRP_CONTEXT IrpContext)
705 {
706 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext);
707 return STATUS_INVALID_DEVICE_REQUEST;
708 }
709
710
711 static
712 NTSTATUS
713 VfatGetRetrievalPointers(
714 PVFAT_IRP_CONTEXT IrpContext)
715 {
716 PIO_STACK_LOCATION Stack;
717 LARGE_INTEGER Vcn;
718 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers;
719 PFILE_OBJECT FileObject;
720 ULONG MaxExtentCount;
721 PVFATFCB Fcb;
722 PDEVICE_EXTENSION DeviceExt;
723 ULONG FirstCluster;
724 ULONG CurrentCluster;
725 ULONG LastCluster;
726 NTSTATUS Status;
727
728 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext);
729
730 DeviceExt = IrpContext->DeviceExt;
731 FileObject = IrpContext->FileObject;
732 Stack = IrpContext->Stack;
733 if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(STARTING_VCN_INPUT_BUFFER) ||
734 Stack->Parameters.DeviceIoControl.Type3InputBuffer == NULL)
735 {
736 return STATUS_INVALID_PARAMETER;
737 }
738
739 if (IrpContext->Irp->UserBuffer == NULL ||
740 Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(RETRIEVAL_POINTERS_BUFFER))
741 {
742 return STATUS_BUFFER_TOO_SMALL;
743 }
744
745 Fcb = FileObject->FsContext;
746
747 ExAcquireResourceSharedLite(&Fcb->MainResource, TRUE);
748
749 Vcn = ((PSTARTING_VCN_INPUT_BUFFER)Stack->Parameters.DeviceIoControl.Type3InputBuffer)->StartingVcn;
750 RetrievalPointers = IrpContext->Irp->UserBuffer;
751
752 MaxExtentCount = ((Stack->Parameters.DeviceIoControl.OutputBufferLength - sizeof(RetrievalPointers->ExtentCount) - sizeof(RetrievalPointers->StartingVcn)) / sizeof(RetrievalPointers->Extents[0]));
753
754 if (Vcn.QuadPart >= Fcb->RFCB.AllocationSize.QuadPart / DeviceExt->FatInfo.BytesPerCluster)
755 {
756 Status = STATUS_INVALID_PARAMETER;
757 goto ByeBye;
758 }
759
760 CurrentCluster = FirstCluster = vfatDirEntryGetFirstCluster(DeviceExt, &Fcb->entry);
761 Status = OffsetToCluster(DeviceExt, FirstCluster,
762 Vcn.u.LowPart * DeviceExt->FatInfo.BytesPerCluster,
763 &CurrentCluster, FALSE);
764 if (!NT_SUCCESS(Status))
765 {
766 goto ByeBye;
767 }
768
769 RetrievalPointers->StartingVcn = Vcn;
770 RetrievalPointers->ExtentCount = 0;
771 RetrievalPointers->Extents[0].Lcn.u.HighPart = 0;
772 RetrievalPointers->Extents[0].Lcn.u.LowPart = CurrentCluster - 2;
773 LastCluster = 0;
774 while (CurrentCluster != 0xffffffff && RetrievalPointers->ExtentCount < MaxExtentCount)
775 {
776 LastCluster = CurrentCluster;
777 Status = NextCluster(DeviceExt, CurrentCluster, &CurrentCluster, FALSE);
778 Vcn.QuadPart++;
779 if (!NT_SUCCESS(Status))
780 {
781 goto ByeBye;
782 }
783
784 if (LastCluster + 1 != CurrentCluster)
785 {
786 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].NextVcn = Vcn;
787 RetrievalPointers->ExtentCount++;
788 if (RetrievalPointers->ExtentCount < MaxExtentCount)
789 {
790 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.HighPart = 0;
791 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.LowPart = CurrentCluster - 2;
792 }
793 }
794 }
795
796 IrpContext->Irp->IoStatus.Information = sizeof(RETRIEVAL_POINTERS_BUFFER) + (sizeof(RetrievalPointers->Extents[0]) * (RetrievalPointers->ExtentCount - 1));
797 Status = STATUS_SUCCESS;
798
799 ByeBye:
800 ExReleaseResourceLite(&Fcb->MainResource);
801
802 return Status;
803 }
804
805 static
806 NTSTATUS
807 VfatMoveFile(
808 PVFAT_IRP_CONTEXT IrpContext)
809 {
810 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext);
811 return STATUS_INVALID_DEVICE_REQUEST;
812 }
813
814 static
815 NTSTATUS
816 VfatIsVolumeDirty(
817 PVFAT_IRP_CONTEXT IrpContext)
818 {
819 PULONG Flags;
820
821 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext);
822
823 if (IrpContext->Stack->Parameters.FileSystemControl.OutputBufferLength != sizeof(ULONG))
824 return STATUS_INVALID_BUFFER_SIZE;
825 else if (!IrpContext->Irp->AssociatedIrp.SystemBuffer)
826 return STATUS_INVALID_USER_BUFFER;
827
828 Flags = (PULONG)IrpContext->Irp->AssociatedIrp.SystemBuffer;
829 *Flags = 0;
830
831 if ((IrpContext->DeviceExt->VolumeFcb->Flags & VCB_IS_DIRTY) &&
832 !(IrpContext->DeviceExt->VolumeFcb->Flags & VCB_CLEAR_DIRTY))
833 {
834 *Flags |= VOLUME_IS_DIRTY;
835 }
836
837 return STATUS_SUCCESS;
838 }
839
840 static
841 NTSTATUS
842 VfatMarkVolumeDirty(
843 PVFAT_IRP_CONTEXT IrpContext)
844 {
845 ULONG eocMark;
846 PDEVICE_EXTENSION DeviceExt;
847 NTSTATUS Status = STATUS_SUCCESS;
848
849 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext);
850 DeviceExt = IrpContext->DeviceExt;
851
852 if (!(DeviceExt->VolumeFcb->Flags & VCB_IS_DIRTY))
853 {
854 Status = GetNextCluster(DeviceExt, 1, &eocMark);
855 if (NT_SUCCESS(Status))
856 {
857 /* unset clean shutdown bit */
858 eocMark &= ~DeviceExt->CleanShutBitMask;
859 Status = WriteCluster(DeviceExt, 1, eocMark);
860 }
861 }
862
863 DeviceExt->VolumeFcb->Flags &= ~VCB_CLEAR_DIRTY;
864
865 return Status;
866 }
867
868 static
869 NTSTATUS
870 VfatLockOrUnlockVolume(
871 PVFAT_IRP_CONTEXT IrpContext,
872 BOOLEAN Lock)
873 {
874 PFILE_OBJECT FileObject;
875 PDEVICE_EXTENSION DeviceExt;
876 PVFATFCB Fcb;
877
878 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext, Lock);
879
880 DeviceExt = IrpContext->DeviceExt;
881 FileObject = IrpContext->FileObject;
882 Fcb = FileObject->FsContext;
883
884 /* Only allow locking with the volume open */
885 if (!(Fcb->Flags & FCB_IS_VOLUME))
886 {
887 return STATUS_ACCESS_DENIED;
888 }
889
890 /* Bail out if it's already in the demanded state */
891 if (((DeviceExt->Flags & VCB_VOLUME_LOCKED) && Lock) ||
892 (!(DeviceExt->Flags & VCB_VOLUME_LOCKED) && !Lock))
893 {
894 return STATUS_ACCESS_DENIED;
895 }
896
897 /* Deny locking if we're not alone */
898 if (Lock && DeviceExt->OpenHandleCount != 1)
899 {
900 return STATUS_ACCESS_DENIED;
901 }
902
903 /* Finally, proceed */
904 if (Lock)
905 {
906 DeviceExt->Flags |= VCB_VOLUME_LOCKED;
907 }
908 else
909 {
910 DeviceExt->Flags &= ~VCB_VOLUME_LOCKED;
911 }
912
913 return STATUS_SUCCESS;
914 }
915
916 static
917 NTSTATUS
918 VfatDismountVolume(
919 PVFAT_IRP_CONTEXT IrpContext)
920 {
921 PDEVICE_EXTENSION DeviceExt;
922 PLIST_ENTRY NextEntry;
923 PVFATFCB Fcb;
924 PFILE_OBJECT FileObject;
925 ULONG eocMark;
926 NTSTATUS Status;
927
928 DPRINT("VfatDismountVolume(%p)\n", IrpContext);
929
930 DeviceExt = IrpContext->DeviceExt;
931 FileObject = IrpContext->FileObject;
932
933 /* We HAVE to be locked. Windows also allows dismount with no lock
934 * but we're here mainly for 1st stage, so KISS
935 */
936 if (!(DeviceExt->Flags & VCB_VOLUME_LOCKED))
937 {
938 return STATUS_ACCESS_DENIED;
939 }
940
941 /* Race condition? */
942 if (DeviceExt->Flags & VCB_DISMOUNT_PENDING)
943 {
944 return STATUS_VOLUME_DISMOUNTED;
945 }
946
947 /* Notify we'll dismount. Pass that point there's no reason we fail */
948 FsRtlNotifyVolumeEvent(IrpContext->Stack->FileObject, FSRTL_VOLUME_DISMOUNT);
949
950 ExAcquireResourceExclusiveLite(&DeviceExt->FatResource, TRUE);
951
952 if (DeviceExt->VolumeFcb->Flags & VCB_CLEAR_DIRTY)
953 {
954 /* Set clean shutdown bit */
955 Status = GetNextCluster(DeviceExt, 1, &eocMark);
956 if (NT_SUCCESS(Status))
957 {
958 eocMark |= DeviceExt->CleanShutBitMask;
959 if (NT_SUCCESS(WriteCluster(DeviceExt, 1, eocMark)))
960 DeviceExt->VolumeFcb->Flags &= ~VCB_IS_DIRTY;
961 }
962 }
963
964 /* Flush volume & files */
965 VfatFlushVolume(DeviceExt, (PVFATFCB)FileObject->FsContext);
966
967 /* Rebrowse the FCB in order to free them now */
968 while (!IsListEmpty(&DeviceExt->FcbListHead))
969 {
970 NextEntry = RemoveHeadList(&DeviceExt->FcbListHead);
971 Fcb = CONTAINING_RECORD(NextEntry, VFATFCB, FcbListEntry);
972 vfatDestroyFCB(Fcb);
973 }
974
975 /* Mark we're being dismounted */
976 DeviceExt->Flags |= VCB_DISMOUNT_PENDING;
977 #ifndef ENABLE_SWAPOUT
978 IrpContext->DeviceObject->Vpb->Flags &= ~VPB_MOUNTED;
979 #endif
980
981 ExReleaseResourceLite(&DeviceExt->FatResource);
982
983 /* Release a few resources and quit, we're done */
984 ExDeleteResourceLite(&DeviceExt->DirResource);
985 ExDeleteResourceLite(&DeviceExt->FatResource);
986 ObDereferenceObject(DeviceExt->FATFileObject);
987
988 return STATUS_SUCCESS;
989 }
990
991 /*
992 * FUNCTION: File system control
993 */
994 NTSTATUS
995 VfatFileSystemControl(
996 PVFAT_IRP_CONTEXT IrpContext)
997 {
998 NTSTATUS Status;
999
1000 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext);
1001
1002 ASSERT(IrpContext);
1003 ASSERT(IrpContext->Irp);
1004 ASSERT(IrpContext->Stack);
1005
1006 IrpContext->Irp->IoStatus.Information = 0;
1007
1008 switch (IrpContext->MinorFunction)
1009 {
1010 case IRP_MN_KERNEL_CALL:
1011 case IRP_MN_USER_FS_REQUEST:
1012 switch(IrpContext->Stack->Parameters.DeviceIoControl.IoControlCode)
1013 {
1014 case FSCTL_GET_VOLUME_BITMAP:
1015 Status = VfatGetVolumeBitmap(IrpContext);
1016 break;
1017
1018 case FSCTL_GET_RETRIEVAL_POINTERS:
1019 Status = VfatGetRetrievalPointers(IrpContext);
1020 break;
1021
1022 case FSCTL_MOVE_FILE:
1023 Status = VfatMoveFile(IrpContext);
1024 break;
1025
1026 case FSCTL_IS_VOLUME_DIRTY:
1027 Status = VfatIsVolumeDirty(IrpContext);
1028 break;
1029
1030 case FSCTL_MARK_VOLUME_DIRTY:
1031 Status = VfatMarkVolumeDirty(IrpContext);
1032 break;
1033
1034 case FSCTL_LOCK_VOLUME:
1035 Status = VfatLockOrUnlockVolume(IrpContext, TRUE);
1036 break;
1037
1038 case FSCTL_UNLOCK_VOLUME:
1039 Status = VfatLockOrUnlockVolume(IrpContext, FALSE);
1040 break;
1041
1042 case FSCTL_DISMOUNT_VOLUME:
1043 Status = VfatDismountVolume(IrpContext);
1044 break;
1045
1046 default:
1047 Status = STATUS_INVALID_DEVICE_REQUEST;
1048 }
1049 break;
1050
1051 case IRP_MN_MOUNT_VOLUME:
1052 Status = VfatMount(IrpContext);
1053 break;
1054
1055 case IRP_MN_VERIFY_VOLUME:
1056 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1057 Status = VfatVerify(IrpContext);
1058 break;
1059
1060 default:
1061 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext->MinorFunction);
1062 Status = STATUS_INVALID_DEVICE_REQUEST;
1063 break;
1064 }
1065
1066 return Status;
1067 }