[FASTFAT] Fix size checking in VfatGetFileNameInformation()
[reactos.git] / 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 extern VFAT_DISPATCH FatXDispatch;
34 extern VFAT_DISPATCH FatDispatch;
35
36 /* FUNCTIONS ****************************************************************/
37
38 #define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
39 (pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
40
41 static
42 NTSTATUS
43 VfatHasFileSystem(
44 PDEVICE_OBJECT DeviceToMount,
45 PBOOLEAN RecognizedFS,
46 PFATINFO pFatInfo,
47 BOOLEAN Override)
48 {
49 NTSTATUS Status;
50 PARTITION_INFORMATION PartitionInfo;
51 DISK_GEOMETRY DiskGeometry;
52 FATINFO FatInfo;
53 ULONG Size;
54 ULONG Sectors;
55 LARGE_INTEGER Offset;
56 struct _BootSector* Boot;
57 struct _BootSectorFatX* BootFatX;
58 BOOLEAN PartitionInfoIsValid = FALSE;
59
60 DPRINT("VfatHasFileSystem\n");
61
62 *RecognizedFS = FALSE;
63
64 Size = sizeof(DISK_GEOMETRY);
65 Status = VfatBlockDeviceIoControl(DeviceToMount,
66 IOCTL_DISK_GET_DRIVE_GEOMETRY,
67 NULL,
68 0,
69 &DiskGeometry,
70 &Size,
71 Override);
72 if (!NT_SUCCESS(Status))
73 {
74 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status);
75 return Status;
76 }
77
78 FatInfo.FixedMedia = DiskGeometry.MediaType == FixedMedia ? TRUE : FALSE;
79 if (DiskGeometry.MediaType == FixedMedia || DiskGeometry.MediaType == RemovableMedia)
80 {
81 // We have found a hard disk
82 Size = sizeof(PARTITION_INFORMATION);
83 Status = VfatBlockDeviceIoControl(DeviceToMount,
84 IOCTL_DISK_GET_PARTITION_INFO,
85 NULL,
86 0,
87 &PartitionInfo,
88 &Size,
89 Override);
90 if (!NT_SUCCESS(Status))
91 {
92 DPRINT("VfatBlockDeviceIoControl failed (%x)\n", Status);
93 return Status;
94 }
95
96 DPRINT("Partition Information:\n");
97 DPRINT("StartingOffset %I64x\n", PartitionInfo.StartingOffset.QuadPart / 512);
98 DPRINT("PartitionLength %I64x\n", PartitionInfo.PartitionLength.QuadPart / 512);
99 DPRINT("HiddenSectors %u\n", PartitionInfo.HiddenSectors);
100 DPRINT("PartitionNumber %u\n", PartitionInfo.PartitionNumber);
101 DPRINT("PartitionType %u\n", PartitionInfo.PartitionType);
102 DPRINT("BootIndicator %u\n", PartitionInfo.BootIndicator);
103 DPRINT("RecognizedPartition %u\n", PartitionInfo.RecognizedPartition);
104 DPRINT("RewritePartition %u\n", PartitionInfo.RewritePartition);
105 if (PartitionInfo.PartitionType)
106 {
107 if (PartitionInfo.PartitionType == PARTITION_FAT_12 ||
108 PartitionInfo.PartitionType == PARTITION_FAT_16 ||
109 PartitionInfo.PartitionType == PARTITION_HUGE ||
110 PartitionInfo.PartitionType == PARTITION_FAT32 ||
111 PartitionInfo.PartitionType == PARTITION_FAT32_XINT13 ||
112 PartitionInfo.PartitionType == PARTITION_XINT13)
113 {
114 PartitionInfoIsValid = TRUE;
115 *RecognizedFS = TRUE;
116 }
117 }
118 else if (DiskGeometry.MediaType == RemovableMedia &&
119 PartitionInfo.PartitionNumber > 0 &&
120 PartitionInfo.StartingOffset.QuadPart == 0 &&
121 PartitionInfo.PartitionLength.QuadPart > 0)
122 {
123 /* This is possible a removable media formated as super floppy */
124 PartitionInfoIsValid = TRUE;
125 *RecognizedFS = TRUE;
126 }
127 }
128 else
129 {
130 *RecognizedFS = TRUE;
131 }
132
133 if (*RecognizedFS)
134 {
135 Boot = ExAllocatePoolWithTag(NonPagedPool, DiskGeometry.BytesPerSector, TAG_VFAT);
136 if (Boot == NULL)
137 {
138 return STATUS_INSUFFICIENT_RESOURCES;
139 }
140
141 Offset.QuadPart = 0;
142
143 /* Try to recognize FAT12/FAT16/FAT32 partitions */
144 Status = VfatReadDisk(DeviceToMount, &Offset, DiskGeometry.BytesPerSector, (PUCHAR) Boot, Override);
145 if (NT_SUCCESS(Status))
146 {
147 if (Boot->Signatur1 != 0xaa55)
148 {
149 *RecognizedFS = FALSE;
150 }
151
152 if (*RecognizedFS &&
153 Boot->BytesPerSector != 512 &&
154 Boot->BytesPerSector != 1024 &&
155 Boot->BytesPerSector != 2048 &&
156 Boot->BytesPerSector != 4096)
157 {
158 DPRINT1("BytesPerSector %u\n", Boot->BytesPerSector);
159 *RecognizedFS = FALSE;
160 }
161
162 if (*RecognizedFS &&
163 Boot->FATCount != 1 &&
164 Boot->FATCount != 2)
165 {
166 DPRINT1("FATCount %u\n", Boot->FATCount);
167 *RecognizedFS = FALSE;
168 }
169
170 if (*RecognizedFS &&
171 Boot->Media != 0xf0 &&
172 Boot->Media != 0xf8 &&
173 Boot->Media != 0xf9 &&
174 Boot->Media != 0xfa &&
175 Boot->Media != 0xfb &&
176 Boot->Media != 0xfc &&
177 Boot->Media != 0xfd &&
178 Boot->Media != 0xfe &&
179 Boot->Media != 0xff)
180 {
181 DPRINT1("Media %02x\n", Boot->Media);
182 *RecognizedFS = FALSE;
183 }
184
185 if (*RecognizedFS &&
186 Boot->SectorsPerCluster != 1 &&
187 Boot->SectorsPerCluster != 2 &&
188 Boot->SectorsPerCluster != 4 &&
189 Boot->SectorsPerCluster != 8 &&
190 Boot->SectorsPerCluster != 16 &&
191 Boot->SectorsPerCluster != 32 &&
192 Boot->SectorsPerCluster != 64 &&
193 Boot->SectorsPerCluster != 128)
194 {
195 DPRINT1("SectorsPerCluster %02x\n", Boot->SectorsPerCluster);
196 *RecognizedFS = FALSE;
197 }
198
199 if (*RecognizedFS &&
200 Boot->BytesPerSector * Boot->SectorsPerCluster > 32 * 1024)
201 {
202 DPRINT1("ClusterSize %dx\n", Boot->BytesPerSector * Boot->SectorsPerCluster);
203 *RecognizedFS = FALSE;
204 }
205
206 if (*RecognizedFS)
207 {
208 FatInfo.VolumeID = Boot->VolumeID;
209 FatInfo.FATStart = Boot->ReservedSectors;
210 FatInfo.FATCount = Boot->FATCount;
211 FatInfo.FATSectors = Boot->FATSectors ? Boot->FATSectors : ((struct _BootSector32*) Boot)->FATSectors32;
212 FatInfo.BytesPerSector = Boot->BytesPerSector;
213 FatInfo.SectorsPerCluster = Boot->SectorsPerCluster;
214 FatInfo.BytesPerCluster = FatInfo.BytesPerSector * FatInfo.SectorsPerCluster;
215 FatInfo.rootDirectorySectors = ((Boot->RootEntries * 32) + Boot->BytesPerSector - 1) / Boot->BytesPerSector;
216 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
217 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
218 FatInfo.Sectors = Sectors = Boot->Sectors ? Boot->Sectors : Boot->SectorsHuge;
219 Sectors -= Boot->ReservedSectors + FatInfo.FATCount * FatInfo.FATSectors + FatInfo.rootDirectorySectors;
220 FatInfo.NumberOfClusters = Sectors / Boot->SectorsPerCluster;
221 if (FatInfo.NumberOfClusters < 4085)
222 {
223 DPRINT("FAT12\n");
224 FatInfo.FatType = FAT12;
225 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
226 RtlCopyMemory(&FatInfo.VolumeLabel, &Boot->VolumeLabel, sizeof(FatInfo.VolumeLabel));
227 }
228 else if (FatInfo.NumberOfClusters >= 65525)
229 {
230 DPRINT("FAT32\n");
231 FatInfo.FatType = FAT32;
232 FatInfo.RootCluster = ((struct _BootSector32*) Boot)->RootCluster;
233 FatInfo.rootStart = FatInfo.dataStart + ((FatInfo.RootCluster - 2) * FatInfo.SectorsPerCluster);
234 FatInfo.VolumeID = ((struct _BootSector32*) Boot)->VolumeID;
235 RtlCopyMemory(&FatInfo.VolumeLabel, &((struct _BootSector32*)Boot)->VolumeLabel, sizeof(FatInfo.VolumeLabel));
236 }
237 else
238 {
239 DPRINT("FAT16\n");
240 FatInfo.FatType = FAT16;
241 FatInfo.RootCluster = FatInfo.rootStart / FatInfo.SectorsPerCluster;
242 RtlCopyMemory(&FatInfo.VolumeLabel, &Boot->VolumeLabel, sizeof(FatInfo.VolumeLabel));
243 }
244
245 if (PartitionInfoIsValid &&
246 FatInfo.Sectors > PartitionInfo.PartitionLength.QuadPart / FatInfo.BytesPerSector)
247 {
248 *RecognizedFS = FALSE;
249 }
250
251 if (pFatInfo && *RecognizedFS)
252 {
253 *pFatInfo = FatInfo;
254 }
255 }
256 }
257
258 ExFreePool(Boot);
259 }
260
261 if (!*RecognizedFS && PartitionInfoIsValid)
262 {
263 BootFatX = ExAllocatePoolWithTag(NonPagedPool, sizeof(struct _BootSectorFatX), TAG_VFAT);
264 if (BootFatX == NULL)
265 {
266 *RecognizedFS=FALSE;
267 return STATUS_INSUFFICIENT_RESOURCES;
268 }
269
270 Offset.QuadPart = 0;
271
272 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
273 Status = VfatReadDisk(DeviceToMount, &Offset, sizeof(struct _BootSectorFatX), (PUCHAR) BootFatX, Override);
274 if (NT_SUCCESS(Status))
275 {
276 *RecognizedFS = TRUE;
277 if (BootFatX->SysType[0] != 'F' ||
278 BootFatX->SysType[1] != 'A' ||
279 BootFatX->SysType[2] != 'T' ||
280 BootFatX->SysType[3] != 'X')
281 {
282 DPRINT1("SysType %c%c%c%c\n", BootFatX->SysType[0], BootFatX->SysType[1], BootFatX->SysType[2], BootFatX->SysType[3]);
283 *RecognizedFS=FALSE;
284 }
285
286 if (*RecognizedFS &&
287 BootFatX->SectorsPerCluster != 1 &&
288 BootFatX->SectorsPerCluster != 2 &&
289 BootFatX->SectorsPerCluster != 4 &&
290 BootFatX->SectorsPerCluster != 8 &&
291 BootFatX->SectorsPerCluster != 16 &&
292 BootFatX->SectorsPerCluster != 32 &&
293 BootFatX->SectorsPerCluster != 64 &&
294 BootFatX->SectorsPerCluster != 128)
295 {
296 DPRINT1("SectorsPerCluster %lu\n", BootFatX->SectorsPerCluster);
297 *RecognizedFS=FALSE;
298 }
299
300 if (*RecognizedFS)
301 {
302 FatInfo.BytesPerSector = DiskGeometry.BytesPerSector;
303 FatInfo.SectorsPerCluster = BootFatX->SectorsPerCluster;
304 FatInfo.rootDirectorySectors = BootFatX->SectorsPerCluster;
305 FatInfo.BytesPerCluster = BootFatX->SectorsPerCluster * DiskGeometry.BytesPerSector;
306 FatInfo.Sectors = (ULONG)(PartitionInfo.PartitionLength.QuadPart / DiskGeometry.BytesPerSector);
307 if (FatInfo.Sectors / FatInfo.SectorsPerCluster < 65525)
308 {
309 DPRINT("FATX16\n");
310 FatInfo.FatType = FATX16;
311 }
312 else
313 {
314 DPRINT("FATX32\n");
315 FatInfo.FatType = FATX32;
316 }
317 FatInfo.VolumeID = BootFatX->VolumeID;
318 FatInfo.FATStart = sizeof(struct _BootSectorFatX) / DiskGeometry.BytesPerSector;
319 FatInfo.FATCount = BootFatX->FATCount;
320 FatInfo.FATSectors =
321 ROUND_UP(FatInfo.Sectors / FatInfo.SectorsPerCluster * (FatInfo.FatType == FATX16 ? 2 : 4), 4096) /
322 FatInfo.BytesPerSector;
323 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
324 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
325 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
326 FatInfo.NumberOfClusters = (FatInfo.Sectors - FatInfo.dataStart) / FatInfo.SectorsPerCluster;
327
328 if (pFatInfo && *RecognizedFS)
329 {
330 *pFatInfo = FatInfo;
331 }
332 }
333 }
334 ExFreePool(BootFatX);
335 }
336
337 DPRINT("VfatHasFileSystem done\n");
338 return Status;
339 }
340
341 /*
342 * FUNCTION: Read the volume label
343 * WARNING: Read this comment carefully before using it (and using it wrong)
344 * Device parameter is expected to be the lower DO is start isn't 0
345 * otherwise, it is expected to be the VCB is start is 0
346 * Start parameter is expected to be, in bytes, the beginning of the root start.
347 * Set it to 0 if you wish to use the associated FCB with caching.
348 * In that specific case, Device parameter is expected to be the VCB!
349 * VolumeLabel parameter is expected to be a preallocated UNICODE_STRING (ie, with buffer)
350 * Its buffer has to be able to contain MAXIMUM_VOLUME_LABEL_LENGTH bytes
351 */
352 static
353 NTSTATUS
354 ReadVolumeLabel(
355 PVOID Device,
356 ULONG Start,
357 BOOLEAN IsFatX,
358 PUNICODE_STRING VolumeLabel)
359 {
360 PDEVICE_EXTENSION DeviceExt;
361 PDEVICE_OBJECT DeviceObject;
362 PVOID Context = NULL;
363 ULONG DirIndex = 0;
364 PDIR_ENTRY Entry;
365 PVFATFCB pFcb;
366 LARGE_INTEGER FileOffset;
367 ULONG SizeDirEntry;
368 ULONG EntriesPerPage;
369 OEM_STRING StringO;
370 BOOLEAN NoCache = (Start != 0);
371 PVOID Buffer;
372 NTSTATUS Status = STATUS_SUCCESS;
373
374 if (IsFatX)
375 {
376 SizeDirEntry = sizeof(FATX_DIR_ENTRY);
377 EntriesPerPage = FATX_ENTRIES_PER_PAGE;
378 }
379 else
380 {
381 SizeDirEntry = sizeof(FAT_DIR_ENTRY);
382 EntriesPerPage = FAT_ENTRIES_PER_PAGE;
383 }
384
385 FileOffset.QuadPart = Start;
386 if (!NoCache)
387 {
388 DeviceExt = Device;
389
390 /* FIXME: Check we really have a VCB
391 ASSERT();
392 */
393
394 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
395 pFcb = vfatOpenRootFCB(DeviceExt);
396 ExReleaseResourceLite(&DeviceExt->DirResource);
397
398 _SEH2_TRY
399 {
400 CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, MAP_WAIT, &Context, (PVOID*)&Entry);
401 }
402 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
403 {
404 Status = _SEH2_GetExceptionCode();
405 }
406 _SEH2_END;
407 }
408 else
409 {
410 DeviceObject = Device;
411
412 ASSERT(DeviceObject->Type == 3);
413
414 Buffer = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, TAG_VFAT);
415 if (Buffer != NULL)
416 {
417 Status = VfatReadDisk(DeviceObject, &FileOffset, PAGE_SIZE, (PUCHAR)Buffer, TRUE);
418 if (!NT_SUCCESS(Status))
419 {
420 ExFreePoolWithTag(Buffer, TAG_VFAT);
421 }
422 else
423 {
424 Entry = Buffer;
425 }
426 }
427 else
428 {
429 Status = STATUS_INSUFFICIENT_RESOURCES;
430 }
431 }
432
433 if (NT_SUCCESS(Status))
434 {
435 while (TRUE)
436 {
437 if (ENTRY_VOLUME(IsFatX, Entry))
438 {
439 /* copy volume label */
440 if (IsFatX)
441 {
442 StringO.Buffer = (PCHAR)Entry->FatX.Filename;
443 StringO.MaximumLength = StringO.Length = Entry->FatX.FilenameLength;
444 RtlOemStringToUnicodeString(VolumeLabel, &StringO, FALSE);
445 }
446 else
447 {
448 vfat8Dot3ToString(&Entry->Fat, VolumeLabel);
449 }
450 break;
451 }
452 if (ENTRY_END(IsFatX, Entry))
453 {
454 break;
455 }
456 DirIndex++;
457 Entry = (PDIR_ENTRY)((ULONG_PTR)Entry + SizeDirEntry);
458 if ((DirIndex % EntriesPerPage) == 0)
459 {
460 FileOffset.u.LowPart += PAGE_SIZE;
461
462 if (!NoCache)
463 {
464 CcUnpinData(Context);
465
466 _SEH2_TRY
467 {
468 CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, MAP_WAIT, &Context, (PVOID*)&Entry);
469 }
470 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
471 {
472 Status = _SEH2_GetExceptionCode();
473 }
474 _SEH2_END;
475 if (!NT_SUCCESS(Status))
476 {
477 Context = NULL;
478 break;
479 }
480 }
481 else
482 {
483 Status = VfatReadDisk(DeviceObject, &FileOffset, PAGE_SIZE, (PUCHAR)Buffer, TRUE);
484 if (!NT_SUCCESS(Status))
485 {
486 break;
487 }
488 Entry = Buffer;
489 }
490 }
491 }
492 if (Context)
493 {
494 CcUnpinData(Context);
495 }
496 else if (NoCache)
497 {
498 ExFreePoolWithTag(Buffer, TAG_VFAT);
499 }
500 }
501
502 if (!NoCache)
503 {
504 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
505 vfatReleaseFCB(DeviceExt, pFcb);
506 ExReleaseResourceLite(&DeviceExt->DirResource);
507 }
508
509 return STATUS_SUCCESS;
510 }
511
512
513 /*
514 * FUNCTION: Mount the filesystem
515 */
516 static
517 NTSTATUS
518 VfatMount(
519 PVFAT_IRP_CONTEXT IrpContext)
520 {
521 PDEVICE_OBJECT DeviceObject = NULL;
522 PDEVICE_EXTENSION DeviceExt = NULL;
523 BOOLEAN RecognizedFS;
524 NTSTATUS Status;
525 PVFATFCB Fcb = NULL;
526 PVFATFCB VolumeFcb = NULL;
527 PVFATCCB Ccb = NULL;
528 PDEVICE_OBJECT DeviceToMount;
529 PVPB Vpb;
530 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\$$Fat$$");
531 UNICODE_STRING VolumeNameU = RTL_CONSTANT_STRING(L"\\$$Volume$$");
532 UNICODE_STRING VolumeLabelU;
533 ULONG HashTableSize;
534 ULONG eocMark;
535 ULONG i;
536 FATINFO FatInfo;
537
538 DPRINT("VfatMount(IrpContext %p)\n", IrpContext);
539
540 ASSERT(IrpContext);
541
542 if (IrpContext->DeviceObject != VfatGlobalData->DeviceObject)
543 {
544 Status = STATUS_INVALID_DEVICE_REQUEST;
545 goto ByeBye;
546 }
547
548 DeviceToMount = IrpContext->Stack->Parameters.MountVolume.DeviceObject;
549 Vpb = IrpContext->Stack->Parameters.MountVolume.Vpb;
550
551 Status = VfatHasFileSystem(DeviceToMount, &RecognizedFS, &FatInfo, FALSE);
552 if (!NT_SUCCESS(Status))
553 {
554 goto ByeBye;
555 }
556
557 if (RecognizedFS == FALSE)
558 {
559 DPRINT("VFAT: Unrecognized Volume\n");
560 Status = STATUS_UNRECOGNIZED_VOLUME;
561 goto ByeBye;
562 }
563
564 /* Use prime numbers for the table size */
565 if (FatInfo.FatType == FAT12)
566 {
567 HashTableSize = 4099; // 4096 = 4 * 1024
568 }
569 else if (FatInfo.FatType == FAT16 ||
570 FatInfo.FatType == FATX16)
571 {
572 HashTableSize = 16411; // 16384 = 16 * 1024
573 }
574 else
575 {
576 HashTableSize = 65537; // 65536 = 64 * 1024;
577 }
578 DPRINT("VFAT: Recognized volume\n");
579 Status = IoCreateDevice(VfatGlobalData->DriverObject,
580 ROUND_UP(sizeof (DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize,
581 NULL,
582 FILE_DEVICE_DISK_FILE_SYSTEM,
583 DeviceToMount->Characteristics,
584 FALSE,
585 &DeviceObject);
586 if (!NT_SUCCESS(Status))
587 {
588 goto ByeBye;
589 }
590
591 DeviceExt = DeviceObject->DeviceExtension;
592 RtlZeroMemory(DeviceExt, ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize);
593 DeviceExt->FcbHashTable = (HASHENTRY**)((ULONG_PTR)DeviceExt + ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)));
594 DeviceExt->HashTableSize = HashTableSize;
595 DeviceExt->VolumeDevice = DeviceObject;
596
597 /* use same vpb as device disk */
598 DeviceObject->Vpb = Vpb;
599 DeviceToMount->Vpb = Vpb;
600
601 RtlCopyMemory(&DeviceExt->FatInfo, &FatInfo, sizeof(FATINFO));
602
603 DPRINT("BytesPerSector: %u\n", DeviceExt->FatInfo.BytesPerSector);
604 DPRINT("SectorsPerCluster: %u\n", DeviceExt->FatInfo.SectorsPerCluster);
605 DPRINT("FATCount: %u\n", DeviceExt->FatInfo.FATCount);
606 DPRINT("FATSectors: %u\n", DeviceExt->FatInfo.FATSectors);
607 DPRINT("RootStart: %u\n", DeviceExt->FatInfo.rootStart);
608 DPRINT("DataStart: %u\n", DeviceExt->FatInfo.dataStart);
609 if (DeviceExt->FatInfo.FatType == FAT32)
610 {
611 DPRINT("RootCluster: %u\n", DeviceExt->FatInfo.RootCluster);
612 }
613
614 switch (DeviceExt->FatInfo.FatType)
615 {
616 case FAT12:
617 DeviceExt->GetNextCluster = FAT12GetNextCluster;
618 DeviceExt->FindAndMarkAvailableCluster = FAT12FindAndMarkAvailableCluster;
619 DeviceExt->WriteCluster = FAT12WriteCluster;
620 DeviceExt->CleanShutBitMask = 0;
621 break;
622
623 case FAT16:
624 case FATX16:
625 DeviceExt->GetNextCluster = FAT16GetNextCluster;
626 DeviceExt->FindAndMarkAvailableCluster = FAT16FindAndMarkAvailableCluster;
627 DeviceExt->WriteCluster = FAT16WriteCluster;
628 DeviceExt->CleanShutBitMask = 0x8000;
629 break;
630
631 case FAT32:
632 case FATX32:
633 DeviceExt->GetNextCluster = FAT32GetNextCluster;
634 DeviceExt->FindAndMarkAvailableCluster = FAT32FindAndMarkAvailableCluster;
635 DeviceExt->WriteCluster = FAT32WriteCluster;
636 DeviceExt->CleanShutBitMask = 0x80000000;
637 break;
638 }
639
640 if (DeviceExt->FatInfo.FatType == FATX16 ||
641 DeviceExt->FatInfo.FatType == FATX32)
642 {
643 DeviceExt->Flags |= VCB_IS_FATX;
644 DeviceExt->BaseDateYear = 2000;
645 RtlCopyMemory(&DeviceExt->Dispatch, &FatXDispatch, sizeof(VFAT_DISPATCH));
646 }
647 else
648 {
649 DeviceExt->BaseDateYear = 1980;
650 RtlCopyMemory(&DeviceExt->Dispatch, &FatDispatch, sizeof(VFAT_DISPATCH));
651 }
652
653 DeviceExt->StorageDevice = DeviceToMount;
654 DeviceExt->StorageDevice->Vpb->DeviceObject = DeviceObject;
655 DeviceExt->StorageDevice->Vpb->RealDevice = DeviceExt->StorageDevice;
656 DeviceExt->StorageDevice->Vpb->Flags |= VPB_MOUNTED;
657 DeviceObject->StackSize = DeviceExt->StorageDevice->StackSize + 1;
658 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
659
660 DPRINT("FsDeviceObject %p\n", DeviceObject);
661
662 /* Initialize this resource early ... it's used in VfatCleanup */
663 ExInitializeResourceLite(&DeviceExt->DirResource);
664
665 DeviceExt->IoVPB = DeviceObject->Vpb;
666 DeviceExt->SpareVPB = ExAllocatePoolWithTag(NonPagedPool, sizeof(VPB), TAG_VFAT);
667 if (DeviceExt->SpareVPB == NULL)
668 {
669 Status = STATUS_INSUFFICIENT_RESOURCES;
670 goto ByeBye;
671 }
672
673 DeviceExt->Statistics = ExAllocatePoolWithTag(NonPagedPool,
674 sizeof(STATISTICS) * VfatGlobalData->NumberProcessors,
675 TAG_VFAT);
676 if (DeviceExt->Statistics == NULL)
677 {
678 Status = STATUS_INSUFFICIENT_RESOURCES;
679 goto ByeBye;
680 }
681
682 RtlZeroMemory(DeviceExt->Statistics, sizeof(STATISTICS) * VfatGlobalData->NumberProcessors);
683 for (i = 0; i < VfatGlobalData->NumberProcessors; ++i)
684 {
685 DeviceExt->Statistics[i].Base.FileSystemType = FILESYSTEM_STATISTICS_TYPE_FAT;
686 DeviceExt->Statistics[i].Base.Version = 1;
687 DeviceExt->Statistics[i].Base.SizeOfCompleteStructure = sizeof(STATISTICS);
688 }
689
690 DeviceExt->FATFileObject = IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
691 Fcb = vfatNewFCB(DeviceExt, &NameU);
692 if (Fcb == NULL)
693 {
694 Status = STATUS_INSUFFICIENT_RESOURCES;
695 goto ByeBye;
696 }
697
698 Ccb = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
699 if (Ccb == NULL)
700 {
701 Status = STATUS_INSUFFICIENT_RESOURCES;
702 goto ByeBye;
703 }
704
705 RtlZeroMemory(Ccb, sizeof (VFATCCB));
706 DeviceExt->FATFileObject->FsContext = Fcb;
707 DeviceExt->FATFileObject->FsContext2 = Ccb;
708 DeviceExt->FATFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
709 DeviceExt->FATFileObject->PrivateCacheMap = NULL;
710 DeviceExt->FATFileObject->Vpb = DeviceObject->Vpb;
711 Fcb->FileObject = DeviceExt->FATFileObject;
712
713 Fcb->Flags |= FCB_IS_FAT;
714
715 Fcb->RFCB.FileSize.QuadPart = DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector;
716 Fcb->RFCB.ValidDataLength = Fcb->RFCB.FileSize;
717 Fcb->RFCB.AllocationSize = Fcb->RFCB.FileSize;
718
719 _SEH2_TRY
720 {
721 CcInitializeCacheMap(DeviceExt->FATFileObject,
722 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
723 TRUE,
724 &VfatGlobalData->CacheMgrCallbacks,
725 Fcb);
726 }
727 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
728 {
729 Status = _SEH2_GetExceptionCode();
730 goto ByeBye;
731 }
732 _SEH2_END;
733
734 DeviceExt->LastAvailableCluster = 2;
735 ExInitializeResourceLite(&DeviceExt->FatResource);
736
737 InitializeListHead(&DeviceExt->FcbListHead);
738
739 VolumeFcb = vfatNewFCB(DeviceExt, &VolumeNameU);
740 if (VolumeFcb == NULL)
741 {
742 Status = STATUS_INSUFFICIENT_RESOURCES;
743 goto ByeBye;
744 }
745
746 VolumeFcb->Flags = FCB_IS_VOLUME;
747 VolumeFcb->RFCB.FileSize.QuadPart = (LONGLONG) DeviceExt->FatInfo.Sectors * DeviceExt->FatInfo.BytesPerSector;
748 VolumeFcb->RFCB.ValidDataLength = VolumeFcb->RFCB.FileSize;
749 VolumeFcb->RFCB.AllocationSize = VolumeFcb->RFCB.FileSize;
750 DeviceExt->VolumeFcb = VolumeFcb;
751
752 ExAcquireResourceExclusiveLite(&VfatGlobalData->VolumeListLock, TRUE);
753 InsertHeadList(&VfatGlobalData->VolumeListHead, &DeviceExt->VolumeListEntry);
754 ExReleaseResourceLite(&VfatGlobalData->VolumeListLock);
755
756 /* read serial number */
757 DeviceObject->Vpb->SerialNumber = DeviceExt->FatInfo.VolumeID;
758
759 /* read volume label */
760 VolumeLabelU.Buffer = DeviceObject->Vpb->VolumeLabel;
761 VolumeLabelU.Length = 0;
762 VolumeLabelU.MaximumLength = sizeof(DeviceObject->Vpb->VolumeLabel);
763 ReadVolumeLabel(DeviceExt, 0, vfatVolumeIsFatX(DeviceExt), &VolumeLabelU);
764 Vpb->VolumeLabelLength = VolumeLabelU.Length;
765
766 /* read clean shutdown bit status */
767 Status = GetNextCluster(DeviceExt, 1, &eocMark);
768 if (NT_SUCCESS(Status))
769 {
770 if (eocMark & DeviceExt->CleanShutBitMask)
771 {
772 /* unset clean shutdown bit */
773 eocMark &= ~DeviceExt->CleanShutBitMask;
774 WriteCluster(DeviceExt, 1, eocMark);
775 VolumeFcb->Flags |= VCB_CLEAR_DIRTY;
776 }
777 }
778
779 VolumeFcb->Flags |= VCB_IS_DIRTY;
780
781 FsRtlNotifyVolumeEvent(DeviceExt->FATFileObject, FSRTL_VOLUME_MOUNT);
782 FsRtlNotifyInitializeSync(&DeviceExt->NotifySync);
783 InitializeListHead(&DeviceExt->NotifyList);
784
785 DPRINT("Mount success\n");
786
787 Status = STATUS_SUCCESS;
788
789 ByeBye:
790 if (!NT_SUCCESS(Status))
791 {
792 /* Cleanup */
793 if (DeviceExt && DeviceExt->FATFileObject)
794 ObDereferenceObject (DeviceExt->FATFileObject);
795 if (DeviceExt && DeviceExt->SpareVPB)
796 ExFreePoolWithTag(DeviceExt->SpareVPB, TAG_VFAT);
797 if (DeviceExt && DeviceExt->Statistics)
798 ExFreePoolWithTag(DeviceExt->Statistics, TAG_VFAT);
799 if (Fcb)
800 vfatDestroyFCB(Fcb);
801 if (Ccb)
802 vfatDestroyCCB(Ccb);
803 if (DeviceObject)
804 IoDeleteDevice(DeviceObject);
805 }
806
807 return Status;
808 }
809
810
811 /*
812 * FUNCTION: Verify the filesystem
813 */
814 static
815 NTSTATUS
816 VfatVerify(
817 PVFAT_IRP_CONTEXT IrpContext)
818 {
819 PDEVICE_OBJECT DeviceToVerify;
820 NTSTATUS Status;
821 FATINFO FatInfo;
822 BOOLEAN RecognizedFS;
823 PDEVICE_EXTENSION DeviceExt;
824 BOOLEAN AllowRaw;
825 PVPB Vpb;
826 ULONG ChangeCount, BufSize = sizeof(ChangeCount);
827
828 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext);
829
830 DeviceToVerify = IrpContext->Stack->Parameters.VerifyVolume.DeviceObject;
831 DeviceExt = DeviceToVerify->DeviceExtension;
832 Vpb = IrpContext->Stack->Parameters.VerifyVolume.Vpb;
833 AllowRaw = BooleanFlagOn(IrpContext->Stack->Flags, SL_ALLOW_RAW_MOUNT);
834
835 if (!BooleanFlagOn(Vpb->RealDevice->Flags, DO_VERIFY_VOLUME))
836 {
837 DPRINT("Already verified\n");
838 return STATUS_SUCCESS;
839 }
840
841 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
842 IOCTL_DISK_CHECK_VERIFY,
843 NULL,
844 0,
845 &ChangeCount,
846 &BufSize,
847 TRUE);
848 if (!NT_SUCCESS(Status) && Status != STATUS_VERIFY_REQUIRED)
849 {
850 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status);
851 Status = (AllowRaw ? STATUS_WRONG_VOLUME : Status);
852 }
853 else
854 {
855 Status = VfatHasFileSystem(DeviceExt->StorageDevice, &RecognizedFS, &FatInfo, TRUE);
856 if (!NT_SUCCESS(Status) || RecognizedFS == FALSE)
857 {
858 if (NT_SUCCESS(Status) || AllowRaw)
859 {
860 Status = STATUS_WRONG_VOLUME;
861 }
862 }
863 else if (sizeof(FATINFO) == RtlCompareMemory(&FatInfo, &DeviceExt->FatInfo, sizeof(FATINFO)))
864 {
865 WCHAR BufferU[MAXIMUM_VOLUME_LABEL_LENGTH / sizeof(WCHAR)];
866 UNICODE_STRING VolumeLabelU;
867 UNICODE_STRING VpbLabelU;
868
869 VolumeLabelU.Buffer = BufferU;
870 VolumeLabelU.Length = 0;
871 VolumeLabelU.MaximumLength = sizeof(BufferU);
872 Status = ReadVolumeLabel(DeviceExt->StorageDevice, FatInfo.rootStart * FatInfo.BytesPerSector, (FatInfo.FatType >= FATX16), &VolumeLabelU);
873 if (!NT_SUCCESS(Status))
874 {
875 if (AllowRaw)
876 {
877 Status = STATUS_WRONG_VOLUME;
878 }
879 }
880 else
881 {
882 VpbLabelU.Buffer = Vpb->VolumeLabel;
883 VpbLabelU.Length = Vpb->VolumeLabelLength;
884 VpbLabelU.MaximumLength = sizeof(Vpb->VolumeLabel);
885
886 if (RtlCompareUnicodeString(&VpbLabelU, &VolumeLabelU, FALSE) != 0)
887 {
888 Status = STATUS_WRONG_VOLUME;
889 }
890 else
891 {
892 DPRINT1("Same volume\n");
893 }
894 }
895 }
896 else
897 {
898 Status = STATUS_WRONG_VOLUME;
899 }
900 }
901
902 Vpb->RealDevice->Flags &= ~DO_VERIFY_VOLUME;
903
904 return Status;
905 }
906
907
908 static
909 NTSTATUS
910 VfatGetVolumeBitmap(
911 PVFAT_IRP_CONTEXT IrpContext)
912 {
913 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext);
914 return STATUS_INVALID_DEVICE_REQUEST;
915 }
916
917
918 static
919 NTSTATUS
920 VfatGetRetrievalPointers(
921 PVFAT_IRP_CONTEXT IrpContext)
922 {
923 PIO_STACK_LOCATION Stack;
924 LARGE_INTEGER Vcn;
925 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers;
926 PFILE_OBJECT FileObject;
927 ULONG MaxExtentCount;
928 PVFATFCB Fcb;
929 PDEVICE_EXTENSION DeviceExt;
930 ULONG FirstCluster;
931 ULONG CurrentCluster;
932 ULONG LastCluster;
933 NTSTATUS Status;
934
935 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext);
936
937 DeviceExt = IrpContext->DeviceExt;
938 FileObject = IrpContext->FileObject;
939 Stack = IrpContext->Stack;
940 if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(STARTING_VCN_INPUT_BUFFER) ||
941 Stack->Parameters.DeviceIoControl.Type3InputBuffer == NULL)
942 {
943 return STATUS_INVALID_PARAMETER;
944 }
945
946 if (IrpContext->Irp->UserBuffer == NULL ||
947 Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(RETRIEVAL_POINTERS_BUFFER))
948 {
949 return STATUS_BUFFER_TOO_SMALL;
950 }
951
952 Fcb = FileObject->FsContext;
953
954 ExAcquireResourceSharedLite(&Fcb->MainResource, TRUE);
955
956 Vcn = ((PSTARTING_VCN_INPUT_BUFFER)Stack->Parameters.DeviceIoControl.Type3InputBuffer)->StartingVcn;
957 RetrievalPointers = IrpContext->Irp->UserBuffer;
958
959 MaxExtentCount = ((Stack->Parameters.DeviceIoControl.OutputBufferLength - sizeof(RetrievalPointers->ExtentCount) - sizeof(RetrievalPointers->StartingVcn)) / sizeof(RetrievalPointers->Extents[0]));
960
961 if (Vcn.QuadPart >= Fcb->RFCB.AllocationSize.QuadPart / DeviceExt->FatInfo.BytesPerCluster)
962 {
963 Status = STATUS_INVALID_PARAMETER;
964 goto ByeBye;
965 }
966
967 CurrentCluster = FirstCluster = vfatDirEntryGetFirstCluster(DeviceExt, &Fcb->entry);
968 Status = OffsetToCluster(DeviceExt, FirstCluster,
969 Vcn.u.LowPart * DeviceExt->FatInfo.BytesPerCluster,
970 &CurrentCluster, FALSE);
971 if (!NT_SUCCESS(Status))
972 {
973 goto ByeBye;
974 }
975
976 RetrievalPointers->StartingVcn = Vcn;
977 RetrievalPointers->ExtentCount = 0;
978 RetrievalPointers->Extents[0].Lcn.u.HighPart = 0;
979 RetrievalPointers->Extents[0].Lcn.u.LowPart = CurrentCluster - 2;
980 LastCluster = 0;
981 while (CurrentCluster != 0xffffffff && RetrievalPointers->ExtentCount < MaxExtentCount)
982 {
983 LastCluster = CurrentCluster;
984 Status = NextCluster(DeviceExt, CurrentCluster, &CurrentCluster, FALSE);
985 Vcn.QuadPart++;
986 if (!NT_SUCCESS(Status))
987 {
988 goto ByeBye;
989 }
990
991 if (LastCluster + 1 != CurrentCluster)
992 {
993 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].NextVcn = Vcn;
994 RetrievalPointers->ExtentCount++;
995 if (RetrievalPointers->ExtentCount < MaxExtentCount)
996 {
997 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.HighPart = 0;
998 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.LowPart = CurrentCluster - 2;
999 }
1000 }
1001 }
1002
1003 IrpContext->Irp->IoStatus.Information = sizeof(RETRIEVAL_POINTERS_BUFFER) + (sizeof(RetrievalPointers->Extents[0]) * (RetrievalPointers->ExtentCount - 1));
1004 Status = STATUS_SUCCESS;
1005
1006 ByeBye:
1007 ExReleaseResourceLite(&Fcb->MainResource);
1008
1009 return Status;
1010 }
1011
1012 static
1013 NTSTATUS
1014 VfatMoveFile(
1015 PVFAT_IRP_CONTEXT IrpContext)
1016 {
1017 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext);
1018 return STATUS_INVALID_DEVICE_REQUEST;
1019 }
1020
1021 static
1022 NTSTATUS
1023 VfatIsVolumeDirty(
1024 PVFAT_IRP_CONTEXT IrpContext)
1025 {
1026 PULONG Flags;
1027
1028 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext);
1029
1030 if (IrpContext->Stack->Parameters.FileSystemControl.OutputBufferLength != sizeof(ULONG))
1031 return STATUS_INVALID_BUFFER_SIZE;
1032 else if (!IrpContext->Irp->AssociatedIrp.SystemBuffer)
1033 return STATUS_INVALID_USER_BUFFER;
1034
1035 Flags = (PULONG)IrpContext->Irp->AssociatedIrp.SystemBuffer;
1036 *Flags = 0;
1037
1038 if (BooleanFlagOn(IrpContext->DeviceExt->VolumeFcb->Flags, VCB_IS_DIRTY) &&
1039 !BooleanFlagOn(IrpContext->DeviceExt->VolumeFcb->Flags, VCB_CLEAR_DIRTY))
1040 {
1041 *Flags |= VOLUME_IS_DIRTY;
1042 }
1043
1044 IrpContext->Irp->IoStatus.Information = sizeof(ULONG);
1045
1046 return STATUS_SUCCESS;
1047 }
1048
1049 static
1050 NTSTATUS
1051 VfatMarkVolumeDirty(
1052 PVFAT_IRP_CONTEXT IrpContext)
1053 {
1054 ULONG eocMark;
1055 PDEVICE_EXTENSION DeviceExt;
1056 NTSTATUS Status = STATUS_SUCCESS;
1057
1058 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext);
1059 DeviceExt = IrpContext->DeviceExt;
1060
1061 if (!BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_IS_DIRTY))
1062 {
1063 Status = GetNextCluster(DeviceExt, 1, &eocMark);
1064 if (NT_SUCCESS(Status))
1065 {
1066 /* unset clean shutdown bit */
1067 eocMark &= ~DeviceExt->CleanShutBitMask;
1068 Status = WriteCluster(DeviceExt, 1, eocMark);
1069 }
1070 }
1071
1072 DeviceExt->VolumeFcb->Flags &= ~VCB_CLEAR_DIRTY;
1073
1074 return Status;
1075 }
1076
1077 static
1078 NTSTATUS
1079 VfatLockOrUnlockVolume(
1080 PVFAT_IRP_CONTEXT IrpContext,
1081 BOOLEAN Lock)
1082 {
1083 PFILE_OBJECT FileObject;
1084 PDEVICE_EXTENSION DeviceExt;
1085 PVFATFCB Fcb;
1086 PVPB Vpb;
1087
1088 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext, Lock);
1089
1090 DeviceExt = IrpContext->DeviceExt;
1091 FileObject = IrpContext->FileObject;
1092 Fcb = FileObject->FsContext;
1093 Vpb = DeviceExt->FATFileObject->Vpb;
1094
1095 /* Only allow locking with the volume open */
1096 if (!BooleanFlagOn(Fcb->Flags, FCB_IS_VOLUME))
1097 {
1098 return STATUS_ACCESS_DENIED;
1099 }
1100
1101 /* Bail out if it's already in the demanded state */
1102 if ((BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED) && Lock) ||
1103 (!BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED) && !Lock))
1104 {
1105 return STATUS_ACCESS_DENIED;
1106 }
1107
1108 /* Bail out if it's already in the demanded state */
1109 if ((BooleanFlagOn(Vpb->Flags, VPB_LOCKED) && Lock) ||
1110 (!BooleanFlagOn(Vpb->Flags, VPB_LOCKED) && !Lock))
1111 {
1112 return STATUS_ACCESS_DENIED;
1113 }
1114
1115 /* Deny locking if we're not alone */
1116 if (Lock && DeviceExt->OpenHandleCount != 1)
1117 {
1118 return STATUS_ACCESS_DENIED;
1119 }
1120
1121 /* Finally, proceed */
1122 if (Lock)
1123 {
1124 DeviceExt->Flags |= VCB_VOLUME_LOCKED;
1125 Vpb->Flags |= VPB_LOCKED;
1126 }
1127 else
1128 {
1129 DeviceExt->Flags &= ~VCB_VOLUME_LOCKED;
1130 Vpb->Flags &= ~VPB_LOCKED;
1131 }
1132
1133 return STATUS_SUCCESS;
1134 }
1135
1136 static
1137 NTSTATUS
1138 VfatDismountVolume(
1139 PVFAT_IRP_CONTEXT IrpContext)
1140 {
1141 PDEVICE_EXTENSION DeviceExt;
1142 PLIST_ENTRY NextEntry;
1143 PVFATFCB Fcb;
1144 PFILE_OBJECT FileObject;
1145 ULONG eocMark;
1146 NTSTATUS Status;
1147
1148 DPRINT("VfatDismountVolume(%p)\n", IrpContext);
1149
1150 DeviceExt = IrpContext->DeviceExt;
1151 FileObject = IrpContext->FileObject;
1152
1153 /* We HAVE to be locked. Windows also allows dismount with no lock
1154 * but we're here mainly for 1st stage, so KISS
1155 */
1156 if (!BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED))
1157 {
1158 return STATUS_ACCESS_DENIED;
1159 }
1160
1161 /* Race condition? */
1162 if (BooleanFlagOn(DeviceExt->Flags, VCB_DISMOUNT_PENDING))
1163 {
1164 return STATUS_VOLUME_DISMOUNTED;
1165 }
1166
1167 /* Notify we'll dismount. Pass that point there's no reason we fail */
1168 FsRtlNotifyVolumeEvent(IrpContext->Stack->FileObject, FSRTL_VOLUME_DISMOUNT);
1169
1170 ExAcquireResourceExclusiveLite(&DeviceExt->FatResource, TRUE);
1171
1172 if (BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_CLEAR_DIRTY))
1173 {
1174 /* Set clean shutdown bit */
1175 Status = GetNextCluster(DeviceExt, 1, &eocMark);
1176 if (NT_SUCCESS(Status))
1177 {
1178 eocMark |= DeviceExt->CleanShutBitMask;
1179 if (NT_SUCCESS(WriteCluster(DeviceExt, 1, eocMark)))
1180 DeviceExt->VolumeFcb->Flags &= ~VCB_IS_DIRTY;
1181 }
1182 }
1183
1184 /* Flush volume & files */
1185 VfatFlushVolume(DeviceExt, (PVFATFCB)FileObject->FsContext);
1186
1187 /* Rebrowse the FCB in order to free them now */
1188 while (!IsListEmpty(&DeviceExt->FcbListHead))
1189 {
1190 NextEntry = RemoveHeadList(&DeviceExt->FcbListHead);
1191 Fcb = CONTAINING_RECORD(NextEntry, VFATFCB, FcbListEntry);
1192 vfatDestroyFCB(Fcb);
1193 }
1194
1195 /* Mark we're being dismounted */
1196 DeviceExt->Flags |= VCB_DISMOUNT_PENDING;
1197 #ifndef ENABLE_SWAPOUT
1198 IrpContext->DeviceObject->Vpb->Flags &= ~VPB_MOUNTED;
1199 #endif
1200
1201 ExReleaseResourceLite(&DeviceExt->FatResource);
1202
1203 /* Release a few resources and quit, we're done */
1204 ExDeleteResourceLite(&DeviceExt->DirResource);
1205 ExDeleteResourceLite(&DeviceExt->FatResource);
1206 ObDereferenceObject(DeviceExt->FATFileObject);
1207
1208 return STATUS_SUCCESS;
1209 }
1210
1211 static
1212 NTSTATUS
1213 VfatGetStatistics(
1214 PVFAT_IRP_CONTEXT IrpContext)
1215 {
1216 PVOID Buffer;
1217 ULONG Length;
1218 NTSTATUS Status;
1219 PDEVICE_EXTENSION DeviceExt;
1220
1221 DeviceExt = IrpContext->DeviceExt;
1222 Length = IrpContext->Stack->Parameters.FileSystemControl.OutputBufferLength;
1223 Buffer = IrpContext->Irp->AssociatedIrp.SystemBuffer;
1224
1225 if (Length < sizeof(FILESYSTEM_STATISTICS))
1226 {
1227 return STATUS_BUFFER_TOO_SMALL;
1228 }
1229
1230 if (Buffer == NULL)
1231 {
1232 return STATUS_INVALID_USER_BUFFER;
1233 }
1234
1235 if (Length >= sizeof(STATISTICS) * VfatGlobalData->NumberProcessors)
1236 {
1237 Length = sizeof(STATISTICS) * VfatGlobalData->NumberProcessors;
1238 Status = STATUS_SUCCESS;
1239 }
1240 else
1241 {
1242 Status = STATUS_BUFFER_OVERFLOW;
1243 }
1244
1245 RtlCopyMemory(Buffer, DeviceExt->Statistics, Length);
1246 IrpContext->Irp->IoStatus.Information = Length;
1247
1248 return Status;
1249 }
1250
1251 /*
1252 * FUNCTION: File system control
1253 */
1254 NTSTATUS
1255 VfatFileSystemControl(
1256 PVFAT_IRP_CONTEXT IrpContext)
1257 {
1258 NTSTATUS Status;
1259
1260 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext);
1261
1262 ASSERT(IrpContext);
1263 ASSERT(IrpContext->Irp);
1264 ASSERT(IrpContext->Stack);
1265
1266 IrpContext->Irp->IoStatus.Information = 0;
1267
1268 switch (IrpContext->MinorFunction)
1269 {
1270 case IRP_MN_KERNEL_CALL:
1271 case IRP_MN_USER_FS_REQUEST:
1272 switch(IrpContext->Stack->Parameters.DeviceIoControl.IoControlCode)
1273 {
1274 case FSCTL_GET_VOLUME_BITMAP:
1275 Status = VfatGetVolumeBitmap(IrpContext);
1276 break;
1277
1278 case FSCTL_GET_RETRIEVAL_POINTERS:
1279 Status = VfatGetRetrievalPointers(IrpContext);
1280 break;
1281
1282 case FSCTL_MOVE_FILE:
1283 Status = VfatMoveFile(IrpContext);
1284 break;
1285
1286 case FSCTL_IS_VOLUME_DIRTY:
1287 Status = VfatIsVolumeDirty(IrpContext);
1288 break;
1289
1290 case FSCTL_MARK_VOLUME_DIRTY:
1291 Status = VfatMarkVolumeDirty(IrpContext);
1292 break;
1293
1294 case FSCTL_LOCK_VOLUME:
1295 Status = VfatLockOrUnlockVolume(IrpContext, TRUE);
1296 break;
1297
1298 case FSCTL_UNLOCK_VOLUME:
1299 Status = VfatLockOrUnlockVolume(IrpContext, FALSE);
1300 break;
1301
1302 case FSCTL_DISMOUNT_VOLUME:
1303 Status = VfatDismountVolume(IrpContext);
1304 break;
1305
1306 case FSCTL_FILESYSTEM_GET_STATISTICS:
1307 Status = VfatGetStatistics(IrpContext);
1308 break;
1309
1310 default:
1311 Status = STATUS_INVALID_DEVICE_REQUEST;
1312 }
1313 break;
1314
1315 case IRP_MN_MOUNT_VOLUME:
1316 Status = VfatMount(IrpContext);
1317 break;
1318
1319 case IRP_MN_VERIFY_VOLUME:
1320 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1321 Status = VfatVerify(IrpContext);
1322 break;
1323
1324 default:
1325 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext->MinorFunction);
1326 Status = STATUS_INVALID_DEVICE_REQUEST;
1327 break;
1328 }
1329
1330 return Status;
1331 }