[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 RtlCopyMemory(&FatInfo.VolumeLabel, &Boot->VolumeLabel, sizeof(FatInfo.VolumeLabel));
224 }
225 else if (FatInfo.NumberOfClusters >= 65525)
226 {
227 DPRINT("FAT32\n");
228 FatInfo.FatType = FAT32;
229 FatInfo.RootCluster = ((struct _BootSector32*) Boot)->RootCluster;
230 FatInfo.rootStart = FatInfo.dataStart + ((FatInfo.RootCluster - 2) * FatInfo.SectorsPerCluster);
231 FatInfo.VolumeID = ((struct _BootSector32*) Boot)->VolumeID;
232 RtlCopyMemory(&FatInfo.VolumeLabel, &((struct _BootSector32*)Boot)->VolumeLabel, sizeof(FatInfo.VolumeLabel));
233 }
234 else
235 {
236 DPRINT("FAT16\n");
237 FatInfo.FatType = FAT16;
238 FatInfo.RootCluster = FatInfo.rootStart / FatInfo.SectorsPerCluster;
239 RtlCopyMemory(&FatInfo.VolumeLabel, &Boot->VolumeLabel, sizeof(FatInfo.VolumeLabel));
240 }
241
242 if (PartitionInfoIsValid &&
243 FatInfo.Sectors > PartitionInfo.PartitionLength.QuadPart / FatInfo.BytesPerSector)
244 {
245 *RecognizedFS = FALSE;
246 }
247
248 if (pFatInfo && *RecognizedFS)
249 {
250 *pFatInfo = FatInfo;
251 }
252 }
253 }
254
255 ExFreePool(Boot);
256 }
257
258 if (!*RecognizedFS && PartitionInfoIsValid)
259 {
260 BootFatX = ExAllocatePoolWithTag(NonPagedPool, sizeof(struct _BootSectorFatX), TAG_VFAT);
261 if (BootFatX == NULL)
262 {
263 *RecognizedFS=FALSE;
264 return STATUS_INSUFFICIENT_RESOURCES;
265 }
266
267 Offset.QuadPart = 0;
268
269 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
270 Status = VfatReadDisk(DeviceToMount, &Offset, sizeof(struct _BootSectorFatX), (PUCHAR) BootFatX, Override);
271 if (NT_SUCCESS(Status))
272 {
273 *RecognizedFS = TRUE;
274 if (BootFatX->SysType[0] != 'F' ||
275 BootFatX->SysType[1] != 'A' ||
276 BootFatX->SysType[2] != 'T' ||
277 BootFatX->SysType[3] != 'X')
278 {
279 DPRINT1("SysType %c%c%c%c\n", BootFatX->SysType[0], BootFatX->SysType[1], BootFatX->SysType[2], BootFatX->SysType[3]);
280 *RecognizedFS=FALSE;
281 }
282
283 if (*RecognizedFS &&
284 BootFatX->SectorsPerCluster != 1 &&
285 BootFatX->SectorsPerCluster != 2 &&
286 BootFatX->SectorsPerCluster != 4 &&
287 BootFatX->SectorsPerCluster != 8 &&
288 BootFatX->SectorsPerCluster != 16 &&
289 BootFatX->SectorsPerCluster != 32 &&
290 BootFatX->SectorsPerCluster != 64 &&
291 BootFatX->SectorsPerCluster != 128)
292 {
293 DPRINT1("SectorsPerCluster %lu\n", BootFatX->SectorsPerCluster);
294 *RecognizedFS=FALSE;
295 }
296
297 if (*RecognizedFS)
298 {
299 FatInfo.BytesPerSector = DiskGeometry.BytesPerSector;
300 FatInfo.SectorsPerCluster = BootFatX->SectorsPerCluster;
301 FatInfo.rootDirectorySectors = BootFatX->SectorsPerCluster;
302 FatInfo.BytesPerCluster = BootFatX->SectorsPerCluster * DiskGeometry.BytesPerSector;
303 FatInfo.Sectors = (ULONG)(PartitionInfo.PartitionLength.QuadPart / DiskGeometry.BytesPerSector);
304 if (FatInfo.Sectors / FatInfo.SectorsPerCluster < 65525)
305 {
306 DPRINT("FATX16\n");
307 FatInfo.FatType = FATX16;
308 }
309 else
310 {
311 DPRINT("FATX32\n");
312 FatInfo.FatType = FATX32;
313 }
314 FatInfo.VolumeID = BootFatX->VolumeID;
315 FatInfo.FATStart = sizeof(struct _BootSectorFatX) / DiskGeometry.BytesPerSector;
316 FatInfo.FATCount = BootFatX->FATCount;
317 FatInfo.FATSectors =
318 ROUND_UP(FatInfo.Sectors / FatInfo.SectorsPerCluster * (FatInfo.FatType == FATX16 ? 2 : 4), 4096) /
319 FatInfo.BytesPerSector;
320 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
321 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
322 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
323 FatInfo.NumberOfClusters = (FatInfo.Sectors - FatInfo.dataStart) / FatInfo.SectorsPerCluster;
324
325 if (pFatInfo && *RecognizedFS)
326 {
327 *pFatInfo = FatInfo;
328 }
329 }
330 }
331 ExFreePool(BootFatX);
332 }
333
334 DPRINT("VfatHasFileSystem done\n");
335 return Status;
336 }
337
338
339 /*
340 * FUNCTION: Mount the filesystem
341 */
342 static
343 NTSTATUS
344 VfatMount(
345 PVFAT_IRP_CONTEXT IrpContext)
346 {
347 PDEVICE_OBJECT DeviceObject = NULL;
348 PDEVICE_EXTENSION DeviceExt = NULL;
349 BOOLEAN RecognizedFS;
350 NTSTATUS Status;
351 PVFATFCB Fcb = NULL;
352 PVFATFCB VolumeFcb = NULL;
353 PVFATCCB Ccb = NULL;
354 PDEVICE_OBJECT DeviceToMount;
355 PVPB Vpb;
356 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\$$Fat$$");
357 UNICODE_STRING VolumeNameU = RTL_CONSTANT_STRING(L"\\$$Volume$$");
358 ULONG HashTableSize;
359 ULONG eocMark;
360 FATINFO FatInfo;
361
362 DPRINT("VfatMount(IrpContext %p)\n", IrpContext);
363
364 ASSERT(IrpContext);
365
366 if (IrpContext->DeviceObject != VfatGlobalData->DeviceObject)
367 {
368 Status = STATUS_INVALID_DEVICE_REQUEST;
369 goto ByeBye;
370 }
371
372 DeviceToMount = IrpContext->Stack->Parameters.MountVolume.DeviceObject;
373 Vpb = IrpContext->Stack->Parameters.MountVolume.Vpb;
374
375 Status = VfatHasFileSystem(DeviceToMount, &RecognizedFS, &FatInfo, FALSE);
376 if (!NT_SUCCESS(Status))
377 {
378 goto ByeBye;
379 }
380
381 if (RecognizedFS == FALSE)
382 {
383 DPRINT("VFAT: Unrecognized Volume\n");
384 Status = STATUS_UNRECOGNIZED_VOLUME;
385 goto ByeBye;
386 }
387
388 /* Use prime numbers for the table size */
389 if (FatInfo.FatType == FAT12)
390 {
391 HashTableSize = 4099; // 4096 = 4 * 1024
392 }
393 else if (FatInfo.FatType == FAT16 ||
394 FatInfo.FatType == FATX16)
395 {
396 HashTableSize = 16411; // 16384 = 16 * 1024
397 }
398 else
399 {
400 HashTableSize = 65537; // 65536 = 64 * 1024;
401 }
402 DPRINT("VFAT: Recognized volume\n");
403 Status = IoCreateDevice(VfatGlobalData->DriverObject,
404 ROUND_UP(sizeof (DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize,
405 NULL,
406 FILE_DEVICE_DISK_FILE_SYSTEM,
407 DeviceToMount->Characteristics,
408 FALSE,
409 &DeviceObject);
410 if (!NT_SUCCESS(Status))
411 {
412 goto ByeBye;
413 }
414
415 DeviceExt = DeviceObject->DeviceExtension;
416 RtlZeroMemory(DeviceExt, ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize);
417 DeviceExt->FcbHashTable = (HASHENTRY**)((ULONG_PTR)DeviceExt + ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)));
418 DeviceExt->HashTableSize = HashTableSize;
419 DeviceExt->VolumeDevice = DeviceObject;
420
421 /* use same vpb as device disk */
422 DeviceObject->Vpb = Vpb;
423 DeviceToMount->Vpb = Vpb;
424
425 RtlCopyMemory(&DeviceExt->FatInfo, &FatInfo, sizeof(FATINFO));
426
427 DPRINT("BytesPerSector: %u\n", DeviceExt->FatInfo.BytesPerSector);
428 DPRINT("SectorsPerCluster: %u\n", DeviceExt->FatInfo.SectorsPerCluster);
429 DPRINT("FATCount: %u\n", DeviceExt->FatInfo.FATCount);
430 DPRINT("FATSectors: %u\n", DeviceExt->FatInfo.FATSectors);
431 DPRINT("RootStart: %u\n", DeviceExt->FatInfo.rootStart);
432 DPRINT("DataStart: %u\n", DeviceExt->FatInfo.dataStart);
433 if (DeviceExt->FatInfo.FatType == FAT32)
434 {
435 DPRINT("RootCluster: %u\n", DeviceExt->FatInfo.RootCluster);
436 }
437
438 switch (DeviceExt->FatInfo.FatType)
439 {
440 case FAT12:
441 DeviceExt->GetNextCluster = FAT12GetNextCluster;
442 DeviceExt->FindAndMarkAvailableCluster = FAT12FindAndMarkAvailableCluster;
443 DeviceExt->WriteCluster = FAT12WriteCluster;
444 DeviceExt->CleanShutBitMask = 0;
445 break;
446
447 case FAT16:
448 case FATX16:
449 DeviceExt->GetNextCluster = FAT16GetNextCluster;
450 DeviceExt->FindAndMarkAvailableCluster = FAT16FindAndMarkAvailableCluster;
451 DeviceExt->WriteCluster = FAT16WriteCluster;
452 DeviceExt->CleanShutBitMask = 0x8000;
453 break;
454
455 case FAT32:
456 case FATX32:
457 DeviceExt->GetNextCluster = FAT32GetNextCluster;
458 DeviceExt->FindAndMarkAvailableCluster = FAT32FindAndMarkAvailableCluster;
459 DeviceExt->WriteCluster = FAT32WriteCluster;
460 DeviceExt->CleanShutBitMask = 0x80000000;
461 break;
462 }
463
464 if (DeviceExt->FatInfo.FatType == FATX16 ||
465 DeviceExt->FatInfo.FatType == FATX32)
466 {
467 DeviceExt->Flags |= VCB_IS_FATX;
468 DeviceExt->GetNextDirEntry = FATXGetNextDirEntry;
469 DeviceExt->BaseDateYear = 2000;
470 }
471 else
472 {
473 DeviceExt->GetNextDirEntry = FATGetNextDirEntry;
474 DeviceExt->BaseDateYear = 1980;
475 }
476
477 DeviceExt->StorageDevice = DeviceToMount;
478 DeviceExt->StorageDevice->Vpb->DeviceObject = DeviceObject;
479 DeviceExt->StorageDevice->Vpb->RealDevice = DeviceExt->StorageDevice;
480 DeviceExt->StorageDevice->Vpb->Flags |= VPB_MOUNTED;
481 DeviceObject->StackSize = DeviceExt->StorageDevice->StackSize + 1;
482 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
483
484 DPRINT("FsDeviceObject %p\n", DeviceObject);
485
486 /* Initialize this resource early ... it's used in VfatCleanup */
487 ExInitializeResourceLite(&DeviceExt->DirResource);
488
489 DeviceExt->IoVPB = DeviceObject->Vpb;
490 DeviceExt->SpareVPB = ExAllocatePoolWithTag(NonPagedPool, sizeof(VPB), TAG_VFAT);
491 if (DeviceExt->SpareVPB == NULL)
492 {
493 Status = STATUS_INSUFFICIENT_RESOURCES;
494 goto ByeBye;
495 }
496
497 DeviceExt->FATFileObject = IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
498 Fcb = vfatNewFCB(DeviceExt, &NameU);
499 if (Fcb == NULL)
500 {
501 Status = STATUS_INSUFFICIENT_RESOURCES;
502 goto ByeBye;
503 }
504
505 Ccb = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
506 if (Ccb == NULL)
507 {
508 Status = STATUS_INSUFFICIENT_RESOURCES;
509 goto ByeBye;
510 }
511
512 RtlZeroMemory(Ccb, sizeof (VFATCCB));
513 DeviceExt->FATFileObject->FsContext = Fcb;
514 DeviceExt->FATFileObject->FsContext2 = Ccb;
515 DeviceExt->FATFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
516 DeviceExt->FATFileObject->PrivateCacheMap = NULL;
517 DeviceExt->FATFileObject->Vpb = DeviceObject->Vpb;
518 Fcb->FileObject = DeviceExt->FATFileObject;
519
520 Fcb->Flags |= FCB_IS_FAT;
521
522 Fcb->RFCB.FileSize.QuadPart = DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector;
523 Fcb->RFCB.ValidDataLength = Fcb->RFCB.FileSize;
524 Fcb->RFCB.AllocationSize = Fcb->RFCB.FileSize;
525
526 _SEH2_TRY
527 {
528 CcInitializeCacheMap(DeviceExt->FATFileObject,
529 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
530 TRUE,
531 &VfatGlobalData->CacheMgrCallbacks,
532 Fcb);
533 }
534 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
535 {
536 Status = _SEH2_GetExceptionCode();
537 goto ByeBye;
538 }
539 _SEH2_END;
540
541 DeviceExt->LastAvailableCluster = 2;
542 ExInitializeResourceLite(&DeviceExt->FatResource);
543
544 InitializeListHead(&DeviceExt->FcbListHead);
545
546 VolumeFcb = vfatNewFCB(DeviceExt, &VolumeNameU);
547 if (VolumeFcb == NULL)
548 {
549 Status = STATUS_INSUFFICIENT_RESOURCES;
550 goto ByeBye;
551 }
552
553 VolumeFcb->Flags = FCB_IS_VOLUME;
554 VolumeFcb->RFCB.FileSize.QuadPart = (LONGLONG) DeviceExt->FatInfo.Sectors * DeviceExt->FatInfo.BytesPerSector;
555 VolumeFcb->RFCB.ValidDataLength = VolumeFcb->RFCB.FileSize;
556 VolumeFcb->RFCB.AllocationSize = VolumeFcb->RFCB.FileSize;
557 DeviceExt->VolumeFcb = VolumeFcb;
558
559 ExAcquireResourceExclusiveLite(&VfatGlobalData->VolumeListLock, TRUE);
560 InsertHeadList(&VfatGlobalData->VolumeListHead, &DeviceExt->VolumeListEntry);
561 ExReleaseResourceLite(&VfatGlobalData->VolumeListLock);
562
563 /* read serial number */
564 DeviceObject->Vpb->SerialNumber = DeviceExt->FatInfo.VolumeID;
565
566 /* read volume label */
567 ReadVolumeLabel(DeviceExt, DeviceObject->Vpb);
568
569 /* read clean shutdown bit status */
570 Status = GetNextCluster(DeviceExt, 1, &eocMark);
571 if (NT_SUCCESS(Status))
572 {
573 if (eocMark & DeviceExt->CleanShutBitMask)
574 {
575 /* unset clean shutdown bit */
576 eocMark &= ~DeviceExt->CleanShutBitMask;
577 WriteCluster(DeviceExt, 1, eocMark);
578 VolumeFcb->Flags |= VCB_CLEAR_DIRTY;
579 }
580 }
581
582 VolumeFcb->Flags |= VCB_IS_DIRTY;
583
584 FsRtlNotifyVolumeEvent(DeviceExt->FATFileObject, FSRTL_VOLUME_MOUNT);
585 FsRtlNotifyInitializeSync(&DeviceExt->NotifySync);
586 InitializeListHead(&DeviceExt->NotifyList);
587
588 DPRINT("Mount success\n");
589
590 Status = STATUS_SUCCESS;
591
592 ByeBye:
593 if (!NT_SUCCESS(Status))
594 {
595 /* Cleanup */
596 if (DeviceExt && DeviceExt->FATFileObject)
597 ObDereferenceObject (DeviceExt->FATFileObject);
598 if (DeviceExt && DeviceExt->SpareVPB)
599 ExFreePoolWithTag(DeviceExt->SpareVPB, TAG_VFAT);
600 if (Fcb)
601 vfatDestroyFCB(Fcb);
602 if (Ccb)
603 vfatDestroyCCB(Ccb);
604 if (DeviceObject)
605 IoDeleteDevice(DeviceObject);
606 }
607
608 return Status;
609 }
610
611
612 /*
613 * FUNCTION: Verify the filesystem
614 */
615 static
616 NTSTATUS
617 VfatVerify(
618 PVFAT_IRP_CONTEXT IrpContext)
619 {
620 PDEVICE_OBJECT DeviceToVerify;
621 NTSTATUS Status = STATUS_SUCCESS;
622 FATINFO FatInfo;
623 BOOLEAN RecognizedFS;
624 PDEVICE_EXTENSION DeviceExt;
625
626 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext);
627
628 DeviceToVerify = IrpContext->Stack->Parameters.VerifyVolume.DeviceObject;
629 DeviceExt = DeviceToVerify->DeviceExtension;
630 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
631 IOCTL_DISK_CHECK_VERIFY,
632 NULL,
633 0,
634 NULL,
635 0,
636 TRUE);
637 if (!NT_SUCCESS(Status) && Status != STATUS_VERIFY_REQUIRED)
638 {
639 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status);
640 Status = STATUS_WRONG_VOLUME;
641 }
642 else
643 {
644 Status = VfatHasFileSystem(DeviceExt->StorageDevice, &RecognizedFS, &FatInfo, TRUE);
645 if (!NT_SUCCESS(Status) || RecognizedFS == FALSE)
646 {
647 Status = STATUS_WRONG_VOLUME;
648 }
649 else if (sizeof(FATINFO) == RtlCompareMemory(&FatInfo, &DeviceExt->FatInfo, sizeof(FATINFO)))
650 {
651 DPRINT1("Same volume\n");
652 /*
653 * FIXME:
654 * Preformatted floppy disks have very often a serial number of 0000:0000.
655 * We should calculate a crc sum over the sectors from the root directory as secondary volume number.
656 * Each write to the root directory must update this crc sum.
657 */
658 /* HACK */
659 if (!FatInfo.FixedMedia && FatInfo.FatType >= FATX16)
660 {
661 Status = STATUS_WRONG_VOLUME;
662 }
663 }
664 else
665 {
666 Status = STATUS_WRONG_VOLUME;
667 }
668 }
669
670 IrpContext->Stack->Parameters.VerifyVolume.Vpb->RealDevice->Flags &= ~DO_VERIFY_VOLUME;
671
672 return Status;
673 }
674
675
676 static
677 NTSTATUS
678 VfatGetVolumeBitmap(
679 PVFAT_IRP_CONTEXT IrpContext)
680 {
681 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext);
682 return STATUS_INVALID_DEVICE_REQUEST;
683 }
684
685
686 static
687 NTSTATUS
688 VfatGetRetrievalPointers(
689 PVFAT_IRP_CONTEXT IrpContext)
690 {
691 PIO_STACK_LOCATION Stack;
692 LARGE_INTEGER Vcn;
693 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers;
694 PFILE_OBJECT FileObject;
695 ULONG MaxExtentCount;
696 PVFATFCB Fcb;
697 PDEVICE_EXTENSION DeviceExt;
698 ULONG FirstCluster;
699 ULONG CurrentCluster;
700 ULONG LastCluster;
701 NTSTATUS Status;
702
703 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext);
704
705 DeviceExt = IrpContext->DeviceExt;
706 FileObject = IrpContext->FileObject;
707 Stack = IrpContext->Stack;
708 if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(STARTING_VCN_INPUT_BUFFER) ||
709 Stack->Parameters.DeviceIoControl.Type3InputBuffer == NULL)
710 {
711 return STATUS_INVALID_PARAMETER;
712 }
713
714 if (IrpContext->Irp->UserBuffer == NULL ||
715 Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(RETRIEVAL_POINTERS_BUFFER))
716 {
717 return STATUS_BUFFER_TOO_SMALL;
718 }
719
720 Fcb = FileObject->FsContext;
721
722 ExAcquireResourceSharedLite(&Fcb->MainResource, TRUE);
723
724 Vcn = ((PSTARTING_VCN_INPUT_BUFFER)Stack->Parameters.DeviceIoControl.Type3InputBuffer)->StartingVcn;
725 RetrievalPointers = IrpContext->Irp->UserBuffer;
726
727 MaxExtentCount = ((Stack->Parameters.DeviceIoControl.OutputBufferLength - sizeof(RetrievalPointers->ExtentCount) - sizeof(RetrievalPointers->StartingVcn)) / sizeof(RetrievalPointers->Extents[0]));
728
729 if (Vcn.QuadPart >= Fcb->RFCB.AllocationSize.QuadPart / DeviceExt->FatInfo.BytesPerCluster)
730 {
731 Status = STATUS_INVALID_PARAMETER;
732 goto ByeBye;
733 }
734
735 CurrentCluster = FirstCluster = vfatDirEntryGetFirstCluster(DeviceExt, &Fcb->entry);
736 Status = OffsetToCluster(DeviceExt, FirstCluster,
737 Vcn.u.LowPart * DeviceExt->FatInfo.BytesPerCluster,
738 &CurrentCluster, FALSE);
739 if (!NT_SUCCESS(Status))
740 {
741 goto ByeBye;
742 }
743
744 RetrievalPointers->StartingVcn = Vcn;
745 RetrievalPointers->ExtentCount = 0;
746 RetrievalPointers->Extents[0].Lcn.u.HighPart = 0;
747 RetrievalPointers->Extents[0].Lcn.u.LowPart = CurrentCluster - 2;
748 LastCluster = 0;
749 while (CurrentCluster != 0xffffffff && RetrievalPointers->ExtentCount < MaxExtentCount)
750 {
751 LastCluster = CurrentCluster;
752 Status = NextCluster(DeviceExt, CurrentCluster, &CurrentCluster, FALSE);
753 Vcn.QuadPart++;
754 if (!NT_SUCCESS(Status))
755 {
756 goto ByeBye;
757 }
758
759 if (LastCluster + 1 != CurrentCluster)
760 {
761 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].NextVcn = Vcn;
762 RetrievalPointers->ExtentCount++;
763 if (RetrievalPointers->ExtentCount < MaxExtentCount)
764 {
765 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.HighPart = 0;
766 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.LowPart = CurrentCluster - 2;
767 }
768 }
769 }
770
771 IrpContext->Irp->IoStatus.Information = sizeof(RETRIEVAL_POINTERS_BUFFER) + (sizeof(RetrievalPointers->Extents[0]) * (RetrievalPointers->ExtentCount - 1));
772 Status = STATUS_SUCCESS;
773
774 ByeBye:
775 ExReleaseResourceLite(&Fcb->MainResource);
776
777 return Status;
778 }
779
780 static
781 NTSTATUS
782 VfatMoveFile(
783 PVFAT_IRP_CONTEXT IrpContext)
784 {
785 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext);
786 return STATUS_INVALID_DEVICE_REQUEST;
787 }
788
789 static
790 NTSTATUS
791 VfatIsVolumeDirty(
792 PVFAT_IRP_CONTEXT IrpContext)
793 {
794 PULONG Flags;
795
796 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext);
797
798 if (IrpContext->Stack->Parameters.FileSystemControl.OutputBufferLength != sizeof(ULONG))
799 return STATUS_INVALID_BUFFER_SIZE;
800 else if (!IrpContext->Irp->AssociatedIrp.SystemBuffer)
801 return STATUS_INVALID_USER_BUFFER;
802
803 Flags = (PULONG)IrpContext->Irp->AssociatedIrp.SystemBuffer;
804 *Flags = 0;
805
806 if ((IrpContext->DeviceExt->VolumeFcb->Flags & VCB_IS_DIRTY) &&
807 !(IrpContext->DeviceExt->VolumeFcb->Flags & VCB_CLEAR_DIRTY))
808 {
809 *Flags |= VOLUME_IS_DIRTY;
810 }
811
812 return STATUS_SUCCESS;
813 }
814
815 static
816 NTSTATUS
817 VfatMarkVolumeDirty(
818 PVFAT_IRP_CONTEXT IrpContext)
819 {
820 ULONG eocMark;
821 PDEVICE_EXTENSION DeviceExt;
822 NTSTATUS Status = STATUS_SUCCESS;
823
824 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext);
825 DeviceExt = IrpContext->DeviceExt;
826
827 if (!(DeviceExt->VolumeFcb->Flags & VCB_IS_DIRTY))
828 {
829 Status = GetNextCluster(DeviceExt, 1, &eocMark);
830 if (NT_SUCCESS(Status))
831 {
832 /* unset clean shutdown bit */
833 eocMark &= ~DeviceExt->CleanShutBitMask;
834 Status = WriteCluster(DeviceExt, 1, eocMark);
835 }
836 }
837
838 DeviceExt->VolumeFcb->Flags &= ~VCB_CLEAR_DIRTY;
839
840 return Status;
841 }
842
843 static
844 NTSTATUS
845 VfatLockOrUnlockVolume(
846 PVFAT_IRP_CONTEXT IrpContext,
847 BOOLEAN Lock)
848 {
849 PFILE_OBJECT FileObject;
850 PDEVICE_EXTENSION DeviceExt;
851 PVFATFCB Fcb;
852
853 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext, Lock);
854
855 DeviceExt = IrpContext->DeviceExt;
856 FileObject = IrpContext->FileObject;
857 Fcb = FileObject->FsContext;
858
859 /* Only allow locking with the volume open */
860 if (!(Fcb->Flags & FCB_IS_VOLUME))
861 {
862 return STATUS_ACCESS_DENIED;
863 }
864
865 /* Bail out if it's already in the demanded state */
866 if (((DeviceExt->Flags & VCB_VOLUME_LOCKED) && Lock) ||
867 (!(DeviceExt->Flags & VCB_VOLUME_LOCKED) && !Lock))
868 {
869 return STATUS_ACCESS_DENIED;
870 }
871
872 /* Deny locking if we're not alone */
873 if (Lock && DeviceExt->OpenHandleCount != 1)
874 {
875 return STATUS_ACCESS_DENIED;
876 }
877
878 /* Finally, proceed */
879 if (Lock)
880 {
881 DeviceExt->Flags |= VCB_VOLUME_LOCKED;
882 }
883 else
884 {
885 DeviceExt->Flags &= ~VCB_VOLUME_LOCKED;
886 }
887
888 return STATUS_SUCCESS;
889 }
890
891 static
892 NTSTATUS
893 VfatDismountVolume(
894 PVFAT_IRP_CONTEXT IrpContext)
895 {
896 PDEVICE_EXTENSION DeviceExt;
897 PLIST_ENTRY NextEntry;
898 PVFATFCB Fcb;
899 PFILE_OBJECT FileObject;
900 ULONG eocMark;
901 NTSTATUS Status;
902
903 DPRINT("VfatDismountVolume(%p)\n", IrpContext);
904
905 DeviceExt = IrpContext->DeviceExt;
906 FileObject = IrpContext->FileObject;
907
908 /* We HAVE to be locked. Windows also allows dismount with no lock
909 * but we're here mainly for 1st stage, so KISS
910 */
911 if (!(DeviceExt->Flags & VCB_VOLUME_LOCKED))
912 {
913 return STATUS_ACCESS_DENIED;
914 }
915
916 /* Race condition? */
917 if (DeviceExt->Flags & VCB_DISMOUNT_PENDING)
918 {
919 return STATUS_VOLUME_DISMOUNTED;
920 }
921
922 /* Notify we'll dismount. Pass that point there's no reason we fail */
923 FsRtlNotifyVolumeEvent(IrpContext->Stack->FileObject, FSRTL_VOLUME_DISMOUNT);
924
925 ExAcquireResourceExclusiveLite(&DeviceExt->FatResource, TRUE);
926
927 if (DeviceExt->VolumeFcb->Flags & VCB_CLEAR_DIRTY)
928 {
929 /* Set clean shutdown bit */
930 Status = GetNextCluster(DeviceExt, 1, &eocMark);
931 if (NT_SUCCESS(Status))
932 {
933 eocMark |= DeviceExt->CleanShutBitMask;
934 if (NT_SUCCESS(WriteCluster(DeviceExt, 1, eocMark)))
935 DeviceExt->VolumeFcb->Flags &= ~VCB_IS_DIRTY;
936 }
937 }
938
939 /* Flush volume & files */
940 VfatFlushVolume(DeviceExt, (PVFATFCB)FileObject->FsContext);
941
942 /* Rebrowse the FCB in order to free them now */
943 while (!IsListEmpty(&DeviceExt->FcbListHead))
944 {
945 NextEntry = RemoveHeadList(&DeviceExt->FcbListHead);
946 Fcb = CONTAINING_RECORD(NextEntry, VFATFCB, FcbListEntry);
947 vfatDestroyFCB(Fcb);
948 }
949
950 /* Mark we're being dismounted */
951 DeviceExt->Flags |= VCB_DISMOUNT_PENDING;
952 #ifndef ENABLE_SWAPOUT
953 IrpContext->DeviceObject->Vpb->Flags &= ~VPB_MOUNTED;
954 #endif
955
956 ExReleaseResourceLite(&DeviceExt->FatResource);
957
958 /* Release a few resources and quit, we're done */
959 ExDeleteResourceLite(&DeviceExt->DirResource);
960 ExDeleteResourceLite(&DeviceExt->FatResource);
961 ObDereferenceObject(DeviceExt->FATFileObject);
962
963 return STATUS_SUCCESS;
964 }
965
966 /*
967 * FUNCTION: File system control
968 */
969 NTSTATUS
970 VfatFileSystemControl(
971 PVFAT_IRP_CONTEXT IrpContext)
972 {
973 NTSTATUS Status;
974
975 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext);
976
977 ASSERT(IrpContext);
978 ASSERT(IrpContext->Irp);
979 ASSERT(IrpContext->Stack);
980
981 IrpContext->Irp->IoStatus.Information = 0;
982
983 switch (IrpContext->MinorFunction)
984 {
985 case IRP_MN_KERNEL_CALL:
986 case IRP_MN_USER_FS_REQUEST:
987 switch(IrpContext->Stack->Parameters.DeviceIoControl.IoControlCode)
988 {
989 case FSCTL_GET_VOLUME_BITMAP:
990 Status = VfatGetVolumeBitmap(IrpContext);
991 break;
992
993 case FSCTL_GET_RETRIEVAL_POINTERS:
994 Status = VfatGetRetrievalPointers(IrpContext);
995 break;
996
997 case FSCTL_MOVE_FILE:
998 Status = VfatMoveFile(IrpContext);
999 break;
1000
1001 case FSCTL_IS_VOLUME_DIRTY:
1002 Status = VfatIsVolumeDirty(IrpContext);
1003 break;
1004
1005 case FSCTL_MARK_VOLUME_DIRTY:
1006 Status = VfatMarkVolumeDirty(IrpContext);
1007 break;
1008
1009 case FSCTL_LOCK_VOLUME:
1010 Status = VfatLockOrUnlockVolume(IrpContext, TRUE);
1011 break;
1012
1013 case FSCTL_UNLOCK_VOLUME:
1014 Status = VfatLockOrUnlockVolume(IrpContext, FALSE);
1015 break;
1016
1017 case FSCTL_DISMOUNT_VOLUME:
1018 Status = VfatDismountVolume(IrpContext);
1019 break;
1020
1021 default:
1022 Status = STATUS_INVALID_DEVICE_REQUEST;
1023 }
1024 break;
1025
1026 case IRP_MN_MOUNT_VOLUME:
1027 Status = VfatMount(IrpContext);
1028 break;
1029
1030 case IRP_MN_VERIFY_VOLUME:
1031 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1032 Status = VfatVerify(IrpContext);
1033 break;
1034
1035 default:
1036 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext->MinorFunction);
1037 Status = STATUS_INVALID_DEVICE_REQUEST;
1038 break;
1039 }
1040
1041 return Status;
1042 }