3 * Copyright (C) 2002 ReactOS Team
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystems/ntfs/fsctl.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
28 /* INCLUDES *****************************************************************/
37 /* FUNCTIONS ****************************************************************/
40 * FUNCTION: Tests if the device contains a filesystem that can be mounted
45 NtfsHasFileSystem(PDEVICE_OBJECT DeviceToMount
)
47 PARTITION_INFORMATION PartitionInfo
;
48 DISK_GEOMETRY DiskGeometry
;
49 ULONG ClusterSize
, Size
, k
;
50 PBOOT_SECTOR BootSector
;
53 DPRINT1("NtfsHasFileSystem() called\n");
55 Size
= sizeof(DISK_GEOMETRY
);
56 Status
= NtfsDeviceIoControl(DeviceToMount
,
57 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
63 if (!NT_SUCCESS(Status
))
65 DPRINT1("NtfsDeviceIoControl() failed (Status %lx)\n", Status
);
69 if (DiskGeometry
.MediaType
== FixedMedia
)
71 /* We have found a hard disk */
72 Size
= sizeof(PARTITION_INFORMATION
);
73 Status
= NtfsDeviceIoControl(DeviceToMount
,
74 IOCTL_DISK_GET_PARTITION_INFO
,
80 if (!NT_SUCCESS(Status
))
82 DPRINT1("NtfsDeviceIoControl() failed (Status %lx)\n", Status
);
86 if (PartitionInfo
.PartitionType
!= PARTITION_IFS
)
88 DPRINT1("Invalid partition type\n");
89 return STATUS_UNRECOGNIZED_VOLUME
;
93 DPRINT1("BytesPerSector: %lu\n", DiskGeometry
.BytesPerSector
);
94 BootSector
= ExAllocatePoolWithTag(NonPagedPool
,
95 DiskGeometry
.BytesPerSector
,
97 if (BootSector
== NULL
)
99 return STATUS_INSUFFICIENT_RESOURCES
;
102 Status
= NtfsReadSectors(DeviceToMount
,
105 DiskGeometry
.BytesPerSector
,
108 if (!NT_SUCCESS(Status
))
114 * Check values of different fields. If those fields have not expected
115 * values, we fail, to avoid mounting partitions that Windows won't mount.
118 /* OEMID: this field must be NTFS */
119 if (RtlCompareMemory(BootSector
->OEMID
, "NTFS ", 8) != 8)
121 DPRINT1("Failed with NTFS-identifier: [%.8s]\n", BootSector
->OEMID
);
122 Status
= STATUS_UNRECOGNIZED_VOLUME
;
126 /* Unused0: this field must be COMPLETELY null */
127 for (k
= 0; k
< 7; k
++)
129 if (BootSector
->BPB
.Unused0
[k
] != 0)
131 DPRINT1("Failed in field Unused0: [%.7s]\n", BootSector
->BPB
.Unused0
);
132 Status
= STATUS_UNRECOGNIZED_VOLUME
;
137 /* Unused3: this field must be COMPLETELY null */
138 for (k
= 0; k
< 4; k
++)
140 if (BootSector
->BPB
.Unused3
[k
] != 0)
142 DPRINT1("Failed in field Unused3: [%.4s]\n", BootSector
->BPB
.Unused3
);
143 Status
= STATUS_UNRECOGNIZED_VOLUME
;
148 /* Check cluster size */
149 ClusterSize
= BootSector
->BPB
.BytesPerSector
* BootSector
->BPB
.SectorsPerCluster
;
150 if (ClusterSize
!= 512 && ClusterSize
!= 1024 &&
151 ClusterSize
!= 2048 && ClusterSize
!= 4096 &&
152 ClusterSize
!= 8192 && ClusterSize
!= 16384 &&
153 ClusterSize
!= 32768 && ClusterSize
!= 65536)
155 DPRINT1("Cluster size failed: %hu, %hu, %hu\n",
156 BootSector
->BPB
.BytesPerSector
,
157 BootSector
->BPB
.SectorsPerCluster
,
159 Status
= STATUS_UNRECOGNIZED_VOLUME
;
164 ExFreePool(BootSector
);
172 NtfsQueryMftZoneReservation(VOID
)
174 ULONG ZoneReservation
= 1;
175 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
177 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
178 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
179 QueryTable
[0].Name
= L
"NtfsMftZoneReservation";
180 QueryTable
[0].EntryContext
= &ZoneReservation
;
182 RtlQueryRegistryValues(RTL_REGISTRY_CONTROL
,
188 return ZoneReservation
;
194 NtfsGetVolumeData(PDEVICE_OBJECT DeviceObject
,
195 PDEVICE_EXTENSION DeviceExt
)
197 DISK_GEOMETRY DiskGeometry
;
198 PFILE_RECORD_HEADER VolumeRecord
;
199 PVOLINFO_ATTRIBUTE VolumeInfo
;
200 PBOOT_SECTOR BootSector
;
202 PNTFS_INFO NtfsInfo
= &DeviceExt
->NtfsInfo
;
204 PNTFS_ATTR_CONTEXT AttrCtxt
;
205 PNTFS_ATTR_RECORD Attribute
;
209 DPRINT("NtfsGetVolumeData() called\n");
211 Size
= sizeof(DISK_GEOMETRY
);
212 Status
= NtfsDeviceIoControl(DeviceObject
,
213 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
219 if (!NT_SUCCESS(Status
))
221 DPRINT("NtfsDeviceIoControl() failed (Status %lx)\n", Status
);
225 DPRINT("BytesPerSector: %lu\n", DiskGeometry
.BytesPerSector
);
226 BootSector
= ExAllocatePoolWithTag(NonPagedPool
,
227 DiskGeometry
.BytesPerSector
,
229 if (BootSector
== NULL
)
231 return STATUS_INSUFFICIENT_RESOURCES
;
234 Status
= NtfsReadSectors(DeviceObject
,
235 0, /* Partition boot sector */
237 DiskGeometry
.BytesPerSector
,
240 if (!NT_SUCCESS(Status
))
242 ExFreePool(BootSector
);
246 /* Read data from the bootsector */
247 NtfsInfo
->BytesPerSector
= BootSector
->BPB
.BytesPerSector
;
248 NtfsInfo
->SectorsPerCluster
= BootSector
->BPB
.SectorsPerCluster
;
249 NtfsInfo
->BytesPerCluster
= BootSector
->BPB
.BytesPerSector
* BootSector
->BPB
.SectorsPerCluster
;
250 NtfsInfo
->SectorCount
= BootSector
->EBPB
.SectorCount
;
251 NtfsInfo
->ClusterCount
= DeviceExt
->NtfsInfo
.SectorCount
/ (ULONGLONG
)DeviceExt
->NtfsInfo
.SectorsPerCluster
;
253 NtfsInfo
->MftStart
.QuadPart
= BootSector
->EBPB
.MftLocation
;
254 NtfsInfo
->MftMirrStart
.QuadPart
= BootSector
->EBPB
.MftMirrLocation
;
255 NtfsInfo
->SerialNumber
= BootSector
->EBPB
.SerialNumber
;
256 if (BootSector
->EBPB
.ClustersPerMftRecord
> 0)
257 NtfsInfo
->BytesPerFileRecord
= BootSector
->EBPB
.ClustersPerMftRecord
* NtfsInfo
->BytesPerCluster
;
259 NtfsInfo
->BytesPerFileRecord
= 1 << (-BootSector
->EBPB
.ClustersPerMftRecord
);
260 if (BootSector
->EBPB
.ClustersPerIndexRecord
> 0)
261 NtfsInfo
->BytesPerIndexRecord
= BootSector
->EBPB
.ClustersPerIndexRecord
* NtfsInfo
->BytesPerCluster
;
263 NtfsInfo
->BytesPerIndexRecord
= 1 << (-BootSector
->EBPB
.ClustersPerIndexRecord
);
265 DPRINT("Boot sector information:\n");
266 DPRINT(" BytesPerSector: %hu\n", BootSector
->BPB
.BytesPerSector
);
267 DPRINT(" SectorsPerCluster: %hu\n", BootSector
->BPB
.SectorsPerCluster
);
268 DPRINT(" SectorCount: %I64u\n", BootSector
->EBPB
.SectorCount
);
269 DPRINT(" MftStart: %I64u\n", BootSector
->EBPB
.MftLocation
);
270 DPRINT(" MftMirrStart: %I64u\n", BootSector
->EBPB
.MftMirrLocation
);
271 DPRINT(" ClustersPerMftRecord: %lx\n", BootSector
->EBPB
.ClustersPerMftRecord
);
272 DPRINT(" ClustersPerIndexRecord: %lx\n", BootSector
->EBPB
.ClustersPerIndexRecord
);
273 DPRINT(" SerialNumber: %I64x\n", BootSector
->EBPB
.SerialNumber
);
275 ExFreePool(BootSector
);
277 DeviceExt
->MasterFileTable
= ExAllocatePoolWithTag(NonPagedPool
,
278 NtfsInfo
->BytesPerFileRecord
,
280 if (DeviceExt
->MasterFileTable
== NULL
)
282 return STATUS_INSUFFICIENT_RESOURCES
;
285 Status
= NtfsReadSectors(DeviceObject
,
286 NtfsInfo
->MftStart
.u
.LowPart
* NtfsInfo
->SectorsPerCluster
,
287 NtfsInfo
->BytesPerFileRecord
/ NtfsInfo
->BytesPerSector
,
288 NtfsInfo
->BytesPerSector
,
289 (PVOID
)DeviceExt
->MasterFileTable
,
291 if (!NT_SUCCESS(Status
))
293 DPRINT1("Failed reading MFT.\n");
294 ExFreePool(DeviceExt
->MasterFileTable
);
298 Status
= FindAttribute(DeviceExt
, DeviceExt
->MasterFileTable
, AttributeData
, L
"", 0, &DeviceExt
->MFTContext
);
299 if (!NT_SUCCESS(Status
))
301 DPRINT1("Can't find data attribute for Master File Table.\n");
302 ExFreePool(DeviceExt
->MasterFileTable
);
306 VolumeRecord
= ExAllocatePoolWithTag(NonPagedPool
,
307 NtfsInfo
->BytesPerFileRecord
,
309 if (VolumeRecord
== NULL
)
311 DPRINT1("Allocation failed for volume record\n");
312 ExFreePool(DeviceExt
->MasterFileTable
);
313 return STATUS_INSUFFICIENT_RESOURCES
;
316 /* Read Volume File (MFT index 3) */
317 DeviceExt
->StorageDevice
= DeviceObject
;
318 Status
= ReadFileRecord(DeviceExt
,
321 if (!NT_SUCCESS(Status
))
323 DPRINT1("Failed reading volume file\n");
324 ExFreePool(VolumeRecord
);
325 ExFreePool(DeviceExt
->MasterFileTable
);
329 /* Enumerate attributes */
330 NtfsDumpFileAttributes(DeviceExt
, DeviceExt
->MasterFileTable
);
332 /* Enumerate attributes */
333 NtfsDumpFileAttributes(DeviceExt
, VolumeRecord
);
335 /* Get volume name */
336 Status
= FindAttribute(DeviceExt
, VolumeRecord
, AttributeVolumeName
, L
"", 0, &AttrCtxt
);
338 if (NT_SUCCESS(Status
) && AttrCtxt
->Record
.Resident
.ValueLength
!= 0)
340 Attribute
= &AttrCtxt
->Record
;
341 DPRINT("Data length %lu\n", AttributeDataLength(Attribute
));
342 NtfsInfo
->VolumeLabelLength
=
343 min (Attribute
->Resident
.ValueLength
, MAXIMUM_VOLUME_LABEL_LENGTH
);
344 RtlCopyMemory(NtfsInfo
->VolumeLabel
,
345 (PVOID
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
),
346 NtfsInfo
->VolumeLabelLength
);
347 VolumeNameU
= NtfsInfo
->VolumeLabel
;
351 NtfsInfo
->VolumeLabelLength
= 0;
355 if (NT_SUCCESS(Status
))
357 ReleaseAttributeContext(AttrCtxt
);
360 VolumeFcb
= NtfsCreateFCB(VolumeNameU
, NULL
, DeviceExt
);
361 if (VolumeFcb
== NULL
)
363 DPRINT1("Failed allocating volume FCB\n");
364 ExFreePool(VolumeRecord
);
365 ExFreePool(DeviceExt
->MasterFileTable
);
366 return STATUS_INSUFFICIENT_RESOURCES
;
369 VolumeFcb
->Flags
= FCB_IS_VOLUME
;
370 VolumeFcb
->RFCB
.FileSize
.QuadPart
= DeviceExt
->NtfsInfo
.SectorCount
* DeviceExt
->NtfsInfo
.BytesPerSector
;
371 VolumeFcb
->RFCB
.ValidDataLength
= VolumeFcb
->RFCB
.FileSize
;
372 VolumeFcb
->RFCB
.AllocationSize
= VolumeFcb
->RFCB
.FileSize
;
373 VolumeFcb
->MFTIndex
= 0;
374 DeviceExt
->VolumeFcb
= VolumeFcb
;
376 /* Get volume information */
377 Status
= FindAttribute(DeviceExt
, VolumeRecord
, AttributeVolumeInformation
, L
"", 0, &AttrCtxt
);
379 if (NT_SUCCESS(Status
) && AttrCtxt
->Record
.Resident
.ValueLength
!= 0)
381 Attribute
= &AttrCtxt
->Record
;
382 DPRINT("Data length %lu\n", AttributeDataLength (Attribute
));
383 VolumeInfo
= (PVOID
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
385 NtfsInfo
->MajorVersion
= VolumeInfo
->MajorVersion
;
386 NtfsInfo
->MinorVersion
= VolumeInfo
->MinorVersion
;
387 NtfsInfo
->Flags
= VolumeInfo
->Flags
;
390 if (NT_SUCCESS(Status
))
392 ReleaseAttributeContext(AttrCtxt
);
395 ExFreePool(VolumeRecord
);
397 NtfsInfo
->MftZoneReservation
= NtfsQueryMftZoneReservation();
405 NtfsMountVolume(PDEVICE_OBJECT DeviceObject
,
408 PDEVICE_OBJECT NewDeviceObject
= NULL
;
409 PDEVICE_OBJECT DeviceToMount
;
410 PIO_STACK_LOCATION Stack
;
411 PNTFS_FCB Fcb
= NULL
;
412 PNTFS_CCB Ccb
= NULL
;
413 PNTFS_VCB Vcb
= NULL
;
416 DPRINT1("NtfsMountVolume() called\n");
418 if (DeviceObject
!= NtfsGlobalData
->DeviceObject
)
420 Status
= STATUS_INVALID_DEVICE_REQUEST
;
424 Stack
= IoGetCurrentIrpStackLocation(Irp
);
425 DeviceToMount
= Stack
->Parameters
.MountVolume
.DeviceObject
;
427 Status
= NtfsHasFileSystem(DeviceToMount
);
428 if (!NT_SUCCESS(Status
))
433 Status
= IoCreateDevice(NtfsGlobalData
->DriverObject
,
434 sizeof(DEVICE_EXTENSION
),
436 FILE_DEVICE_DISK_FILE_SYSTEM
,
440 if (!NT_SUCCESS(Status
))
443 NewDeviceObject
->Flags
|= DO_DIRECT_IO
;
444 Vcb
= (PVOID
)NewDeviceObject
->DeviceExtension
;
445 RtlZeroMemory(Vcb
, sizeof(NTFS_VCB
));
447 Vcb
->Identifier
.Type
= NTFS_TYPE_VCB
;
448 Vcb
->Identifier
.Size
= sizeof(NTFS_TYPE_VCB
);
450 Status
= NtfsGetVolumeData(DeviceToMount
,
452 if (!NT_SUCCESS(Status
))
455 NewDeviceObject
->Vpb
= DeviceToMount
->Vpb
;
457 Vcb
->StorageDevice
= DeviceToMount
;
458 Vcb
->StorageDevice
->Vpb
->DeviceObject
= NewDeviceObject
;
459 Vcb
->StorageDevice
->Vpb
->RealDevice
= Vcb
->StorageDevice
;
460 Vcb
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
461 NewDeviceObject
->StackSize
= Vcb
->StorageDevice
->StackSize
+ 1;
462 NewDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
464 Vcb
->StreamFileObject
= IoCreateStreamFileObject(NULL
,
467 InitializeListHead(&Vcb
->FcbListHead
);
469 Fcb
= NtfsCreateFCB(NULL
, NULL
, Vcb
);
472 Status
= STATUS_INSUFFICIENT_RESOURCES
;
476 Ccb
= ExAllocatePoolWithTag(NonPagedPool
,
481 Status
= STATUS_INSUFFICIENT_RESOURCES
;
485 RtlZeroMemory(Ccb
, sizeof(NTFS_CCB
));
487 Ccb
->Identifier
.Type
= NTFS_TYPE_CCB
;
488 Ccb
->Identifier
.Size
= sizeof(NTFS_TYPE_CCB
);
490 Vcb
->StreamFileObject
->FsContext
= Fcb
;
491 Vcb
->StreamFileObject
->FsContext2
= Ccb
;
492 Vcb
->StreamFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
493 Vcb
->StreamFileObject
->PrivateCacheMap
= NULL
;
494 Vcb
->StreamFileObject
->Vpb
= Vcb
->Vpb
;
495 Ccb
->PtrFileObject
= Vcb
->StreamFileObject
;
496 Fcb
->FileObject
= Vcb
->StreamFileObject
;
497 Fcb
->Vcb
= (PDEVICE_EXTENSION
)Vcb
->StorageDevice
;
499 Fcb
->Flags
= FCB_IS_VOLUME_STREAM
;
501 Fcb
->RFCB
.FileSize
.QuadPart
= Vcb
->NtfsInfo
.SectorCount
* Vcb
->NtfsInfo
.BytesPerSector
;
502 Fcb
->RFCB
.ValidDataLength
.QuadPart
= Vcb
->NtfsInfo
.SectorCount
* Vcb
->NtfsInfo
.BytesPerSector
;
503 Fcb
->RFCB
.AllocationSize
.QuadPart
= Vcb
->NtfsInfo
.SectorCount
* Vcb
->NtfsInfo
.BytesPerSector
; /* Correct? */
505 // Fcb->Entry.ExtentLocationL = 0;
506 // Fcb->Entry.DataLengthL = DeviceExt->CdInfo.VolumeSpaceSize * BLOCKSIZE;
510 CcInitializeCacheMap(Vcb
->StreamFileObject
,
511 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
513 &(NtfsGlobalData
->CacheMgrCallbacks
),
516 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
518 Status
= _SEH2_GetExceptionCode();
523 ExInitializeResourceLite(&Vcb
->DirResource
);
525 KeInitializeSpinLock(&Vcb
->FcbListLock
);
527 /* Get serial number */
528 NewDeviceObject
->Vpb
->SerialNumber
= Vcb
->NtfsInfo
.SerialNumber
;
530 /* Get volume label */
531 NewDeviceObject
->Vpb
->VolumeLabelLength
= Vcb
->NtfsInfo
.VolumeLabelLength
;
532 RtlCopyMemory(NewDeviceObject
->Vpb
->VolumeLabel
,
533 Vcb
->NtfsInfo
.VolumeLabel
,
534 Vcb
->NtfsInfo
.VolumeLabelLength
);
536 FsRtlNotifyVolumeEvent(Vcb
->StreamFileObject
, FSRTL_VOLUME_MOUNT
);
538 Status
= STATUS_SUCCESS
;
541 if (!NT_SUCCESS(Status
))
544 if (Vcb
&& Vcb
->StreamFileObject
)
545 ObDereferenceObject(Vcb
->StreamFileObject
);
554 IoDeleteDevice(NewDeviceObject
);
557 DPRINT("NtfsMountVolume() done (Status: %lx)\n", Status
);
565 NtfsVerifyVolume(PDEVICE_OBJECT DeviceObject
,
568 UNREFERENCED_PARAMETER(DeviceObject
);
569 UNREFERENCED_PARAMETER(Irp
);
570 DPRINT1("NtfsVerifyVolume() called\n");
571 return STATUS_WRONG_VOLUME
;
577 GetNfsVolumeData(PDEVICE_EXTENSION DeviceExt
,
580 PIO_STACK_LOCATION Stack
;
581 PNTFS_VOLUME_DATA_BUFFER DataBuffer
;
582 PNTFS_ATTR_RECORD Attribute
;
583 FIND_ATTR_CONTXT Context
;
586 DataBuffer
= (PNTFS_VOLUME_DATA_BUFFER
)Irp
->AssociatedIrp
.SystemBuffer
;
587 Stack
= IoGetCurrentIrpStackLocation(Irp
);
589 if (Stack
->Parameters
.FileSystemControl
.OutputBufferLength
< sizeof(NTFS_VOLUME_DATA_BUFFER
) ||
590 Irp
->UserBuffer
== NULL
)
592 DPRINT1("Invalid output! %d %p\n", Stack
->Parameters
.FileSystemControl
.OutputBufferLength
, Irp
->UserBuffer
);
593 return STATUS_INVALID_PARAMETER
;
596 DataBuffer
->VolumeSerialNumber
.QuadPart
= DeviceExt
->NtfsInfo
.SerialNumber
;
597 DataBuffer
->NumberSectors
.QuadPart
= DeviceExt
->NtfsInfo
.SectorCount
;
598 DataBuffer
->TotalClusters
.QuadPart
= DeviceExt
->NtfsInfo
.ClusterCount
;
599 DataBuffer
->FreeClusters
.QuadPart
= NtfsGetFreeClusters(DeviceExt
);
600 DataBuffer
->TotalReserved
.QuadPart
= 0LL; // FIXME
601 DataBuffer
->BytesPerSector
= DeviceExt
->NtfsInfo
.BytesPerSector
;
602 DataBuffer
->BytesPerCluster
= DeviceExt
->NtfsInfo
.BytesPerCluster
;
603 DataBuffer
->BytesPerFileRecordSegment
= DeviceExt
->NtfsInfo
.BytesPerFileRecord
;
604 DataBuffer
->ClustersPerFileRecordSegment
= DeviceExt
->NtfsInfo
.BytesPerFileRecord
/ DeviceExt
->NtfsInfo
.BytesPerCluster
;
605 DataBuffer
->MftStartLcn
.QuadPart
= DeviceExt
->NtfsInfo
.MftStart
.QuadPart
;
606 DataBuffer
->Mft2StartLcn
.QuadPart
= DeviceExt
->NtfsInfo
.MftMirrStart
.QuadPart
;
607 DataBuffer
->MftZoneStart
.QuadPart
= 0; // FIXME
608 DataBuffer
->MftZoneEnd
.QuadPart
= 0; // FIXME
610 Status
= FindFirstAttribute(&Context
, DeviceExt
, DeviceExt
->MasterFileTable
, FALSE
, &Attribute
);
611 while (NT_SUCCESS(Status
))
613 if (Attribute
->Type
== AttributeData
)
615 ASSERT(Attribute
->IsNonResident
);
616 DataBuffer
->MftValidDataLength
.QuadPart
= Attribute
->NonResident
.DataSize
;
621 Status
= FindNextAttribute(&Context
, &Attribute
);
623 FindCloseAttribute(&Context
);
625 Irp
->IoStatus
.Information
= sizeof(NTFS_VOLUME_DATA_BUFFER
);
627 if (Stack
->Parameters
.FileSystemControl
.OutputBufferLength
>= sizeof(NTFS_EXTENDED_VOLUME_DATA
) + sizeof(NTFS_VOLUME_DATA_BUFFER
))
629 PNTFS_EXTENDED_VOLUME_DATA ExtendedData
= (PNTFS_EXTENDED_VOLUME_DATA
)((ULONG_PTR
)Irp
->UserBuffer
+ sizeof(NTFS_VOLUME_DATA_BUFFER
));
631 ExtendedData
->ByteCount
= sizeof(NTFS_EXTENDED_VOLUME_DATA
);
632 ExtendedData
->MajorVersion
= DeviceExt
->NtfsInfo
.MajorVersion
;
633 ExtendedData
->MinorVersion
= DeviceExt
->NtfsInfo
.MinorVersion
;
634 Irp
->IoStatus
.Information
+= sizeof(NTFS_EXTENDED_VOLUME_DATA
);
637 return STATUS_SUCCESS
;
643 GetNtfsFileRecord(PDEVICE_EXTENSION DeviceExt
,
647 PIO_STACK_LOCATION Stack
;
648 PNTFS_FILE_RECORD_INPUT_BUFFER InputBuffer
;
649 PFILE_RECORD_HEADER FileRecord
;
650 PNTFS_FILE_RECORD_OUTPUT_BUFFER OutputBuffer
;
653 Stack
= IoGetCurrentIrpStackLocation(Irp
);
655 if (Stack
->Parameters
.FileSystemControl
.InputBufferLength
< sizeof(NTFS_FILE_RECORD_INPUT_BUFFER
) ||
656 Irp
->AssociatedIrp
.SystemBuffer
== NULL
)
658 DPRINT1("Invalid input! %d %p\n", Stack
->Parameters
.FileSystemControl
.InputBufferLength
, Irp
->AssociatedIrp
.SystemBuffer
);
659 return STATUS_INVALID_PARAMETER
;
662 if (Stack
->Parameters
.FileSystemControl
.OutputBufferLength
< (FIELD_OFFSET(NTFS_FILE_RECORD_OUTPUT_BUFFER
, FileRecordBuffer
) + DeviceExt
->NtfsInfo
.BytesPerFileRecord
) ||
663 Irp
->AssociatedIrp
.SystemBuffer
== NULL
)
665 DPRINT1("Invalid output! %d %p\n", Stack
->Parameters
.FileSystemControl
.OutputBufferLength
, Irp
->AssociatedIrp
.SystemBuffer
);
666 return STATUS_BUFFER_TOO_SMALL
;
669 FileRecord
= ExAllocatePoolWithTag(NonPagedPool
,
670 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
672 if (FileRecord
== NULL
)
674 return STATUS_INSUFFICIENT_RESOURCES
;
677 InputBuffer
= (PNTFS_FILE_RECORD_INPUT_BUFFER
)Irp
->AssociatedIrp
.SystemBuffer
;
679 MFTRecord
= InputBuffer
->FileReferenceNumber
.QuadPart
;
680 DPRINT1("Requesting: %I64x\n", MFTRecord
);
684 Status
= ReadFileRecord(DeviceExt
, MFTRecord
, FileRecord
);
685 if (NT_SUCCESS(Status
))
687 if (FileRecord
->Flags
& FRH_IN_USE
)
696 DPRINT1("Returning: %I64x\n", MFTRecord
);
697 OutputBuffer
= (PNTFS_FILE_RECORD_OUTPUT_BUFFER
)Irp
->AssociatedIrp
.SystemBuffer
;
698 OutputBuffer
->FileReferenceNumber
.QuadPart
= MFTRecord
;
699 OutputBuffer
->FileRecordLength
= DeviceExt
->NtfsInfo
.BytesPerFileRecord
;
700 RtlCopyMemory(OutputBuffer
->FileRecordBuffer
, FileRecord
, DeviceExt
->NtfsInfo
.BytesPerFileRecord
);
702 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
704 Irp
->IoStatus
.Information
= FIELD_OFFSET(NTFS_FILE_RECORD_OUTPUT_BUFFER
, FileRecordBuffer
) + DeviceExt
->NtfsInfo
.BytesPerFileRecord
;
706 return STATUS_SUCCESS
;
712 GetVolumeBitmap(PDEVICE_EXTENSION DeviceExt
,
715 NTSTATUS Status
= STATUS_SUCCESS
;
716 PIO_STACK_LOCATION Stack
;
717 PVOLUME_BITMAP_BUFFER BitmapBuffer
;
718 LONGLONG StartingLcn
;
719 PFILE_RECORD_HEADER BitmapRecord
;
720 PNTFS_ATTR_CONTEXT DataContext
;
721 ULONGLONG TotalClusters
;
723 BOOLEAN Overflow
= FALSE
;
725 DPRINT1("GetVolumeBitmap(%p, %p)\n", DeviceExt
, Irp
);
727 Stack
= IoGetCurrentIrpStackLocation(Irp
);
729 if (Stack
->Parameters
.FileSystemControl
.InputBufferLength
< sizeof(STARTING_LCN_INPUT_BUFFER
))
731 DPRINT1("Invalid input! %d\n", Stack
->Parameters
.FileSystemControl
.InputBufferLength
);
732 return STATUS_INVALID_PARAMETER
;
735 if (Stack
->Parameters
.FileSystemControl
.OutputBufferLength
< sizeof(VOLUME_BITMAP_BUFFER
))
737 DPRINT1("Invalid output! %d\n", Stack
->Parameters
.FileSystemControl
.OutputBufferLength
);
738 return STATUS_BUFFER_TOO_SMALL
;
741 BitmapBuffer
= NtfsGetUserBuffer(Irp
, FALSE
);
742 if (Irp
->RequestorMode
== UserMode
)
746 ProbeForRead(Stack
->Parameters
.FileSystemControl
.Type3InputBuffer
,
747 Stack
->Parameters
.FileSystemControl
.InputBufferLength
,
749 ProbeForWrite(BitmapBuffer
, Stack
->Parameters
.FileSystemControl
.OutputBufferLength
,
752 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
754 Status
= _SEH2_GetExceptionCode();
760 if (Stack
->Parameters
.FileSystemControl
.Type3InputBuffer
== NULL
||
761 BitmapBuffer
== NULL
)
763 Status
= STATUS_INVALID_PARAMETER
;
767 if (!NT_SUCCESS(Status
))
769 DPRINT1("Invalid buffer! %p %p\n", Stack
->Parameters
.FileSystemControl
.Type3InputBuffer
, BitmapBuffer
);
773 StartingLcn
= ((PSTARTING_LCN_INPUT_BUFFER
)Stack
->Parameters
.FileSystemControl
.Type3InputBuffer
)->StartingLcn
.QuadPart
;
774 if (StartingLcn
> DeviceExt
->NtfsInfo
.ClusterCount
)
776 DPRINT1("Requested bitmap start beyond partition end: %I64x %I64x\n", DeviceExt
->NtfsInfo
.ClusterCount
, StartingLcn
);
777 return STATUS_INVALID_PARAMETER
;
780 /* Round down to a multiple of 8 */
781 StartingLcn
= StartingLcn
& ~7;
782 TotalClusters
= DeviceExt
->NtfsInfo
.ClusterCount
- StartingLcn
;
783 ToCopy
= TotalClusters
/ 8;
784 if ((ToCopy
+ FIELD_OFFSET(VOLUME_BITMAP_BUFFER
, Buffer
)) > Stack
->Parameters
.FileSystemControl
.OutputBufferLength
)
786 DPRINT1("Buffer too small: %x, needed: %x\n", Stack
->Parameters
.FileSystemControl
.OutputBufferLength
, (ToCopy
+ FIELD_OFFSET(VOLUME_BITMAP_BUFFER
, Buffer
)));
788 ToCopy
= Stack
->Parameters
.FileSystemControl
.OutputBufferLength
- FIELD_OFFSET(VOLUME_BITMAP_BUFFER
, Buffer
);
791 BitmapRecord
= ExAllocatePoolWithTag(NonPagedPool
,
792 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
794 if (BitmapRecord
== NULL
)
796 return STATUS_INSUFFICIENT_RESOURCES
;
799 Status
= ReadFileRecord(DeviceExt
, NTFS_FILE_BITMAP
, BitmapRecord
);
800 if (!NT_SUCCESS(Status
))
802 DPRINT1("Failed reading volume bitmap: %lx\n", Status
);
803 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
807 Status
= FindAttribute(DeviceExt
, BitmapRecord
, AttributeData
, L
"", 0, &DataContext
);
808 if (!NT_SUCCESS(Status
))
810 DPRINT1("Failed find $DATA for bitmap: %lx\n", Status
);
811 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
815 BitmapBuffer
->StartingLcn
.QuadPart
= StartingLcn
;
816 BitmapBuffer
->BitmapSize
.QuadPart
= ToCopy
* 8;
818 Irp
->IoStatus
.Information
= FIELD_OFFSET(VOLUME_BITMAP_BUFFER
, Buffer
);
821 Irp
->IoStatus
.Information
+= ReadAttribute(DeviceExt
, DataContext
, StartingLcn
/ 8, (PCHAR
)BitmapBuffer
->Buffer
, ToCopy
);
822 Status
= (Overflow
? STATUS_BUFFER_OVERFLOW
: STATUS_SUCCESS
);
824 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
826 Status
= _SEH2_GetExceptionCode();
829 ReleaseAttributeContext(DataContext
);
830 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
838 LockOrUnlockVolume(PDEVICE_EXTENSION DeviceExt
,
842 PFILE_OBJECT FileObject
;
844 PIO_STACK_LOCATION Stack
;
846 DPRINT("LockOrUnlockVolume(%p, %p, %d)\n", DeviceExt
, Irp
, Lock
);
848 Stack
= IoGetCurrentIrpStackLocation(Irp
);
849 FileObject
= Stack
->FileObject
;
850 Fcb
= FileObject
->FsContext
;
852 /* Only allow locking with the volume open */
853 if (!(Fcb
->Flags
& FCB_IS_VOLUME
))
855 return STATUS_ACCESS_DENIED
;
858 /* Bail out if it's already in the demanded state */
859 if (((DeviceExt
->Flags
& VCB_VOLUME_LOCKED
) && Lock
) ||
860 (!(DeviceExt
->Flags
& VCB_VOLUME_LOCKED
) && !Lock
))
862 return STATUS_ACCESS_DENIED
;
865 /* Deny locking if we're not alone */
866 if (Lock
&& DeviceExt
->OpenHandleCount
!= 1)
868 return STATUS_ACCESS_DENIED
;
871 /* Finally, proceed */
874 DeviceExt
->Flags
|= VCB_VOLUME_LOCKED
;
878 DeviceExt
->Flags
&= ~VCB_VOLUME_LOCKED
;
881 return STATUS_SUCCESS
;
887 NtfsUserFsRequest(PDEVICE_OBJECT DeviceObject
,
891 PIO_STACK_LOCATION Stack
;
892 PDEVICE_EXTENSION DeviceExt
;
894 DPRINT1("NtfsUserFsRequest(%p, %p)\n", DeviceObject
, Irp
);
896 Stack
= IoGetCurrentIrpStackLocation(Irp
);
897 DeviceExt
= DeviceObject
->DeviceExtension
;
898 switch (Stack
->Parameters
.FileSystemControl
.FsControlCode
)
900 case FSCTL_CREATE_USN_JOURNAL
:
901 case FSCTL_DELETE_USN_JOURNAL
:
902 case FSCTL_ENUM_USN_DATA
:
903 case FSCTL_EXTEND_VOLUME
:
904 //case FSCTL_GET_RETRIEVAL_POINTER_BASE:
905 case FSCTL_GET_RETRIEVAL_POINTERS
:
906 //case FSCTL_LOOKUP_STREAM_FROM_CLUSTER:
907 case FSCTL_MARK_HANDLE
:
908 case FSCTL_MOVE_FILE
:
909 case FSCTL_QUERY_USN_JOURNAL
:
910 case FSCTL_READ_FILE_USN_DATA
:
911 case FSCTL_READ_USN_JOURNAL
:
912 //case FSCTL_SHRINK_VOLUME:
913 case FSCTL_WRITE_USN_CLOSE_RECORD
:
915 DPRINT1("Unimplemented user request: %x\n", Stack
->Parameters
.FileSystemControl
.FsControlCode
);
916 Status
= STATUS_NOT_IMPLEMENTED
;
919 case FSCTL_LOCK_VOLUME
:
920 Status
= LockOrUnlockVolume(DeviceExt
, Irp
, TRUE
);
923 case FSCTL_UNLOCK_VOLUME
:
924 Status
= LockOrUnlockVolume(DeviceExt
, Irp
, FALSE
);
927 case FSCTL_GET_NTFS_VOLUME_DATA
:
928 Status
= GetNfsVolumeData(DeviceExt
, Irp
);
931 case FSCTL_GET_NTFS_FILE_RECORD
:
932 Status
= GetNtfsFileRecord(DeviceExt
, Irp
);
935 case FSCTL_GET_VOLUME_BITMAP
:
936 Status
= GetVolumeBitmap(DeviceExt
, Irp
);
940 DPRINT("Invalid user request: %x\n", Stack
->Parameters
.FileSystemControl
.FsControlCode
);
941 Status
= STATUS_INVALID_DEVICE_REQUEST
;
950 NtfsFileSystemControl(PNTFS_IRP_CONTEXT IrpContext
)
954 PDEVICE_OBJECT DeviceObject
;
956 DPRINT1("NtfsFileSystemControl() called\n");
958 DeviceObject
= IrpContext
->DeviceObject
;
959 Irp
= IrpContext
->Irp
;
960 Irp
->IoStatus
.Information
= 0;
962 switch (IrpContext
->MinorFunction
)
964 case IRP_MN_KERNEL_CALL
:
965 DPRINT1("NTFS: IRP_MN_USER_FS_REQUEST\n");
966 Status
= STATUS_INVALID_DEVICE_REQUEST
;
969 case IRP_MN_USER_FS_REQUEST
:
970 Status
= NtfsUserFsRequest(DeviceObject
, Irp
);
973 case IRP_MN_MOUNT_VOLUME
:
974 DPRINT("NTFS: IRP_MN_MOUNT_VOLUME\n");
975 Status
= NtfsMountVolume(DeviceObject
, Irp
);
978 case IRP_MN_VERIFY_VOLUME
:
979 DPRINT1("NTFS: IRP_MN_VERIFY_VOLUME\n");
980 Status
= NtfsVerifyVolume(DeviceObject
, Irp
);
984 DPRINT1("NTFS FSC: MinorFunction %d\n", IrpContext
->MinorFunction
);
985 Status
= STATUS_INVALID_DEVICE_REQUEST
;