3 * Copyright (C) 2002, 2003 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 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.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: drivers/filesystems/cdfs/fsctl.c
23 * PURPOSE: CDROM (ISO 9660) filesystem driver
24 * PROGRAMMER: Art Yerkes
28 /* INCLUDES *****************************************************************/
35 /* FUNCTIONS ****************************************************************/
38 int msf_to_lba (UCHAR m
, UCHAR s
, UCHAR f
)
40 return (((m
* 60) + s
) * 75 + f
) - 150;
63 /* Calculate the volume serial number */
65 for (i
= 0; i
< 2048; i
+= 4)
67 /* DON'T optimize this to ULONG!!! (breaks overflow) */
68 Serial
.Part
[3] += Buffer
[i
+0];
69 Serial
.Part
[2] += Buffer
[i
+1];
70 Serial
.Part
[1] += Buffer
[i
+2];
71 Serial
.Part
[0] += Buffer
[i
+3];
73 CdInfo
->SerialNumber
= Serial
.Value
;
75 /* Extract the volume label */
77 pw
= CdInfo
->VolumeLabel
;
78 for (i
= 0; i
< (MAXIMUM_VOLUME_LABEL_LENGTH
/ sizeof(WCHAR
)) - 1; i
++)
84 /* Trim trailing spaces */
85 while (pw
> CdInfo
->VolumeLabel
)
87 if (*--pw
!= ' ') break;
89 /* Remove the space */
96 CdInfo
->VolumeLabelLength
= i
* sizeof(WCHAR
);
98 CdInfo
->VolumeSpaceSize
= Pvd
->VolumeSpaceSizeL
;
99 CdInfo
->RootStart
= Pvd
->RootDirRecord
.ExtentLocationL
;
100 CdInfo
->RootSize
= Pvd
->RootDirRecord
.DataLengthL
;
102 DPRINT("VolumeSerial: %08lx\n", CdInfo
->SerialNumber
);
103 DPRINT("VolumeLabel: '%S'\n", CdInfo
->VolumeLabel
);
104 DPRINT("VolumeLabelLength: %lu\n", CdInfo
->VolumeLabelLength
);
105 DPRINT("VolumeSize: %lu\n", Pvd
->VolumeSpaceSizeL
);
106 DPRINT("RootStart: %lu\n", Pvd
->RootDirRecord
.ExtentLocationL
);
107 DPRINT("RootSize: %lu\n", Pvd
->RootDirRecord
.DataLengthL
);
108 DPRINT("PathTableSize: %lu\n", Pvd
->PathTableSizeL
);
109 DPRINT("PathTablePos: %lu\n", Pvd
->LPathTablePos
);
110 DPRINT("OptPathTablePos: %lu\n", Pvd
->LOptPathTablePos
);
113 DbgPrint("******** PVD **********\n");
114 DbgPrint("VdType: %d\n", Pvd
->VdType
);
115 DbgPrint("StandardId: '%.*s'\n", 5, Pvd
->StandardId
);
116 DbgPrint("VdVersion: %d\n", Pvd
->VdVersion
);
117 DbgPrint("SystemId: '%.*s'\n", 32, Pvd
->SystemId
);
118 DbgPrint("VolumeId: '%.*s'\n", 32, Pvd
->VolumeId
);
119 DbgPrint("VolumeSpaceSizeL: %d (%x)\n", Pvd
->VolumeSpaceSizeL
, Pvd
->VolumeSpaceSizeL
);
120 DbgPrint("VolumeSpaceSizeM: %d (%x)\n", Pvd
->VolumeSpaceSizeM
, Pvd
->VolumeSpaceSizeM
);
121 DbgPrint("VolumeSetSize: %d (%x)\n", Pvd
->VolumeSequenceNumber
, Pvd
->VolumeSequenceNumber
);
122 DbgPrint("VolumeSequenceNumber: %d (%x)\n", Pvd
->VolumeSequenceNumber
, Pvd
->VolumeSequenceNumber
);
123 DbgPrint("LogicalBlockSize: %d (%x)\n", Pvd
->LogicalBlockSize
, Pvd
->LogicalBlockSize
);
124 DbgPrint("PathTableSizeL: %d (%x)\n", Pvd
->PathTableSizeL
, Pvd
->PathTableSizeL
);
125 DbgPrint("PathTableSizeM: %d (%x)\n", Pvd
->PathTableSizeM
, Pvd
->PathTableSizeM
);
126 DbgPrint("LPathTablePos: %d (%x)\n", Pvd
->LPathTablePos
, Pvd
->LPathTablePos
);
127 DbgPrint("LOptPathTablePos: %d (%x)\n", Pvd
->LOptPathTablePos
, Pvd
->LOptPathTablePos
);
128 DbgPrint("MPathTablePos: %d (%x)\n", Pvd
->MPathTablePos
, Pvd
->MPathTablePos
);
129 DbgPrint("MOptPathTablePos: %d (%x)\n", Pvd
->MOptPathTablePos
, Pvd
->MOptPathTablePos
);
130 DbgPrint("VolumeSetIdentifier: '%.*s'\n", 128, Pvd
->VolumeSetIdentifier
);
131 DbgPrint("PublisherIdentifier: '%.*s'\n", 128, Pvd
->PublisherIdentifier
);
132 DbgPrint("******** Root *********\n");
133 DbgPrint("RecordLength: %d\n", Pvd
->RootDirRecord
.RecordLength
);
134 DbgPrint("ExtAttrRecordLength: %d\n", Pvd
->RootDirRecord
.ExtAttrRecordLength
);
135 DbgPrint("ExtentLocationL: %d\n", Pvd
->RootDirRecord
.ExtentLocationL
);
136 DbgPrint("DataLengthL: %d\n", Pvd
->RootDirRecord
.DataLengthL
);
137 DbgPrint("Year: %d\n", Pvd
->RootDirRecord
.Year
);
138 DbgPrint("Month: %d\n", Pvd
->RootDirRecord
.Month
);
139 DbgPrint("Day: %d\n", Pvd
->RootDirRecord
.Day
);
140 DbgPrint("Hour: %d\n", Pvd
->RootDirRecord
.Hour
);
141 DbgPrint("Minute: %d\n", Pvd
->RootDirRecord
.Minute
);
142 DbgPrint("Second: %d\n", Pvd
->RootDirRecord
.Second
);
143 DbgPrint("TimeZone: %d\n", Pvd
->RootDirRecord
.TimeZone
);
144 DbgPrint("FileFlags: %d\n", Pvd
->RootDirRecord
.FileFlags
);
145 DbgPrint("FileUnitSize: %d\n", Pvd
->RootDirRecord
.FileUnitSize
);
146 DbgPrint("InterleaveGapSize: %d\n", Pvd
->RootDirRecord
.InterleaveGapSize
);
147 DbgPrint("VolumeSequenceNumber: %d\n", Pvd
->RootDirRecord
.VolumeSequenceNumber
);
148 DbgPrint("FileIdLength: %d\n", Pvd
->RootDirRecord
.FileIdLength
);
149 DbgPrint("FileId: '%.*s'\n", Pvd
->RootDirRecord
.FileId
);
150 DbgPrint("***********************\n");
162 ULONG JolietLevel
= 0;
166 DPRINT("EscapeSequences: '%.32s'\n", Svd
->EscapeSequences
);
168 if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/@", 3) == 0)
170 DPRINT("Joliet extension found (UCS-2 Level 1)\n");
173 else if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/C", 3) == 0)
175 DPRINT("Joliet extension found (UCS-2 Level 2)\n");
178 else if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/E", 3) == 0)
180 DPRINT("Joliet extension found (UCS-2 Level 3)\n");
184 CdInfo
->JolietLevel
= JolietLevel
;
186 if (JolietLevel
!= 0)
188 CdInfo
->RootStart
= Svd
->RootDirRecord
.ExtentLocationL
;
189 CdInfo
->RootSize
= Svd
->RootDirRecord
.DataLengthL
;
191 DPRINT("RootStart: %lu\n", Svd
->RootDirRecord
.ExtentLocationL
);
192 DPRINT("RootSize: %lu\n", Svd
->RootDirRecord
.DataLengthL
);
200 PDEVICE_OBJECT DeviceObject
,
211 DPRINT("CdfsGetVolumeData\n");
213 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, CDFS_BASIC_SECTOR
, CDFS_TAG
);
215 return STATUS_INSUFFICIENT_RESOURCES
;
218 Status
= CdfsDeviceIoControl(DeviceObject
,
219 IOCTL_CDROM_READ_TOC
,
225 if (!NT_SUCCESS(Status
))
227 ExFreePoolWithTag(Buffer
, CDFS_TAG
);
231 DPRINT("FirstTrack %u, LastTrack %u, TrackNumber %u\n",
232 Toc
.FirstTrack
, Toc
.LastTrack
, Toc
.TrackData
[0].TrackNumber
);
234 Offset
= Toc
.TrackData
[0].Address
[1] * 60 * 75;
235 Offset
+= Toc
.TrackData
[0].Address
[2] * 75;
236 Offset
+= Toc
.TrackData
[0].Address
[3];
239 /* Remove MSF numbering offset of first frame */
240 /* FIXME: should be done only for real cdroms? */
243 CdInfo
->VolumeOffset
= Offset
;
245 DPRINT("Offset of first track in last session %u\n", Offset
);
247 CdInfo
->JolietLevel
= 0;
248 VdHeader
= (PVD_HEADER
)Buffer
;
251 for (Sector
= CDFS_PRIMARY_DESCRIPTOR_LOCATION
; Sector
< 100 && Buffer
[0] != 255; Sector
++)
253 /* Read the Primary Volume Descriptor (PVD) */
254 Status
= CdfsReadSectors(DeviceObject
,
259 if (!NT_SUCCESS(Status
))
261 ExFreePoolWithTag(Buffer
, CDFS_TAG
);
265 if (Sector
== CDFS_PRIMARY_DESCRIPTOR_LOCATION
)
267 DPRINT("CD-identifier: [%.5s]\n", Buffer
+ 1);
269 if (Buffer
[0] != 1 || Buffer
[1] != 'C' || Buffer
[2] != 'D' ||
270 Buffer
[3] != '0' || Buffer
[4] != '0' || Buffer
[5] != '1')
272 ExFreePoolWithTag(Buffer
, CDFS_TAG
);
273 return STATUS_UNRECOGNIZED_VOLUME
;
277 switch (VdHeader
->VdType
)
280 DPRINT("BootVolumeDescriptor found!\n");
284 DPRINT("PrimaryVolumeDescriptor found!\n");
285 CdfsGetPVDData(Buffer
, CdInfo
);
289 DPRINT("SupplementaryVolumeDescriptor found!\n");
290 CdfsGetSVDData(Buffer
, CdInfo
);
294 DPRINT("VolumePartitionDescriptor found!\n");
298 DPRINT("VolumeDescriptorSetTerminator found!\n");
302 DPRINT1("Unknown volume descriptor type %u found!\n", VdHeader
->VdType
);
307 ExFreePoolWithTag(Buffer
, CDFS_TAG
);
309 return STATUS_SUCCESS
;
316 PDEVICE_OBJECT DeviceObject
,
319 PDEVICE_EXTENSION DeviceExt
= NULL
;
320 PDEVICE_OBJECT NewDeviceObject
= NULL
;
321 PDEVICE_OBJECT DeviceToMount
;
322 PIO_STACK_LOCATION Stack
;
329 DPRINT("CdfsMountVolume() called\n");
331 if (DeviceObject
!= CdfsGlobalData
->DeviceObject
)
333 Status
= STATUS_INVALID_DEVICE_REQUEST
;
337 Stack
= IoGetCurrentIrpStackLocation(Irp
);
338 DeviceToMount
= Stack
->Parameters
.MountVolume
.DeviceObject
;
339 Vpb
= Stack
->Parameters
.MountVolume
.Vpb
;
341 Status
= CdfsGetVolumeData(DeviceToMount
, &CdInfo
);
342 if (!NT_SUCCESS(Status
))
347 Status
= IoCreateDevice(CdfsGlobalData
->DriverObject
,
348 sizeof(DEVICE_EXTENSION
),
350 FILE_DEVICE_CD_ROM_FILE_SYSTEM
,
351 DeviceToMount
->Characteristics
,
354 if (!NT_SUCCESS(Status
))
357 NewDeviceObject
->Flags
= NewDeviceObject
->Flags
| DO_DIRECT_IO
;
358 NewDeviceObject
->Flags
&= ~DO_VERIFY_VOLUME
;
359 DeviceExt
= (PVOID
)NewDeviceObject
->DeviceExtension
;
360 RtlZeroMemory(DeviceExt
,
361 sizeof(DEVICE_EXTENSION
));
363 Vpb
->SerialNumber
= CdInfo
.SerialNumber
;
364 Vpb
->VolumeLabelLength
= CdInfo
.VolumeLabelLength
;
365 RtlCopyMemory(Vpb
->VolumeLabel
, CdInfo
.VolumeLabel
, CdInfo
.VolumeLabelLength
);
366 RtlCopyMemory(&DeviceExt
->CdInfo
, &CdInfo
, sizeof(CDINFO
));
368 NewDeviceObject
->Vpb
= DeviceToMount
->Vpb
;
370 DeviceExt
->VolumeDevice
= NewDeviceObject
;
371 DeviceExt
->StorageDevice
= DeviceToMount
;
372 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= NewDeviceObject
;
373 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
374 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
375 NewDeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
376 NewDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
378 /* Close (and cleanup) might be called from IoCreateStreamFileObject
379 * but we use this resource from CdfsCleanup, therefore it should be
380 * initialized no later than this. */
381 ExInitializeResourceLite(&DeviceExt
->DirResource
);
383 DeviceExt
->StreamFileObject
= IoCreateStreamFileObject(NULL
,
384 DeviceExt
->StorageDevice
);
386 Fcb
= CdfsCreateFCB(NULL
);
389 Status
= STATUS_INSUFFICIENT_RESOURCES
;
393 Ccb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(CCB
), CDFS_CCB_TAG
);
396 Status
= STATUS_INSUFFICIENT_RESOURCES
;
403 DeviceExt
->StreamFileObject
->ReadAccess
= TRUE
;
404 DeviceExt
->StreamFileObject
->WriteAccess
= FALSE
;
405 DeviceExt
->StreamFileObject
->DeleteAccess
= FALSE
;
406 DeviceExt
->StreamFileObject
->FsContext
= Fcb
;
407 DeviceExt
->StreamFileObject
->FsContext2
= Ccb
;
408 DeviceExt
->StreamFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
409 DeviceExt
->StreamFileObject
->PrivateCacheMap
= NULL
;
410 DeviceExt
->StreamFileObject
->Vpb
= DeviceExt
->Vpb
;
411 Ccb
->PtrFileObject
= DeviceExt
->StreamFileObject
;
412 Fcb
->FileObject
= DeviceExt
->StreamFileObject
;
413 Fcb
->DevExt
= (PDEVICE_EXTENSION
)DeviceExt
->StorageDevice
;
415 Fcb
->Flags
= FCB_IS_VOLUME_STREAM
;
417 Fcb
->RFCB
.FileSize
.QuadPart
= (DeviceExt
->CdInfo
.VolumeSpaceSize
+ DeviceExt
->CdInfo
.VolumeOffset
) * BLOCKSIZE
;
418 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
420 Fcb
->Entry
.ExtentLocationL
= 0;
421 Fcb
->Entry
.DataLengthL
= (DeviceExt
->CdInfo
.VolumeSpaceSize
+ DeviceExt
->CdInfo
.VolumeOffset
) * BLOCKSIZE
;
425 CcInitializeCacheMap(DeviceExt
->StreamFileObject
,
426 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
428 &(CdfsGlobalData
->CacheMgrCallbacks
),
431 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
433 Status
= _SEH2_GetExceptionCode();
438 ExInitializeResourceLite(&DeviceExt
->VcbResource
);
440 KeInitializeSpinLock(&DeviceExt
->FcbListLock
);
441 InitializeListHead(&DeviceExt
->FcbListHead
);
443 FsRtlNotifyInitializeSync(&DeviceExt
->NotifySync
);
444 InitializeListHead(&DeviceExt
->NotifyList
);
446 Status
= STATUS_SUCCESS
;
449 if (!NT_SUCCESS(Status
))
452 if (DeviceExt
&& DeviceExt
->StreamFileObject
)
453 ObDereferenceObject(DeviceExt
->StreamFileObject
);
455 ExFreePoolWithTag(Fcb
, CDFS_NONPAGED_FCB_TAG
);
457 IoDeleteDevice(NewDeviceObject
);
460 DPRINT("CdfsMountVolume() done (Status: %lx)\n", Status
);
469 PDEVICE_OBJECT DeviceObject
,
472 PDEVICE_EXTENSION DeviceExt
;
473 PIO_STACK_LOCATION Stack
;
480 DPRINT("CdfsVerifyVolume() called\n");
482 DeviceExt
= DeviceObject
->DeviceExtension
;
484 Stack
= IoGetCurrentIrpStackLocation (Irp
);
485 VpbToVerify
= Stack
->Parameters
.VerifyVolume
.Vpb
;
487 FsRtlEnterFileSystem();
488 ExAcquireResourceExclusiveLite(&DeviceExt
->VcbResource
,
491 if (!(VpbToVerify
->RealDevice
->Flags
& DO_VERIFY_VOLUME
))
493 DPRINT1("Volume has been verified!\n");
494 ExReleaseResourceLite (&DeviceExt
->VcbResource
);
495 FsRtlExitFileSystem();
496 return STATUS_SUCCESS
;
499 DPRINT1("Device object %p Device to verify %p\n", DeviceObject
, VpbToVerify
->RealDevice
);
501 Status
= CdfsGetVolumeData(VpbToVerify
->RealDevice
,
503 if (NT_SUCCESS(Status
) &&
504 CdInfo
.SerialNumber
== VpbToVerify
->SerialNumber
&&
505 CdInfo
.VolumeLabelLength
== VpbToVerify
->VolumeLabelLength
&&
506 !wcsncmp(CdInfo
.VolumeLabel
, VpbToVerify
->VolumeLabel
, CdInfo
.VolumeLabelLength
))
508 DPRINT1("Same volume!\n");
510 /* FIXME: Flush and purge metadata */
512 Status
= STATUS_SUCCESS
;
516 DPRINT1("Different volume!\n");
518 /* FIXME: force volume dismount */
519 Entry
= DeviceExt
->FcbListHead
.Flink
;
520 while (Entry
!= &DeviceExt
->FcbListHead
)
522 Fcb
= (PFCB
)CONTAINING_RECORD(Entry
, FCB
, FcbListEntry
);
523 DPRINT1("OpenFile %wZ RefCount %ld\n", &Fcb
->PathName
, Fcb
->RefCount
);
525 Entry
= Entry
->Flink
;
528 Status
= STATUS_WRONG_VOLUME
;
531 VpbToVerify
->RealDevice
->Flags
&= ~DO_VERIFY_VOLUME
;
533 ExReleaseResourceLite(&DeviceExt
->VcbResource
);
534 FsRtlExitFileSystem();
543 IN PDEVICE_OBJECT DeviceObject
,
546 PIO_STACK_LOCATION Stack
;
547 USHORT CompressionState
;
549 UNREFERENCED_PARAMETER(DeviceObject
);
551 Stack
= IoGetCurrentIrpStackLocation(Irp
);
553 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
!= sizeof(CompressionState
))
554 return STATUS_INVALID_DEVICE_REQUEST
;
556 CompressionState
= *(USHORT
*)Irp
->AssociatedIrp
.SystemBuffer
;
557 if (CompressionState
!= COMPRESSION_FORMAT_NONE
)
558 return STATUS_INVALID_PARAMETER
;
560 return STATUS_SUCCESS
;
566 CdfsFileSystemControl(
567 PCDFS_IRP_CONTEXT IrpContext
)
570 PDEVICE_OBJECT DeviceObject
;
571 PIO_STACK_LOCATION Stack
;
574 DPRINT("CdfsFileSystemControl() called\n");
578 DeviceObject
= IrpContext
->DeviceObject
;
579 Irp
= IrpContext
->Irp
;
580 Stack
= IrpContext
->Stack
;
582 Irp
->IoStatus
.Information
= 0;
584 switch (IrpContext
->MinorFunction
)
586 case IRP_MN_KERNEL_CALL
:
587 case IRP_MN_USER_FS_REQUEST
:
588 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
590 case FSCTL_SET_COMPRESSION
:
591 DPRINT("CDFS: IRP_MN_USER_FS_REQUEST / FSCTL_SET_COMPRESSION\n");
592 Status
= CdfsSetCompression(DeviceObject
, Irp
);
596 DPRINT1("CDFS: IRP_MN_USER_FS_REQUEST / Unknown IoControlCode 0x%x\n",
597 Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
598 Status
= STATUS_INVALID_DEVICE_REQUEST
;
602 case IRP_MN_MOUNT_VOLUME
:
603 DPRINT("CDFS: IRP_MN_MOUNT_VOLUME\n");
604 Status
= CdfsMountVolume(DeviceObject
, Irp
);
607 case IRP_MN_VERIFY_VOLUME
:
608 DPRINT1("CDFS: IRP_MN_VERIFY_VOLUME\n");
609 Status
= CdfsVerifyVolume(DeviceObject
, Irp
);
613 DPRINT1("CDFS FSC: MinorFunction %u\n", Stack
->MinorFunction
);
614 Status
= STATUS_INVALID_DEVICE_REQUEST
;