486535d1290fd6f834063ef2c38030d022f60f5d
[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 FatInfo.FSInfoSector = ((struct _BootSector32*) Boot)->FSInfoSector;
236 RtlCopyMemory(&FatInfo.VolumeLabel, &((struct _BootSector32*)Boot)->VolumeLabel, sizeof(FatInfo.VolumeLabel));
237 }
238 else
239 {
240 DPRINT("FAT16\n");
241 FatInfo.FatType = FAT16;
242 FatInfo.RootCluster = FatInfo.rootStart / FatInfo.SectorsPerCluster;
243 RtlCopyMemory(&FatInfo.VolumeLabel, &Boot->VolumeLabel, sizeof(FatInfo.VolumeLabel));
244 }
245
246 if (PartitionInfoIsValid &&
247 FatInfo.Sectors > PartitionInfo.PartitionLength.QuadPart / FatInfo.BytesPerSector)
248 {
249 *RecognizedFS = FALSE;
250 }
251
252 if (pFatInfo && *RecognizedFS)
253 {
254 *pFatInfo = FatInfo;
255 }
256 }
257 }
258
259 ExFreePool(Boot);
260 }
261
262 if (!*RecognizedFS && PartitionInfoIsValid)
263 {
264 BootFatX = ExAllocatePoolWithTag(NonPagedPool, sizeof(struct _BootSectorFatX), TAG_VFAT);
265 if (BootFatX == NULL)
266 {
267 *RecognizedFS=FALSE;
268 return STATUS_INSUFFICIENT_RESOURCES;
269 }
270
271 Offset.QuadPart = 0;
272
273 /* Try to recognize FATX16/FATX32 partitions (Xbox) */
274 Status = VfatReadDisk(DeviceToMount, &Offset, sizeof(struct _BootSectorFatX), (PUCHAR) BootFatX, Override);
275 if (NT_SUCCESS(Status))
276 {
277 *RecognizedFS = TRUE;
278 if (BootFatX->SysType[0] != 'F' ||
279 BootFatX->SysType[1] != 'A' ||
280 BootFatX->SysType[2] != 'T' ||
281 BootFatX->SysType[3] != 'X')
282 {
283 DPRINT1("SysType %c%c%c%c\n", BootFatX->SysType[0], BootFatX->SysType[1], BootFatX->SysType[2], BootFatX->SysType[3]);
284 *RecognizedFS=FALSE;
285 }
286
287 if (*RecognizedFS &&
288 BootFatX->SectorsPerCluster != 1 &&
289 BootFatX->SectorsPerCluster != 2 &&
290 BootFatX->SectorsPerCluster != 4 &&
291 BootFatX->SectorsPerCluster != 8 &&
292 BootFatX->SectorsPerCluster != 16 &&
293 BootFatX->SectorsPerCluster != 32 &&
294 BootFatX->SectorsPerCluster != 64 &&
295 BootFatX->SectorsPerCluster != 128)
296 {
297 DPRINT1("SectorsPerCluster %lu\n", BootFatX->SectorsPerCluster);
298 *RecognizedFS=FALSE;
299 }
300
301 if (*RecognizedFS)
302 {
303 FatInfo.BytesPerSector = DiskGeometry.BytesPerSector;
304 FatInfo.SectorsPerCluster = BootFatX->SectorsPerCluster;
305 FatInfo.rootDirectorySectors = BootFatX->SectorsPerCluster;
306 FatInfo.BytesPerCluster = BootFatX->SectorsPerCluster * DiskGeometry.BytesPerSector;
307 FatInfo.Sectors = (ULONG)(PartitionInfo.PartitionLength.QuadPart / DiskGeometry.BytesPerSector);
308 if (FatInfo.Sectors / FatInfo.SectorsPerCluster < 65525)
309 {
310 DPRINT("FATX16\n");
311 FatInfo.FatType = FATX16;
312 }
313 else
314 {
315 DPRINT("FATX32\n");
316 FatInfo.FatType = FATX32;
317 }
318 FatInfo.VolumeID = BootFatX->VolumeID;
319 FatInfo.FATStart = sizeof(struct _BootSectorFatX) / DiskGeometry.BytesPerSector;
320 FatInfo.FATCount = BootFatX->FATCount;
321 FatInfo.FATSectors =
322 ROUND_UP(FatInfo.Sectors / FatInfo.SectorsPerCluster * (FatInfo.FatType == FATX16 ? 2 : 4), 4096) /
323 FatInfo.BytesPerSector;
324 FatInfo.rootStart = FatInfo.FATStart + FatInfo.FATCount * FatInfo.FATSectors;
325 FatInfo.RootCluster = (FatInfo.rootStart - 1) / FatInfo.SectorsPerCluster;
326 FatInfo.dataStart = FatInfo.rootStart + FatInfo.rootDirectorySectors;
327 FatInfo.NumberOfClusters = (FatInfo.Sectors - FatInfo.dataStart) / FatInfo.SectorsPerCluster;
328
329 if (pFatInfo && *RecognizedFS)
330 {
331 *pFatInfo = FatInfo;
332 }
333 }
334 }
335 ExFreePool(BootFatX);
336 }
337
338 DPRINT("VfatHasFileSystem done\n");
339 return Status;
340 }
341
342 /*
343 * FUNCTION: Read the volume label
344 * WARNING: Read this comment carefully before using it (and using it wrong)
345 * Device parameter is expected to be the lower DO is start isn't 0
346 * otherwise, it is expected to be the VCB is start is 0
347 * Start parameter is expected to be, in bytes, the beginning of the root start.
348 * Set it to 0 if you wish to use the associated FCB with caching.
349 * In that specific case, Device parameter is expected to be the VCB!
350 * VolumeLabel parameter is expected to be a preallocated UNICODE_STRING (ie, with buffer)
351 * Its buffer has to be able to contain MAXIMUM_VOLUME_LABEL_LENGTH bytes
352 */
353 static
354 NTSTATUS
355 ReadVolumeLabel(
356 PVOID Device,
357 ULONG Start,
358 BOOLEAN IsFatX,
359 PUNICODE_STRING VolumeLabel)
360 {
361 PDEVICE_EXTENSION DeviceExt;
362 PDEVICE_OBJECT DeviceObject;
363 PVOID Context = NULL;
364 ULONG DirIndex = 0;
365 PDIR_ENTRY Entry;
366 PVFATFCB pFcb;
367 LARGE_INTEGER FileOffset;
368 ULONG SizeDirEntry;
369 ULONG EntriesPerPage;
370 OEM_STRING StringO;
371 BOOLEAN NoCache = (Start != 0);
372 PVOID Buffer;
373 NTSTATUS Status = STATUS_SUCCESS;
374
375 if (IsFatX)
376 {
377 SizeDirEntry = sizeof(FATX_DIR_ENTRY);
378 EntriesPerPage = FATX_ENTRIES_PER_PAGE;
379 }
380 else
381 {
382 SizeDirEntry = sizeof(FAT_DIR_ENTRY);
383 EntriesPerPage = FAT_ENTRIES_PER_PAGE;
384 }
385
386 FileOffset.QuadPart = Start;
387 if (!NoCache)
388 {
389 DeviceExt = Device;
390
391 /* FIXME: Check we really have a VCB
392 ASSERT();
393 */
394
395 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
396 pFcb = vfatOpenRootFCB(DeviceExt);
397 ExReleaseResourceLite(&DeviceExt->DirResource);
398
399 _SEH2_TRY
400 {
401 CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, MAP_WAIT, &Context, (PVOID*)&Entry);
402 }
403 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
404 {
405 Status = _SEH2_GetExceptionCode();
406 }
407 _SEH2_END;
408 }
409 else
410 {
411 DeviceObject = Device;
412
413 ASSERT(DeviceObject->Type == 3);
414
415 Buffer = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, TAG_VFAT);
416 if (Buffer != NULL)
417 {
418 Status = VfatReadDisk(DeviceObject, &FileOffset, PAGE_SIZE, (PUCHAR)Buffer, TRUE);
419 if (!NT_SUCCESS(Status))
420 {
421 ExFreePoolWithTag(Buffer, TAG_VFAT);
422 }
423 else
424 {
425 Entry = Buffer;
426 }
427 }
428 else
429 {
430 Status = STATUS_INSUFFICIENT_RESOURCES;
431 }
432 }
433
434 if (NT_SUCCESS(Status))
435 {
436 while (TRUE)
437 {
438 if (ENTRY_VOLUME(IsFatX, Entry))
439 {
440 /* copy volume label */
441 if (IsFatX)
442 {
443 StringO.Buffer = (PCHAR)Entry->FatX.Filename;
444 StringO.MaximumLength = StringO.Length = Entry->FatX.FilenameLength;
445 RtlOemStringToUnicodeString(VolumeLabel, &StringO, FALSE);
446 }
447 else
448 {
449 vfat8Dot3ToString(&Entry->Fat, VolumeLabel);
450 }
451 break;
452 }
453 if (ENTRY_END(IsFatX, Entry))
454 {
455 break;
456 }
457 DirIndex++;
458 Entry = (PDIR_ENTRY)((ULONG_PTR)Entry + SizeDirEntry);
459 if ((DirIndex % EntriesPerPage) == 0)
460 {
461 FileOffset.u.LowPart += PAGE_SIZE;
462
463 if (!NoCache)
464 {
465 CcUnpinData(Context);
466
467 _SEH2_TRY
468 {
469 CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, MAP_WAIT, &Context, (PVOID*)&Entry);
470 }
471 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
472 {
473 Status = _SEH2_GetExceptionCode();
474 }
475 _SEH2_END;
476 if (!NT_SUCCESS(Status))
477 {
478 Context = NULL;
479 break;
480 }
481 }
482 else
483 {
484 Status = VfatReadDisk(DeviceObject, &FileOffset, PAGE_SIZE, (PUCHAR)Buffer, TRUE);
485 if (!NT_SUCCESS(Status))
486 {
487 break;
488 }
489 Entry = Buffer;
490 }
491 }
492 }
493 if (Context)
494 {
495 CcUnpinData(Context);
496 }
497 else if (NoCache)
498 {
499 ExFreePoolWithTag(Buffer, TAG_VFAT);
500 }
501 }
502
503 if (!NoCache)
504 {
505 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
506 vfatReleaseFCB(DeviceExt, pFcb);
507 ExReleaseResourceLite(&DeviceExt->DirResource);
508 }
509
510 return STATUS_SUCCESS;
511 }
512
513
514 /*
515 * FUNCTION: Mount the filesystem
516 */
517 static
518 NTSTATUS
519 VfatMount(
520 PVFAT_IRP_CONTEXT IrpContext)
521 {
522 PDEVICE_OBJECT DeviceObject = NULL;
523 PDEVICE_EXTENSION DeviceExt = NULL;
524 BOOLEAN RecognizedFS;
525 NTSTATUS Status;
526 PVFATFCB Fcb = NULL;
527 PVFATFCB VolumeFcb = NULL;
528 PVFATCCB Ccb = NULL;
529 PDEVICE_OBJECT DeviceToMount;
530 PVPB Vpb;
531 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\$$Fat$$");
532 UNICODE_STRING VolumeNameU = RTL_CONSTANT_STRING(L"\\$$Volume$$");
533 UNICODE_STRING VolumeLabelU;
534 ULONG HashTableSize;
535 ULONG i;
536 FATINFO FatInfo;
537 BOOLEAN Dirty;
538
539 DPRINT("VfatMount(IrpContext %p)\n", IrpContext);
540
541 ASSERT(IrpContext);
542
543 if (IrpContext->DeviceObject != VfatGlobalData->DeviceObject)
544 {
545 Status = STATUS_INVALID_DEVICE_REQUEST;
546 goto ByeBye;
547 }
548
549 DeviceToMount = IrpContext->Stack->Parameters.MountVolume.DeviceObject;
550 Vpb = IrpContext->Stack->Parameters.MountVolume.Vpb;
551
552 Status = VfatHasFileSystem(DeviceToMount, &RecognizedFS, &FatInfo, FALSE);
553 if (!NT_SUCCESS(Status))
554 {
555 goto ByeBye;
556 }
557
558 if (RecognizedFS == FALSE)
559 {
560 DPRINT("VFAT: Unrecognized Volume\n");
561 Status = STATUS_UNRECOGNIZED_VOLUME;
562 goto ByeBye;
563 }
564
565 /* Use prime numbers for the table size */
566 if (FatInfo.FatType == FAT12)
567 {
568 HashTableSize = 4099; // 4096 = 4 * 1024
569 }
570 else if (FatInfo.FatType == FAT16 ||
571 FatInfo.FatType == FATX16)
572 {
573 HashTableSize = 16411; // 16384 = 16 * 1024
574 }
575 else
576 {
577 HashTableSize = 65537; // 65536 = 64 * 1024;
578 }
579 DPRINT("VFAT: Recognized volume\n");
580 Status = IoCreateDevice(VfatGlobalData->DriverObject,
581 ROUND_UP(sizeof (DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize,
582 NULL,
583 FILE_DEVICE_DISK_FILE_SYSTEM,
584 DeviceToMount->Characteristics,
585 FALSE,
586 &DeviceObject);
587 if (!NT_SUCCESS(Status))
588 {
589 goto ByeBye;
590 }
591
592 DeviceExt = DeviceObject->DeviceExtension;
593 RtlZeroMemory(DeviceExt, ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)) + sizeof(HASHENTRY*) * HashTableSize);
594 DeviceExt->FcbHashTable = (HASHENTRY**)((ULONG_PTR)DeviceExt + ROUND_UP(sizeof(DEVICE_EXTENSION), sizeof(ULONG)));
595 DeviceExt->HashTableSize = HashTableSize;
596 DeviceExt->VolumeDevice = DeviceObject;
597
598 /* use same vpb as device disk */
599 DeviceObject->Vpb = Vpb;
600 DeviceToMount->Vpb = Vpb;
601
602 RtlCopyMemory(&DeviceExt->FatInfo, &FatInfo, sizeof(FATINFO));
603
604 DPRINT("BytesPerSector: %u\n", DeviceExt->FatInfo.BytesPerSector);
605 DPRINT("SectorsPerCluster: %u\n", DeviceExt->FatInfo.SectorsPerCluster);
606 DPRINT("FATCount: %u\n", DeviceExt->FatInfo.FATCount);
607 DPRINT("FATSectors: %u\n", DeviceExt->FatInfo.FATSectors);
608 DPRINT("RootStart: %u\n", DeviceExt->FatInfo.rootStart);
609 DPRINT("DataStart: %u\n", DeviceExt->FatInfo.dataStart);
610 if (DeviceExt->FatInfo.FatType == FAT32)
611 {
612 DPRINT("RootCluster: %u\n", DeviceExt->FatInfo.RootCluster);
613 }
614
615 switch (DeviceExt->FatInfo.FatType)
616 {
617 case FAT12:
618 DeviceExt->GetNextCluster = FAT12GetNextCluster;
619 DeviceExt->FindAndMarkAvailableCluster = FAT12FindAndMarkAvailableCluster;
620 DeviceExt->WriteCluster = FAT12WriteCluster;
621 /* We don't define dirty bit functions here
622 * FAT12 doesn't have such bit and they won't get called
623 */
624 break;
625
626 case FAT16:
627 case FATX16:
628 DeviceExt->GetNextCluster = FAT16GetNextCluster;
629 DeviceExt->FindAndMarkAvailableCluster = FAT16FindAndMarkAvailableCluster;
630 DeviceExt->WriteCluster = FAT16WriteCluster;
631 DeviceExt->GetDirtyStatus = FAT16GetDirtyStatus;
632 DeviceExt->SetDirtyStatus = FAT16SetDirtyStatus;
633 break;
634
635 case FAT32:
636 case FATX32:
637 DeviceExt->GetNextCluster = FAT32GetNextCluster;
638 DeviceExt->FindAndMarkAvailableCluster = FAT32FindAndMarkAvailableCluster;
639 DeviceExt->WriteCluster = FAT32WriteCluster;
640 DeviceExt->GetDirtyStatus = FAT32GetDirtyStatus;
641 DeviceExt->SetDirtyStatus = FAT32SetDirtyStatus;
642 break;
643 }
644
645 if (DeviceExt->FatInfo.FatType == FATX16 ||
646 DeviceExt->FatInfo.FatType == FATX32)
647 {
648 DeviceExt->Flags |= VCB_IS_FATX;
649 DeviceExt->BaseDateYear = 2000;
650 RtlCopyMemory(&DeviceExt->Dispatch, &FatXDispatch, sizeof(VFAT_DISPATCH));
651 }
652 else
653 {
654 DeviceExt->BaseDateYear = 1980;
655 RtlCopyMemory(&DeviceExt->Dispatch, &FatDispatch, sizeof(VFAT_DISPATCH));
656 }
657
658 DeviceExt->StorageDevice = DeviceToMount;
659 DeviceExt->StorageDevice->Vpb->DeviceObject = DeviceObject;
660 DeviceExt->StorageDevice->Vpb->RealDevice = DeviceExt->StorageDevice;
661 DeviceExt->StorageDevice->Vpb->Flags |= VPB_MOUNTED;
662 DeviceObject->StackSize = DeviceExt->StorageDevice->StackSize + 1;
663 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
664
665 DPRINT("FsDeviceObject %p\n", DeviceObject);
666
667 /* Initialize this resource early ... it's used in VfatCleanup */
668 ExInitializeResourceLite(&DeviceExt->DirResource);
669
670 DeviceExt->IoVPB = DeviceObject->Vpb;
671 DeviceExt->SpareVPB = ExAllocatePoolWithTag(NonPagedPool, sizeof(VPB), TAG_VFAT);
672 if (DeviceExt->SpareVPB == NULL)
673 {
674 Status = STATUS_INSUFFICIENT_RESOURCES;
675 goto ByeBye;
676 }
677
678 DeviceExt->Statistics = ExAllocatePoolWithTag(NonPagedPool,
679 sizeof(STATISTICS) * VfatGlobalData->NumberProcessors,
680 TAG_VFAT);
681 if (DeviceExt->Statistics == NULL)
682 {
683 Status = STATUS_INSUFFICIENT_RESOURCES;
684 goto ByeBye;
685 }
686
687 RtlZeroMemory(DeviceExt->Statistics, sizeof(STATISTICS) * VfatGlobalData->NumberProcessors);
688 for (i = 0; i < VfatGlobalData->NumberProcessors; ++i)
689 {
690 DeviceExt->Statistics[i].Base.FileSystemType = FILESYSTEM_STATISTICS_TYPE_FAT;
691 DeviceExt->Statistics[i].Base.Version = 1;
692 DeviceExt->Statistics[i].Base.SizeOfCompleteStructure = sizeof(STATISTICS);
693 }
694
695 DeviceExt->FATFileObject = IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
696 Fcb = vfatNewFCB(DeviceExt, &NameU);
697 if (Fcb == NULL)
698 {
699 Status = STATUS_INSUFFICIENT_RESOURCES;
700 goto ByeBye;
701 }
702
703 Ccb = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
704 if (Ccb == NULL)
705 {
706 Status = STATUS_INSUFFICIENT_RESOURCES;
707 goto ByeBye;
708 }
709
710 RtlZeroMemory(Ccb, sizeof (VFATCCB));
711 DeviceExt->FATFileObject->FsContext = Fcb;
712 DeviceExt->FATFileObject->FsContext2 = Ccb;
713 DeviceExt->FATFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
714 DeviceExt->FATFileObject->PrivateCacheMap = NULL;
715 DeviceExt->FATFileObject->Vpb = DeviceObject->Vpb;
716 Fcb->FileObject = DeviceExt->FATFileObject;
717
718 Fcb->Flags |= FCB_IS_FAT;
719
720 Fcb->RFCB.FileSize.QuadPart = DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector;
721 Fcb->RFCB.ValidDataLength = Fcb->RFCB.FileSize;
722 Fcb->RFCB.AllocationSize = Fcb->RFCB.FileSize;
723
724 _SEH2_TRY
725 {
726 CcInitializeCacheMap(DeviceExt->FATFileObject,
727 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
728 TRUE,
729 &VfatGlobalData->CacheMgrCallbacks,
730 Fcb);
731 }
732 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
733 {
734 Status = _SEH2_GetExceptionCode();
735 goto ByeBye;
736 }
737 _SEH2_END;
738
739 DeviceExt->LastAvailableCluster = 2;
740 ExInitializeResourceLite(&DeviceExt->FatResource);
741
742 InitializeListHead(&DeviceExt->FcbListHead);
743
744 VolumeFcb = vfatNewFCB(DeviceExt, &VolumeNameU);
745 if (VolumeFcb == NULL)
746 {
747 Status = STATUS_INSUFFICIENT_RESOURCES;
748 goto ByeBye;
749 }
750
751 VolumeFcb->Flags = FCB_IS_VOLUME;
752 VolumeFcb->RFCB.FileSize.QuadPart = (LONGLONG) DeviceExt->FatInfo.Sectors * DeviceExt->FatInfo.BytesPerSector;
753 VolumeFcb->RFCB.ValidDataLength = VolumeFcb->RFCB.FileSize;
754 VolumeFcb->RFCB.AllocationSize = VolumeFcb->RFCB.FileSize;
755 DeviceExt->VolumeFcb = VolumeFcb;
756
757 ExAcquireResourceExclusiveLite(&VfatGlobalData->VolumeListLock, TRUE);
758 InsertHeadList(&VfatGlobalData->VolumeListHead, &DeviceExt->VolumeListEntry);
759 ExReleaseResourceLite(&VfatGlobalData->VolumeListLock);
760
761 /* read serial number */
762 DeviceObject->Vpb->SerialNumber = DeviceExt->FatInfo.VolumeID;
763
764 /* read volume label */
765 VolumeLabelU.Buffer = DeviceObject->Vpb->VolumeLabel;
766 VolumeLabelU.Length = 0;
767 VolumeLabelU.MaximumLength = sizeof(DeviceObject->Vpb->VolumeLabel);
768 ReadVolumeLabel(DeviceExt, 0, vfatVolumeIsFatX(DeviceExt), &VolumeLabelU);
769 Vpb->VolumeLabelLength = VolumeLabelU.Length;
770
771 /* read dirty bit status */
772 Status = GetDirtyStatus(DeviceExt, &Dirty);
773 if (NT_SUCCESS(Status))
774 {
775 /* The volume wasn't dirty, it was properly dismounted */
776 if (!Dirty)
777 {
778 /* Mark it dirty now! */
779 SetDirtyStatus(DeviceExt, TRUE);
780 VolumeFcb->Flags |= VCB_CLEAR_DIRTY;
781 }
782 else
783 {
784 DPRINT1("Mounting a dirty volume\n");
785 }
786 }
787
788 VolumeFcb->Flags |= VCB_IS_DIRTY;
789 if (BooleanFlagOn(Vpb->RealDevice->Flags, DO_SYSTEM_BOOT_PARTITION))
790 {
791 SetFlag(DeviceExt->Flags, VCB_IS_SYS_OR_HAS_PAGE);
792 }
793
794 FsRtlNotifyVolumeEvent(DeviceExt->FATFileObject, FSRTL_VOLUME_MOUNT);
795 FsRtlNotifyInitializeSync(&DeviceExt->NotifySync);
796 InitializeListHead(&DeviceExt->NotifyList);
797
798 DPRINT("Mount success\n");
799
800 Status = STATUS_SUCCESS;
801
802 ByeBye:
803 if (!NT_SUCCESS(Status))
804 {
805 /* Cleanup */
806 if (DeviceExt && DeviceExt->FATFileObject)
807 ObDereferenceObject (DeviceExt->FATFileObject);
808 if (DeviceExt && DeviceExt->SpareVPB)
809 ExFreePoolWithTag(DeviceExt->SpareVPB, TAG_VFAT);
810 if (DeviceExt && DeviceExt->Statistics)
811 ExFreePoolWithTag(DeviceExt->Statistics, TAG_VFAT);
812 if (Fcb)
813 vfatDestroyFCB(Fcb);
814 if (Ccb)
815 vfatDestroyCCB(Ccb);
816 if (DeviceObject)
817 IoDeleteDevice(DeviceObject);
818 }
819
820 return Status;
821 }
822
823
824 /*
825 * FUNCTION: Verify the filesystem
826 */
827 static
828 NTSTATUS
829 VfatVerify(
830 PVFAT_IRP_CONTEXT IrpContext)
831 {
832 PDEVICE_OBJECT DeviceToVerify;
833 NTSTATUS Status;
834 FATINFO FatInfo;
835 BOOLEAN RecognizedFS;
836 PDEVICE_EXTENSION DeviceExt;
837 BOOLEAN AllowRaw;
838 PVPB Vpb;
839 ULONG ChangeCount, BufSize = sizeof(ChangeCount);
840
841 DPRINT("VfatVerify(IrpContext %p)\n", IrpContext);
842
843 DeviceToVerify = IrpContext->Stack->Parameters.VerifyVolume.DeviceObject;
844 DeviceExt = DeviceToVerify->DeviceExtension;
845 Vpb = IrpContext->Stack->Parameters.VerifyVolume.Vpb;
846 AllowRaw = BooleanFlagOn(IrpContext->Stack->Flags, SL_ALLOW_RAW_MOUNT);
847
848 if (!BooleanFlagOn(Vpb->RealDevice->Flags, DO_VERIFY_VOLUME))
849 {
850 DPRINT("Already verified\n");
851 return STATUS_SUCCESS;
852 }
853
854 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
855 IOCTL_DISK_CHECK_VERIFY,
856 NULL,
857 0,
858 &ChangeCount,
859 &BufSize,
860 TRUE);
861 if (!NT_SUCCESS(Status) && Status != STATUS_VERIFY_REQUIRED)
862 {
863 DPRINT("VfatBlockDeviceIoControl() failed (Status %lx)\n", Status);
864 Status = (AllowRaw ? STATUS_WRONG_VOLUME : Status);
865 }
866 else
867 {
868 Status = VfatHasFileSystem(DeviceExt->StorageDevice, &RecognizedFS, &FatInfo, TRUE);
869 if (!NT_SUCCESS(Status) || RecognizedFS == FALSE)
870 {
871 if (NT_SUCCESS(Status) || AllowRaw)
872 {
873 Status = STATUS_WRONG_VOLUME;
874 }
875 }
876 else if (sizeof(FATINFO) == RtlCompareMemory(&FatInfo, &DeviceExt->FatInfo, sizeof(FATINFO)))
877 {
878 WCHAR BufferU[MAXIMUM_VOLUME_LABEL_LENGTH / sizeof(WCHAR)];
879 UNICODE_STRING VolumeLabelU;
880 UNICODE_STRING VpbLabelU;
881
882 VolumeLabelU.Buffer = BufferU;
883 VolumeLabelU.Length = 0;
884 VolumeLabelU.MaximumLength = sizeof(BufferU);
885 Status = ReadVolumeLabel(DeviceExt->StorageDevice, FatInfo.rootStart * FatInfo.BytesPerSector, (FatInfo.FatType >= FATX16), &VolumeLabelU);
886 if (!NT_SUCCESS(Status))
887 {
888 if (AllowRaw)
889 {
890 Status = STATUS_WRONG_VOLUME;
891 }
892 }
893 else
894 {
895 VpbLabelU.Buffer = Vpb->VolumeLabel;
896 VpbLabelU.Length = Vpb->VolumeLabelLength;
897 VpbLabelU.MaximumLength = sizeof(Vpb->VolumeLabel);
898
899 if (RtlCompareUnicodeString(&VpbLabelU, &VolumeLabelU, FALSE) != 0)
900 {
901 Status = STATUS_WRONG_VOLUME;
902 }
903 else
904 {
905 DPRINT1("Same volume\n");
906 }
907 }
908 }
909 else
910 {
911 Status = STATUS_WRONG_VOLUME;
912 }
913 }
914
915 Vpb->RealDevice->Flags &= ~DO_VERIFY_VOLUME;
916
917 return Status;
918 }
919
920
921 static
922 NTSTATUS
923 VfatGetVolumeBitmap(
924 PVFAT_IRP_CONTEXT IrpContext)
925 {
926 DPRINT("VfatGetVolumeBitmap (IrpContext %p)\n", IrpContext);
927 return STATUS_INVALID_DEVICE_REQUEST;
928 }
929
930
931 static
932 NTSTATUS
933 VfatGetRetrievalPointers(
934 PVFAT_IRP_CONTEXT IrpContext)
935 {
936 PIO_STACK_LOCATION Stack;
937 LARGE_INTEGER Vcn;
938 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers;
939 PFILE_OBJECT FileObject;
940 ULONG MaxExtentCount;
941 PVFATFCB Fcb;
942 PDEVICE_EXTENSION DeviceExt;
943 ULONG FirstCluster;
944 ULONG CurrentCluster;
945 ULONG LastCluster;
946 NTSTATUS Status;
947
948 DPRINT("VfatGetRetrievalPointers(IrpContext %p)\n", IrpContext);
949
950 DeviceExt = IrpContext->DeviceExt;
951 FileObject = IrpContext->FileObject;
952 Stack = IrpContext->Stack;
953 if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(STARTING_VCN_INPUT_BUFFER) ||
954 Stack->Parameters.DeviceIoControl.Type3InputBuffer == NULL)
955 {
956 return STATUS_INVALID_PARAMETER;
957 }
958
959 if (IrpContext->Irp->UserBuffer == NULL ||
960 Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(RETRIEVAL_POINTERS_BUFFER))
961 {
962 return STATUS_BUFFER_TOO_SMALL;
963 }
964
965 Fcb = FileObject->FsContext;
966
967 ExAcquireResourceSharedLite(&Fcb->MainResource, TRUE);
968
969 Vcn = ((PSTARTING_VCN_INPUT_BUFFER)Stack->Parameters.DeviceIoControl.Type3InputBuffer)->StartingVcn;
970 RetrievalPointers = IrpContext->Irp->UserBuffer;
971
972 MaxExtentCount = ((Stack->Parameters.DeviceIoControl.OutputBufferLength - sizeof(RetrievalPointers->ExtentCount) - sizeof(RetrievalPointers->StartingVcn)) / sizeof(RetrievalPointers->Extents[0]));
973
974 if (Vcn.QuadPart >= Fcb->RFCB.AllocationSize.QuadPart / DeviceExt->FatInfo.BytesPerCluster)
975 {
976 Status = STATUS_INVALID_PARAMETER;
977 goto ByeBye;
978 }
979
980 CurrentCluster = FirstCluster = vfatDirEntryGetFirstCluster(DeviceExt, &Fcb->entry);
981 Status = OffsetToCluster(DeviceExt, FirstCluster,
982 Vcn.u.LowPart * DeviceExt->FatInfo.BytesPerCluster,
983 &CurrentCluster, FALSE);
984 if (!NT_SUCCESS(Status))
985 {
986 goto ByeBye;
987 }
988
989 RetrievalPointers->StartingVcn = Vcn;
990 RetrievalPointers->ExtentCount = 0;
991 RetrievalPointers->Extents[0].Lcn.u.HighPart = 0;
992 RetrievalPointers->Extents[0].Lcn.u.LowPart = CurrentCluster - 2;
993 LastCluster = 0;
994 while (CurrentCluster != 0xffffffff && RetrievalPointers->ExtentCount < MaxExtentCount)
995 {
996 LastCluster = CurrentCluster;
997 Status = NextCluster(DeviceExt, CurrentCluster, &CurrentCluster, FALSE);
998 Vcn.QuadPart++;
999 if (!NT_SUCCESS(Status))
1000 {
1001 goto ByeBye;
1002 }
1003
1004 if (LastCluster + 1 != CurrentCluster)
1005 {
1006 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].NextVcn = Vcn;
1007 RetrievalPointers->ExtentCount++;
1008 if (RetrievalPointers->ExtentCount < MaxExtentCount)
1009 {
1010 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.HighPart = 0;
1011 RetrievalPointers->Extents[RetrievalPointers->ExtentCount].Lcn.u.LowPart = CurrentCluster - 2;
1012 }
1013 }
1014 }
1015
1016 IrpContext->Irp->IoStatus.Information = sizeof(RETRIEVAL_POINTERS_BUFFER) + (sizeof(RetrievalPointers->Extents[0]) * (RetrievalPointers->ExtentCount - 1));
1017 Status = STATUS_SUCCESS;
1018
1019 ByeBye:
1020 ExReleaseResourceLite(&Fcb->MainResource);
1021
1022 return Status;
1023 }
1024
1025 static
1026 NTSTATUS
1027 VfatMoveFile(
1028 PVFAT_IRP_CONTEXT IrpContext)
1029 {
1030 DPRINT("VfatMoveFile(IrpContext %p)\n", IrpContext);
1031 return STATUS_INVALID_DEVICE_REQUEST;
1032 }
1033
1034 static
1035 NTSTATUS
1036 VfatIsVolumeDirty(
1037 PVFAT_IRP_CONTEXT IrpContext)
1038 {
1039 PULONG Flags;
1040
1041 DPRINT("VfatIsVolumeDirty(IrpContext %p)\n", IrpContext);
1042
1043 if (IrpContext->Stack->Parameters.FileSystemControl.OutputBufferLength != sizeof(ULONG))
1044 return STATUS_INVALID_BUFFER_SIZE;
1045 else if (!IrpContext->Irp->AssociatedIrp.SystemBuffer)
1046 return STATUS_INVALID_USER_BUFFER;
1047
1048 Flags = (PULONG)IrpContext->Irp->AssociatedIrp.SystemBuffer;
1049 *Flags = 0;
1050
1051 if (BooleanFlagOn(IrpContext->DeviceExt->VolumeFcb->Flags, VCB_IS_DIRTY) &&
1052 !BooleanFlagOn(IrpContext->DeviceExt->VolumeFcb->Flags, VCB_CLEAR_DIRTY))
1053 {
1054 *Flags |= VOLUME_IS_DIRTY;
1055 }
1056
1057 IrpContext->Irp->IoStatus.Information = sizeof(ULONG);
1058
1059 return STATUS_SUCCESS;
1060 }
1061
1062 static
1063 NTSTATUS
1064 VfatMarkVolumeDirty(
1065 PVFAT_IRP_CONTEXT IrpContext)
1066 {
1067 PDEVICE_EXTENSION DeviceExt;
1068 NTSTATUS Status = STATUS_SUCCESS;
1069
1070 DPRINT("VfatMarkVolumeDirty(IrpContext %p)\n", IrpContext);
1071 DeviceExt = IrpContext->DeviceExt;
1072
1073 if (!BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_IS_DIRTY))
1074 {
1075 Status = SetDirtyStatus(DeviceExt, TRUE);
1076 }
1077
1078 DeviceExt->VolumeFcb->Flags &= ~VCB_CLEAR_DIRTY;
1079
1080 return Status;
1081 }
1082
1083 static
1084 NTSTATUS
1085 VfatLockOrUnlockVolume(
1086 PVFAT_IRP_CONTEXT IrpContext,
1087 BOOLEAN Lock)
1088 {
1089 PFILE_OBJECT FileObject;
1090 PDEVICE_EXTENSION DeviceExt;
1091 PVFATFCB Fcb;
1092 PVPB Vpb;
1093
1094 DPRINT("VfatLockOrUnlockVolume(%p, %d)\n", IrpContext, Lock);
1095
1096 DeviceExt = IrpContext->DeviceExt;
1097 FileObject = IrpContext->FileObject;
1098 Fcb = FileObject->FsContext;
1099 Vpb = DeviceExt->FATFileObject->Vpb;
1100
1101 /* Only allow locking with the volume open */
1102 if (!BooleanFlagOn(Fcb->Flags, FCB_IS_VOLUME))
1103 {
1104 return STATUS_ACCESS_DENIED;
1105 }
1106
1107 /* Bail out if it's already in the demanded state */
1108 if ((BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED) && Lock) ||
1109 (!BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED) && !Lock))
1110 {
1111 return STATUS_ACCESS_DENIED;
1112 }
1113
1114 /* Bail out if it's already in the demanded state */
1115 if ((BooleanFlagOn(Vpb->Flags, VPB_LOCKED) && Lock) ||
1116 (!BooleanFlagOn(Vpb->Flags, VPB_LOCKED) && !Lock))
1117 {
1118 return STATUS_ACCESS_DENIED;
1119 }
1120
1121 /* Deny locking if we're not alone */
1122 if (Lock && DeviceExt->OpenHandleCount != 1)
1123 {
1124 PLIST_ENTRY ListEntry;
1125
1126 #if 1
1127 /* FIXME: Hack that allows locking the system volume on
1128 * boot so that autochk can run properly
1129 * That hack is, on purpose, really restrictive
1130 * it will only allow locking with two directories
1131 * open: current directory of smss and autochk.
1132 */
1133 BOOLEAN ForceLock = TRUE;
1134 ULONG HandleCount = 0;
1135
1136 /* Only allow boot volume */
1137 if (BooleanFlagOn(DeviceExt->Flags, VCB_IS_SYS_OR_HAS_PAGE))
1138 {
1139 /* We'll browse all the FCB */
1140 ListEntry = DeviceExt->FcbListHead.Flink;
1141 while (ListEntry != &DeviceExt->FcbListHead)
1142 {
1143 Fcb = CONTAINING_RECORD(ListEntry, VFATFCB, FcbListEntry);
1144 ListEntry = ListEntry->Flink;
1145
1146 /* If no handle: that FCB is no problem for locking
1147 * so ignore it
1148 */
1149 if (Fcb->OpenHandleCount == 0)
1150 {
1151 continue;
1152 }
1153
1154 /* Not a dir? We're no longer at boot */
1155 if (!vfatFCBIsDirectory(Fcb))
1156 {
1157 ForceLock = FALSE;
1158 break;
1159 }
1160
1161 /* If we have cached initialized and several handles, we're
1162 not in the boot case
1163 */
1164 if (Fcb->FileObject != NULL && Fcb->OpenHandleCount > 1)
1165 {
1166 ForceLock = FALSE;
1167 break;
1168 }
1169
1170 /* Count the handles */
1171 HandleCount += Fcb->OpenHandleCount;
1172 /* More than two handles? Then, we're not booting anymore */
1173 if (HandleCount > 2)
1174 {
1175 ForceLock = FALSE;
1176 break;
1177 }
1178 }
1179 }
1180 else
1181 {
1182 ForceLock = FALSE;
1183 }
1184
1185 /* Here comes the hack, ignore the failure! */
1186 if (!ForceLock)
1187 {
1188 #endif
1189
1190 DPRINT1("Can't lock: %u opened\n", DeviceExt->OpenHandleCount);
1191
1192 ListEntry = DeviceExt->FcbListHead.Flink;
1193 while (ListEntry != &DeviceExt->FcbListHead)
1194 {
1195 Fcb = CONTAINING_RECORD(ListEntry, VFATFCB, FcbListEntry);
1196 ListEntry = ListEntry->Flink;
1197
1198 if (Fcb->OpenHandleCount > 0)
1199 {
1200 DPRINT1("Opened (%u - %u): %wZ\n", Fcb->OpenHandleCount, Fcb->RefCount, &Fcb->PathNameU);
1201 }
1202 }
1203
1204 return STATUS_ACCESS_DENIED;
1205
1206 #if 1
1207 /* End of the hack: be verbose about its usage,
1208 * just in case we would mess up everything!
1209 */
1210 }
1211 else
1212 {
1213 DPRINT1("HACK: Using lock-hack!\n");
1214 }
1215 #endif
1216 }
1217
1218 /* Finally, proceed */
1219 if (Lock)
1220 {
1221 DeviceExt->Flags |= VCB_VOLUME_LOCKED;
1222 Vpb->Flags |= VPB_LOCKED;
1223 }
1224 else
1225 {
1226 DeviceExt->Flags &= ~VCB_VOLUME_LOCKED;
1227 Vpb->Flags &= ~VPB_LOCKED;
1228 }
1229
1230 return STATUS_SUCCESS;
1231 }
1232
1233 static
1234 NTSTATUS
1235 VfatDismountVolume(
1236 PVFAT_IRP_CONTEXT IrpContext)
1237 {
1238 PDEVICE_EXTENSION DeviceExt;
1239 PLIST_ENTRY NextEntry;
1240 PVFATFCB Fcb;
1241 PFILE_OBJECT FileObject;
1242
1243 DPRINT("VfatDismountVolume(%p)\n", IrpContext);
1244
1245 DeviceExt = IrpContext->DeviceExt;
1246 FileObject = IrpContext->FileObject;
1247
1248 /* We HAVE to be locked. Windows also allows dismount with no lock
1249 * but we're here mainly for 1st stage, so KISS
1250 */
1251 if (!BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED))
1252 {
1253 return STATUS_ACCESS_DENIED;
1254 }
1255
1256 /* Deny dismount of boot volume */
1257 if (BooleanFlagOn(DeviceExt->Flags, VCB_IS_SYS_OR_HAS_PAGE))
1258 {
1259 return STATUS_ACCESS_DENIED;
1260 }
1261
1262 /* Race condition? */
1263 if (BooleanFlagOn(DeviceExt->Flags, VCB_DISMOUNT_PENDING))
1264 {
1265 return STATUS_VOLUME_DISMOUNTED;
1266 }
1267
1268 /* Notify we'll dismount. Pass that point there's no reason we fail */
1269 FsRtlNotifyVolumeEvent(IrpContext->Stack->FileObject, FSRTL_VOLUME_DISMOUNT);
1270
1271 ExAcquireResourceExclusiveLite(&DeviceExt->FatResource, TRUE);
1272
1273 /* We're performing a clean shutdown */
1274 if (BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_CLEAR_DIRTY))
1275 {
1276 /* Drop the dirty bit */
1277 if (NT_SUCCESS(SetDirtyStatus(DeviceExt, FALSE)))
1278 DeviceExt->VolumeFcb->Flags &= ~VCB_IS_DIRTY;
1279 }
1280
1281 /* Flush volume & files */
1282 VfatFlushVolume(DeviceExt, (PVFATFCB)FileObject->FsContext);
1283
1284 /* Rebrowse the FCB in order to free them now */
1285 while (!IsListEmpty(&DeviceExt->FcbListHead))
1286 {
1287 NextEntry = RemoveTailList(&DeviceExt->FcbListHead);
1288 Fcb = CONTAINING_RECORD(NextEntry, VFATFCB, FcbListEntry);
1289 vfatDestroyFCB(Fcb);
1290 }
1291
1292 /* Mark we're being dismounted */
1293 DeviceExt->Flags |= VCB_DISMOUNT_PENDING;
1294 #ifndef ENABLE_SWAPOUT
1295 IrpContext->DeviceObject->Vpb->Flags &= ~VPB_MOUNTED;
1296 #endif
1297
1298 ExReleaseResourceLite(&DeviceExt->FatResource);
1299
1300 /* Release a few resources and quit, we're done */
1301 ExDeleteResourceLite(&DeviceExt->DirResource);
1302 ExDeleteResourceLite(&DeviceExt->FatResource);
1303 ObDereferenceObject(DeviceExt->FATFileObject);
1304
1305 return STATUS_SUCCESS;
1306 }
1307
1308 static
1309 NTSTATUS
1310 VfatGetStatistics(
1311 PVFAT_IRP_CONTEXT IrpContext)
1312 {
1313 PVOID Buffer;
1314 ULONG Length;
1315 NTSTATUS Status;
1316 PDEVICE_EXTENSION DeviceExt;
1317
1318 DeviceExt = IrpContext->DeviceExt;
1319 Length = IrpContext->Stack->Parameters.FileSystemControl.OutputBufferLength;
1320 Buffer = IrpContext->Irp->AssociatedIrp.SystemBuffer;
1321
1322 if (Length < sizeof(FILESYSTEM_STATISTICS))
1323 {
1324 return STATUS_BUFFER_TOO_SMALL;
1325 }
1326
1327 if (Buffer == NULL)
1328 {
1329 return STATUS_INVALID_USER_BUFFER;
1330 }
1331
1332 if (Length >= sizeof(STATISTICS) * VfatGlobalData->NumberProcessors)
1333 {
1334 Length = sizeof(STATISTICS) * VfatGlobalData->NumberProcessors;
1335 Status = STATUS_SUCCESS;
1336 }
1337 else
1338 {
1339 Status = STATUS_BUFFER_OVERFLOW;
1340 }
1341
1342 RtlCopyMemory(Buffer, DeviceExt->Statistics, Length);
1343 IrpContext->Irp->IoStatus.Information = Length;
1344
1345 return Status;
1346 }
1347
1348 /*
1349 * FUNCTION: File system control
1350 */
1351 NTSTATUS
1352 VfatFileSystemControl(
1353 PVFAT_IRP_CONTEXT IrpContext)
1354 {
1355 NTSTATUS Status;
1356
1357 DPRINT("VfatFileSystemControl(IrpContext %p)\n", IrpContext);
1358
1359 ASSERT(IrpContext);
1360 ASSERT(IrpContext->Irp);
1361 ASSERT(IrpContext->Stack);
1362
1363 IrpContext->Irp->IoStatus.Information = 0;
1364
1365 switch (IrpContext->MinorFunction)
1366 {
1367 case IRP_MN_KERNEL_CALL:
1368 case IRP_MN_USER_FS_REQUEST:
1369 switch(IrpContext->Stack->Parameters.DeviceIoControl.IoControlCode)
1370 {
1371 case FSCTL_GET_VOLUME_BITMAP:
1372 Status = VfatGetVolumeBitmap(IrpContext);
1373 break;
1374
1375 case FSCTL_GET_RETRIEVAL_POINTERS:
1376 Status = VfatGetRetrievalPointers(IrpContext);
1377 break;
1378
1379 case FSCTL_MOVE_FILE:
1380 Status = VfatMoveFile(IrpContext);
1381 break;
1382
1383 case FSCTL_IS_VOLUME_DIRTY:
1384 Status = VfatIsVolumeDirty(IrpContext);
1385 break;
1386
1387 case FSCTL_MARK_VOLUME_DIRTY:
1388 Status = VfatMarkVolumeDirty(IrpContext);
1389 break;
1390
1391 case FSCTL_LOCK_VOLUME:
1392 Status = VfatLockOrUnlockVolume(IrpContext, TRUE);
1393 break;
1394
1395 case FSCTL_UNLOCK_VOLUME:
1396 Status = VfatLockOrUnlockVolume(IrpContext, FALSE);
1397 break;
1398
1399 case FSCTL_DISMOUNT_VOLUME:
1400 Status = VfatDismountVolume(IrpContext);
1401 break;
1402
1403 case FSCTL_FILESYSTEM_GET_STATISTICS:
1404 Status = VfatGetStatistics(IrpContext);
1405 break;
1406
1407 default:
1408 Status = STATUS_INVALID_DEVICE_REQUEST;
1409 }
1410 break;
1411
1412 case IRP_MN_MOUNT_VOLUME:
1413 Status = VfatMount(IrpContext);
1414 break;
1415
1416 case IRP_MN_VERIFY_VOLUME:
1417 DPRINT("VFATFS: IRP_MN_VERIFY_VOLUME\n");
1418 Status = VfatVerify(IrpContext);
1419 break;
1420
1421 default:
1422 DPRINT("VFAT FSC: MinorFunction %u\n", IrpContext->MinorFunction);
1423 Status = STATUS_INVALID_DEVICE_REQUEST;
1424 break;
1425 }
1426
1427 return Status;
1428 }