[NTFS]
[reactos.git] / reactos / drivers / filesystems / ntfs / 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
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.
18 *
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
24 * Valentin Verkhovsky
25 * Pierre Schweitzer
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include "ntfs.h"
31
32 #include <ntdddisk.h>
33
34 #define NDEBUG
35 #include <debug.h>
36
37 /* FUNCTIONS ****************************************************************/
38
39 /*
40 * FUNCTION: Tests if the device contains a filesystem that can be mounted
41 * by this fsd.
42 */
43 static
44 NTSTATUS
45 NtfsHasFileSystem(PDEVICE_OBJECT DeviceToMount)
46 {
47 PARTITION_INFORMATION PartitionInfo;
48 DISK_GEOMETRY DiskGeometry;
49 ULONG ClusterSize, Size, k;
50 PBOOT_SECTOR BootSector;
51 NTSTATUS Status;
52
53 DPRINT1("NtfsHasFileSystem() called\n");
54
55 Size = sizeof(DISK_GEOMETRY);
56 Status = NtfsDeviceIoControl(DeviceToMount,
57 IOCTL_DISK_GET_DRIVE_GEOMETRY,
58 NULL,
59 0,
60 &DiskGeometry,
61 &Size,
62 TRUE);
63 if (!NT_SUCCESS(Status))
64 {
65 DPRINT1("NtfsDeviceIoControl() failed (Status %lx)\n", Status);
66 return Status;
67 }
68
69 if (DiskGeometry.MediaType == FixedMedia)
70 {
71 /* We have found a hard disk */
72 Size = sizeof(PARTITION_INFORMATION);
73 Status = NtfsDeviceIoControl(DeviceToMount,
74 IOCTL_DISK_GET_PARTITION_INFO,
75 NULL,
76 0,
77 &PartitionInfo,
78 &Size,
79 TRUE);
80 if (!NT_SUCCESS(Status))
81 {
82 DPRINT1("NtfsDeviceIoControl() failed (Status %lx)\n", Status);
83 return Status;
84 }
85
86 if (PartitionInfo.PartitionType != PARTITION_IFS)
87 {
88 DPRINT1("Invalid partition type\n");
89 return STATUS_UNRECOGNIZED_VOLUME;
90 }
91 }
92
93 DPRINT1("BytesPerSector: %lu\n", DiskGeometry.BytesPerSector);
94 BootSector = ExAllocatePoolWithTag(NonPagedPool,
95 DiskGeometry.BytesPerSector,
96 TAG_NTFS);
97 if (BootSector == NULL)
98 {
99 return STATUS_INSUFFICIENT_RESOURCES;
100 }
101
102 Status = NtfsReadSectors(DeviceToMount,
103 0,
104 1,
105 DiskGeometry.BytesPerSector,
106 (PVOID)BootSector,
107 TRUE);
108 if (!NT_SUCCESS(Status))
109 {
110 goto ByeBye;
111 }
112
113 /*
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.
116 */
117
118 /* OEMID: this field must be NTFS */
119 if (RtlCompareMemory(BootSector->OEMID, "NTFS ", 8) != 8)
120 {
121 DPRINT1("Failed with NTFS-identifier: [%.8s]\n", BootSector->OEMID);
122 Status = STATUS_UNRECOGNIZED_VOLUME;
123 goto ByeBye;
124 }
125
126 /* Unused0: this field must be COMPLETELY null */
127 for (k = 0; k < 7; k++)
128 {
129 if (BootSector->BPB.Unused0[k] != 0)
130 {
131 DPRINT1("Failed in field Unused0: [%.7s]\n", BootSector->BPB.Unused0);
132 Status = STATUS_UNRECOGNIZED_VOLUME;
133 goto ByeBye;
134 }
135 }
136
137 /* Unused3: this field must be COMPLETELY null */
138 for (k = 0; k < 4; k++)
139 {
140 if (BootSector->BPB.Unused3[k] != 0)
141 {
142 DPRINT1("Failed in field Unused3: [%.4s]\n", BootSector->BPB.Unused3);
143 Status = STATUS_UNRECOGNIZED_VOLUME;
144 goto ByeBye;
145 }
146 }
147
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)
154 {
155 DPRINT1("Cluster size failed: %hu, %hu, %hu\n",
156 BootSector->BPB.BytesPerSector,
157 BootSector->BPB.SectorsPerCluster,
158 ClusterSize);
159 Status = STATUS_UNRECOGNIZED_VOLUME;
160 goto ByeBye;
161 }
162
163 ByeBye:
164 ExFreePool(BootSector);
165
166 return Status;
167 }
168
169
170 static
171 NTSTATUS
172 NtfsGetVolumeData(PDEVICE_OBJECT DeviceObject,
173 PDEVICE_EXTENSION DeviceExt)
174 {
175 DISK_GEOMETRY DiskGeometry;
176 PFILE_RECORD_HEADER MftRecord;
177 PFILE_RECORD_HEADER VolumeRecord;
178 PVOLINFO_ATTRIBUTE VolumeInfo;
179 PBOOT_SECTOR BootSector;
180 PATTRIBUTE Attribute;
181 ULONG Size;
182 PNTFS_INFO NtfsInfo = &DeviceExt->NtfsInfo;
183 NTSTATUS Status;
184
185 DPRINT("NtfsGetVolumeData() called\n");
186
187 Size = sizeof(DISK_GEOMETRY);
188 Status = NtfsDeviceIoControl(DeviceObject,
189 IOCTL_DISK_GET_DRIVE_GEOMETRY,
190 NULL,
191 0,
192 &DiskGeometry,
193 &Size,
194 TRUE);
195 if (!NT_SUCCESS(Status))
196 {
197 DPRINT("NtfsDeviceIoControl() failed (Status %lx)\n", Status);
198 return Status;
199 }
200
201 DPRINT("BytesPerSector: %lu\n", DiskGeometry.BytesPerSector);
202 BootSector = ExAllocatePoolWithTag(NonPagedPool,
203 DiskGeometry.BytesPerSector,
204 TAG_NTFS);
205 if (BootSector == NULL)
206 {
207 return STATUS_INSUFFICIENT_RESOURCES;
208 }
209
210 Status = NtfsReadSectors(DeviceObject,
211 0, /* Partition boot sector */
212 1,
213 DiskGeometry.BytesPerSector,
214 (PVOID)BootSector,
215 TRUE);
216 if (!NT_SUCCESS(Status))
217 {
218 ExFreePool(BootSector);
219 return Status;
220 }
221
222 /* Read data from the bootsector */
223 NtfsInfo->BytesPerSector = BootSector->BPB.BytesPerSector;
224 NtfsInfo->SectorsPerCluster = BootSector->BPB.SectorsPerCluster;
225 NtfsInfo->BytesPerCluster = BootSector->BPB.BytesPerSector * BootSector->BPB.SectorsPerCluster;
226 NtfsInfo->SectorCount = BootSector->EBPB.SectorCount;
227
228 NtfsInfo->MftStart.QuadPart = BootSector->EBPB.MftLocation;
229 NtfsInfo->MftMirrStart.QuadPart = BootSector->EBPB.MftMirrLocation;
230 NtfsInfo->SerialNumber = BootSector->EBPB.SerialNumber;
231 if (BootSector->EBPB.ClustersPerMftRecord > 0)
232 NtfsInfo->BytesPerFileRecord = BootSector->EBPB.ClustersPerMftRecord * NtfsInfo->BytesPerCluster;
233 else
234 NtfsInfo->BytesPerFileRecord = 1 << (-BootSector->EBPB.ClustersPerMftRecord);
235 if (BootSector->EBPB.ClustersPerIndexRecord > 0)
236 NtfsInfo->BytesPerIndexRecord = BootSector->EBPB.ClustersPerIndexRecord * NtfsInfo->BytesPerCluster;
237 else
238 NtfsInfo->BytesPerIndexRecord = 1 << (-BootSector->EBPB.ClustersPerIndexRecord);
239
240 DPRINT("Boot sector information:\n");
241 DPRINT(" BytesPerSector: %hu\n", BootSector->BPB.BytesPerSector);
242 DPRINT(" SectorsPerCluster: %hu\n", BootSector->BPB.SectorsPerCluster);
243 DPRINT(" SectorCount: %I64u\n", BootSector->EBPB.SectorCount);
244 DPRINT(" MftStart: %I64u\n", BootSector->EBPB.MftLocation);
245 DPRINT(" MftMirrStart: %I64u\n", BootSector->EBPB.MftMirrLocation);
246 DPRINT(" ClustersPerMftRecord: %lx\n", BootSector->EBPB.ClustersPerMftRecord);
247 DPRINT(" ClustersPerIndexRecord: %lx\n", BootSector->EBPB.ClustersPerIndexRecord);
248 DPRINT(" SerialNumber: %I64x\n", BootSector->EBPB.SerialNumber);
249
250 ExFreePool(BootSector);
251
252 MftRecord = ExAllocatePoolWithTag(NonPagedPool,
253 NtfsInfo->BytesPerFileRecord,
254 TAG_NTFS);
255 if (MftRecord == NULL)
256 {
257 return STATUS_INSUFFICIENT_RESOURCES;
258 }
259
260 Status = NtfsReadSectors(DeviceObject,
261 NtfsInfo->MftStart.u.LowPart * NtfsInfo->SectorsPerCluster,
262 NtfsInfo->BytesPerFileRecord / NtfsInfo->BytesPerSector,
263 NtfsInfo->BytesPerSector,
264 (PVOID)MftRecord,
265 TRUE);
266 if (!NT_SUCCESS(Status))
267 {
268 ExFreePool(MftRecord);
269 return Status;
270 }
271
272 VolumeRecord = ExAllocatePoolWithTag(NonPagedPool,
273 NtfsInfo->BytesPerFileRecord,
274 TAG_NTFS);
275 if (VolumeRecord == NULL)
276 {
277 ExFreePool(MftRecord);
278 return STATUS_INSUFFICIENT_RESOURCES;
279 }
280
281 /* Read Volume File (MFT index 3) */
282 DeviceExt->StorageDevice = DeviceObject;
283 Status = ReadFileRecord(DeviceExt,
284 3,
285 VolumeRecord,
286 MftRecord);
287 if (!NT_SUCCESS(Status))
288 {
289 ExFreePool(MftRecord);
290 return Status;
291 }
292
293 /* Enumerate attributes */
294 NtfsDumpFileAttributes (MftRecord);
295
296 /* Enumerate attributes */
297 NtfsDumpFileAttributes (VolumeRecord);
298
299 /* Get volume name */
300 Attribute = FindAttribute (VolumeRecord, AttributeVolumeName, NULL);
301 DPRINT("Attribute %p\n", Attribute);
302
303 if (Attribute != NULL && ((PRESIDENT_ATTRIBUTE)Attribute)->ValueLength != 0)
304 {
305 DPRINT("Data length %lu\n", AttributeDataLength (Attribute));
306 NtfsInfo->VolumeLabelLength =
307 min (((PRESIDENT_ATTRIBUTE)Attribute)->ValueLength, MAXIMUM_VOLUME_LABEL_LENGTH);
308 RtlCopyMemory(NtfsInfo->VolumeLabel,
309 (PVOID)((ULONG_PTR)Attribute + ((PRESIDENT_ATTRIBUTE)Attribute)->ValueOffset),
310 NtfsInfo->VolumeLabelLength);
311 }
312 else
313 {
314 NtfsInfo->VolumeLabelLength = 0;
315 }
316
317 /* Get volume information */
318 Attribute = FindAttribute (VolumeRecord, AttributeVolumeInformation, NULL);
319 DPRINT("Attribute %p\n", Attribute);
320
321 if (Attribute != NULL && ((PRESIDENT_ATTRIBUTE)Attribute)->ValueLength != 0)
322 {
323 DPRINT("Data length %lu\n", AttributeDataLength (Attribute));
324 VolumeInfo = (PVOID)((ULONG_PTR)Attribute + ((PRESIDENT_ATTRIBUTE)Attribute)->ValueOffset);
325
326 NtfsInfo->MajorVersion = VolumeInfo->MajorVersion;
327 NtfsInfo->MinorVersion = VolumeInfo->MinorVersion;
328 NtfsInfo->Flags = VolumeInfo->Flags;
329 }
330
331 ExFreePool(MftRecord);
332 ExFreePool(VolumeRecord);
333
334 return Status;
335 }
336
337
338 static
339 NTSTATUS
340 NtfsMountVolume(PDEVICE_OBJECT DeviceObject,
341 PIRP Irp)
342 {
343 PDEVICE_OBJECT NewDeviceObject = NULL;
344 PDEVICE_OBJECT DeviceToMount;
345 PIO_STACK_LOCATION Stack;
346 PNTFS_FCB Fcb = NULL;
347 PNTFS_CCB Ccb = NULL;
348 PNTFS_VCB Vcb = NULL;
349 NTSTATUS Status;
350
351 DPRINT1("NtfsMountVolume() called\n");
352
353 if (DeviceObject != NtfsGlobalData->DeviceObject)
354 {
355 Status = STATUS_INVALID_DEVICE_REQUEST;
356 goto ByeBye;
357 }
358
359 Stack = IoGetCurrentIrpStackLocation(Irp);
360 DeviceToMount = Stack->Parameters.MountVolume.DeviceObject;
361
362 Status = NtfsHasFileSystem(DeviceToMount);
363 if (!NT_SUCCESS(Status))
364 {
365 goto ByeBye;
366 }
367
368 Status = IoCreateDevice(NtfsGlobalData->DriverObject,
369 sizeof(DEVICE_EXTENSION),
370 NULL,
371 FILE_DEVICE_DISK_FILE_SYSTEM,
372 0,
373 FALSE,
374 &NewDeviceObject);
375 if (!NT_SUCCESS(Status))
376 goto ByeBye;
377
378 NewDeviceObject->Flags |= DO_DIRECT_IO;
379 Vcb = (PVOID)NewDeviceObject->DeviceExtension;
380 RtlZeroMemory(Vcb, sizeof(NTFS_VCB));
381
382 Vcb->Identifier.Type = NTFS_TYPE_VCB;
383 Vcb->Identifier.Size = sizeof(NTFS_TYPE_VCB);
384
385 Status = NtfsGetVolumeData(DeviceToMount,
386 Vcb);
387 if (!NT_SUCCESS(Status))
388 goto ByeBye;
389
390 NewDeviceObject->Vpb = DeviceToMount->Vpb;
391
392 Vcb->StorageDevice = DeviceToMount;
393 Vcb->StorageDevice->Vpb->DeviceObject = NewDeviceObject;
394 Vcb->StorageDevice->Vpb->RealDevice = Vcb->StorageDevice;
395 Vcb->StorageDevice->Vpb->Flags |= VPB_MOUNTED;
396 NewDeviceObject->StackSize = Vcb->StorageDevice->StackSize + 1;
397 NewDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
398
399 Vcb->StreamFileObject = IoCreateStreamFileObject(NULL,
400 Vcb->StorageDevice);
401
402 InitializeListHead(&Vcb->FcbListHead);
403
404 Fcb = NtfsCreateFCB(NULL, Vcb);
405 if (Fcb == NULL)
406 {
407 Status = STATUS_INSUFFICIENT_RESOURCES;
408 goto ByeBye;
409 }
410
411 Ccb = ExAllocatePoolWithTag(NonPagedPool,
412 sizeof(NTFS_CCB),
413 TAG_CCB);
414 if (Ccb == NULL)
415 {
416 Status = STATUS_INSUFFICIENT_RESOURCES;
417 goto ByeBye;
418 }
419
420 RtlZeroMemory(Ccb, sizeof(NTFS_CCB));
421
422 Ccb->Identifier.Type = NTFS_TYPE_CCB;
423 Ccb->Identifier.Size = sizeof(NTFS_TYPE_CCB);
424
425 Vcb->StreamFileObject->FsContext = Fcb;
426 Vcb->StreamFileObject->FsContext2 = Ccb;
427 Vcb->StreamFileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
428 Vcb->StreamFileObject->PrivateCacheMap = NULL;
429 Vcb->StreamFileObject->Vpb = Vcb->Vpb;
430 Ccb->PtrFileObject = Vcb->StreamFileObject;
431 Fcb->FileObject = Vcb->StreamFileObject;
432 Fcb->Vcb = (PDEVICE_EXTENSION)Vcb->StorageDevice;
433
434 Fcb->Flags = FCB_IS_VOLUME_STREAM;
435
436 Fcb->RFCB.FileSize.QuadPart = Vcb->NtfsInfo.SectorCount * Vcb->NtfsInfo.BytesPerSector;
437 Fcb->RFCB.ValidDataLength.QuadPart = Vcb->NtfsInfo.SectorCount * Vcb->NtfsInfo.BytesPerSector;
438 Fcb->RFCB.AllocationSize.QuadPart = Vcb->NtfsInfo.SectorCount * Vcb->NtfsInfo.BytesPerSector; /* Correct? */
439
440 // Fcb->Entry.ExtentLocationL = 0;
441 // Fcb->Entry.DataLengthL = DeviceExt->CdInfo.VolumeSpaceSize * BLOCKSIZE;
442
443 CcInitializeCacheMap(Vcb->StreamFileObject,
444 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
445 FALSE,
446 &(NtfsGlobalData->CacheMgrCallbacks),
447 Fcb);
448
449 ExInitializeResourceLite(&Vcb->DirResource);
450
451 KeInitializeSpinLock(&Vcb->FcbListLock);
452
453 /* Get serial number */
454 NewDeviceObject->Vpb->SerialNumber = Vcb->NtfsInfo.SerialNumber;
455
456 /* Get volume label */
457 NewDeviceObject->Vpb->VolumeLabelLength = Vcb->NtfsInfo.VolumeLabelLength;
458 RtlCopyMemory(NewDeviceObject->Vpb->VolumeLabel,
459 Vcb->NtfsInfo.VolumeLabel,
460 Vcb->NtfsInfo.VolumeLabelLength);
461
462 Status = STATUS_SUCCESS;
463
464 ByeBye:
465 if (!NT_SUCCESS(Status))
466 {
467 /* Cleanup */
468 if (Vcb && Vcb->StreamFileObject)
469 ObDereferenceObject(Vcb->StreamFileObject);
470
471 if (Fcb)
472 ExFreePool(Fcb);
473
474 if (Ccb)
475 ExFreePool(Ccb);
476
477 if (NewDeviceObject)
478 IoDeleteDevice(NewDeviceObject);
479 }
480
481 DPRINT("NtfsMountVolume() done (Status: %lx)\n", Status);
482
483 return Status;
484 }
485
486
487 static
488 NTSTATUS
489 NtfsVerifyVolume(PDEVICE_OBJECT DeviceObject,
490 PIRP Irp)
491 {
492 UNREFERENCED_PARAMETER(DeviceObject);
493 UNREFERENCED_PARAMETER(Irp);
494 DPRINT1("NtfsVerifyVolume() called\n");
495 return STATUS_WRONG_VOLUME;
496 }
497
498
499 NTSTATUS
500 NTAPI
501 NtfsFsdFileSystemControl(PDEVICE_OBJECT DeviceObject,
502 PIRP Irp)
503 {
504 PIO_STACK_LOCATION Stack;
505 NTSTATUS Status;
506
507 DPRINT1("NtfsFileSystemControl() called\n");
508
509 Stack = IoGetCurrentIrpStackLocation(Irp);
510
511 switch (Stack->MinorFunction)
512 {
513 case IRP_MN_KERNEL_CALL:
514 case IRP_MN_USER_FS_REQUEST:
515 DPRINT("NTFS: IRP_MN_USER_FS_REQUEST/IRP_MN_KERNEL_CALL\n");
516 Status = STATUS_INVALID_DEVICE_REQUEST;
517 break;
518
519 case IRP_MN_MOUNT_VOLUME:
520 DPRINT("NTFS: IRP_MN_MOUNT_VOLUME\n");
521 Status = NtfsMountVolume(DeviceObject, Irp);
522 break;
523
524 case IRP_MN_VERIFY_VOLUME:
525 DPRINT1("NTFS: IRP_MN_VERIFY_VOLUME\n");
526 Status = NtfsVerifyVolume(DeviceObject, Irp);
527 break;
528
529 default:
530 DPRINT("NTFS FSC: MinorFunction %d\n", Stack->MinorFunction);
531 Status = STATUS_INVALID_DEVICE_REQUEST;
532 break;
533 }
534
535 Irp->IoStatus.Status = Status;
536 Irp->IoStatus.Information = 0;
537
538 IoCompleteRequest(Irp, IO_NO_INCREMENT);
539
540 return Status;
541 }
542
543 /* EOF */