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/fs/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;
45 CdfsGetPVDData(PUCHAR Buffer
,
61 /* Calculate the volume serial number */
63 for (i
= 0; i
< 2048; i
+= 4)
65 /* DON'T optimize this to ULONG!!! (breaks overflow) */
66 Serial
.Part
[3] += Buffer
[i
+0];
67 Serial
.Part
[2] += Buffer
[i
+1];
68 Serial
.Part
[1] += Buffer
[i
+2];
69 Serial
.Part
[0] += Buffer
[i
+3];
71 CdInfo
->SerialNumber
= Serial
.Value
;
73 /* Extract the volume label */
75 pw
= CdInfo
->VolumeLabel
;
76 for (i
= 0; i
< (MAXIMUM_VOLUME_LABEL_LENGTH
/ sizeof(WCHAR
)) - 1; i
++)
82 /* Trim trailing spaces */
83 while (pw
> CdInfo
->VolumeLabel
)
85 if (*--pw
!= ' ') break;
87 /* Remove the space */
94 CdInfo
->VolumeLabelLength
= i
* sizeof(WCHAR
);
96 CdInfo
->VolumeSpaceSize
= Pvd
->VolumeSpaceSizeL
;
97 CdInfo
->RootStart
= Pvd
->RootDirRecord
.ExtentLocationL
;
98 CdInfo
->RootSize
= Pvd
->RootDirRecord
.DataLengthL
;
100 DPRINT("VolumeSerial: %08lx\n", CdInfo
->SerialNumber
);
101 DPRINT("VolumeLabel: '%S'\n", CdInfo
->VolumeLabel
);
102 DPRINT("VolumeLabelLength: %lu\n", CdInfo
->VolumeLabelLength
);
103 DPRINT("VolumeSize: %lu\n", Pvd
->VolumeSpaceSizeL
);
104 DPRINT("RootStart: %lu\n", Pvd
->RootDirRecord
.ExtentLocationL
);
105 DPRINT("RootSize: %lu\n", Pvd
->RootDirRecord
.DataLengthL
);
106 DPRINT("PathTableSize: %lu\n", Pvd
->PathTableSizeL
);
107 DPRINT("PathTablePos: %lu\n", Pvd
->LPathTablePos
);
108 DPRINT("OptPathTablePos: %lu\n", Pvd
->LOptPathTablePos
);
111 DbgPrint("******** PVD **********\n");
112 DbgPrint("VdType: %d\n", Pvd
->VdType
);
113 DbgPrint("StandardId: '%.*s'\n", 5, Pvd
->StandardId
);
114 DbgPrint("VdVersion: %d\n", Pvd
->VdVersion
);
115 DbgPrint("SystemId: '%.*s'\n", 32, Pvd
->SystemId
);
116 DbgPrint("VolumeId: '%.*s'\n", 32, Pvd
->VolumeId
);
117 DbgPrint("VolumeSpaceSizeL: %d (%x)\n", Pvd
->VolumeSpaceSizeL
, Pvd
->VolumeSpaceSizeL
);
118 DbgPrint("VolumeSpaceSizeM: %d (%x)\n", Pvd
->VolumeSpaceSizeM
, Pvd
->VolumeSpaceSizeM
);
119 DbgPrint("VolumeSetSize: %d (%x)\n", Pvd
->VolumeSequenceNumber
, Pvd
->VolumeSequenceNumber
);
120 DbgPrint("VolumeSequenceNumber: %d (%x)\n", Pvd
->VolumeSequenceNumber
, Pvd
->VolumeSequenceNumber
);
121 DbgPrint("LogicalBlockSize: %d (%x)\n", Pvd
->LogicalBlockSize
, Pvd
->LogicalBlockSize
);
122 DbgPrint("PathTableSizeL: %d (%x)\n", Pvd
->PathTableSizeL
, Pvd
->PathTableSizeL
);
123 DbgPrint("PathTableSizeM: %d (%x)\n", Pvd
->PathTableSizeM
, Pvd
->PathTableSizeM
);
124 DbgPrint("LPathTablePos: %d (%x)\n", Pvd
->LPathTablePos
, Pvd
->LPathTablePos
);
125 DbgPrint("LOptPathTablePos: %d (%x)\n", Pvd
->LOptPathTablePos
, Pvd
->LOptPathTablePos
);
126 DbgPrint("MPathTablePos: %d (%x)\n", Pvd
->MPathTablePos
, Pvd
->MPathTablePos
);
127 DbgPrint("MOptPathTablePos: %d (%x)\n", Pvd
->MOptPathTablePos
, Pvd
->MOptPathTablePos
);
128 DbgPrint("VolumeSetIdentifier: '%.*s'\n", 128, Pvd
->VolumeSetIdentifier
);
129 DbgPrint("PublisherIdentifier: '%.*s'\n", 128, Pvd
->PublisherIdentifier
);
130 DbgPrint("******** Root *********\n");
131 DbgPrint("RecordLength: %d\n", Pvd
->RootDirRecord
.RecordLength
);
132 DbgPrint("ExtAttrRecordLength: %d\n", Pvd
->RootDirRecord
.ExtAttrRecordLength
);
133 DbgPrint("ExtentLocationL: %d\n", Pvd
->RootDirRecord
.ExtentLocationL
);
134 DbgPrint("DataLengthL: %d\n", Pvd
->RootDirRecord
.DataLengthL
);
135 DbgPrint("Year: %d\n", Pvd
->RootDirRecord
.Year
);
136 DbgPrint("Month: %d\n", Pvd
->RootDirRecord
.Month
);
137 DbgPrint("Day: %d\n", Pvd
->RootDirRecord
.Day
);
138 DbgPrint("Hour: %d\n", Pvd
->RootDirRecord
.Hour
);
139 DbgPrint("Minute: %d\n", Pvd
->RootDirRecord
.Minute
);
140 DbgPrint("Second: %d\n", Pvd
->RootDirRecord
.Second
);
141 DbgPrint("TimeZone: %d\n", Pvd
->RootDirRecord
.TimeZone
);
142 DbgPrint("FileFlags: %d\n", Pvd
->RootDirRecord
.FileFlags
);
143 DbgPrint("FileUnitSize: %d\n", Pvd
->RootDirRecord
.FileUnitSize
);
144 DbgPrint("InterleaveGapSize: %d\n", Pvd
->RootDirRecord
.InterleaveGapSize
);
145 DbgPrint("VolumeSequenceNumber: %d\n", Pvd
->RootDirRecord
.VolumeSequenceNumber
);
146 DbgPrint("FileIdLength: %d\n", Pvd
->RootDirRecord
.FileIdLength
);
147 DbgPrint("FileId: '%.*s'\n", Pvd
->RootDirRecord
.FileId
);
148 DbgPrint("***********************\n");
154 CdfsGetSVDData(PUCHAR Buffer
,
158 ULONG JolietLevel
= 0;
162 DPRINT("EscapeSequences: '%.32s'\n", Svd
->EscapeSequences
);
164 if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/@", 3) == 0)
166 DPRINT("Joliet extension found (UCS-2 Level 1)\n");
169 else if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/C", 3) == 0)
171 DPRINT("Joliet extension found (UCS-2 Level 2)\n");
174 else if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/E", 3) == 0)
176 DPRINT("Joliet extension found (UCS-2 Level 3)\n");
180 CdInfo
->JolietLevel
= JolietLevel
;
182 if (JolietLevel
!= 0)
184 CdInfo
->RootStart
= Svd
->RootDirRecord
.ExtentLocationL
;
185 CdInfo
->RootSize
= Svd
->RootDirRecord
.DataLengthL
;
187 DPRINT("RootStart: %lu\n", Svd
->RootDirRecord
.ExtentLocationL
);
188 DPRINT("RootSize: %lu\n", Svd
->RootDirRecord
.DataLengthL
);
194 CdfsGetVolumeData(PDEVICE_OBJECT DeviceObject
,
206 DPRINT("CdfsGetVolumeData\n");
208 Buffer
= ExAllocatePool(NonPagedPool
,
211 return STATUS_INSUFFICIENT_RESOURCES
;
214 Status
= CdfsDeviceIoControl(DeviceObject
,
215 IOCTL_CDROM_READ_TOC
,
221 if (!NT_SUCCESS(Status
))
227 DPRINT("FirstTrack %u, LastTrack %u, TrackNumber %u\n",
228 Toc
.FirstTrack
, Toc
.LastTrack
, Toc
.TrackData
[0].TrackNumber
);
231 for (i
= 0; i
< 4; i
++)
233 Offset
= (Offset
<< 8) + Toc
.TrackData
[0].Address
[i
];
235 CdInfo
->VolumeOffset
= Offset
;
237 DPRINT("Offset of first track in last session %u\n", Offset
);
239 CdInfo
->JolietLevel
= 0;
240 VdHeader
= (PVD_HEADER
)Buffer
;
243 for (Sector
= CDFS_PRIMARY_DESCRIPTOR_LOCATION
; Sector
< 100 && Buffer
[0] != 255; Sector
++)
245 /* Read the Primary Volume Descriptor (PVD) */
246 Status
= CdfsReadSectors (DeviceObject
,
251 if (!NT_SUCCESS(Status
))
257 if (Sector
== CDFS_PRIMARY_DESCRIPTOR_LOCATION
)
259 DPRINT("CD-identifier: [%.5s]\n", Buffer
+ 1);
261 if (Buffer
[0] != 1 || Buffer
[1] != 'C' || Buffer
[2] != 'D' ||
262 Buffer
[3] != '0' || Buffer
[4] != '0' || Buffer
[5] != '1')
265 return STATUS_UNRECOGNIZED_VOLUME
;
269 switch (VdHeader
->VdType
)
272 DPRINT("BootVolumeDescriptor found!\n");
276 DPRINT("PrimaryVolumeDescriptor found!\n");
277 CdfsGetPVDData(Buffer
, CdInfo
);
281 DPRINT("SupplementaryVolumeDescriptor found!\n");
282 CdfsGetSVDData(Buffer
, CdInfo
);
286 DPRINT("VolumePartitionDescriptor found!\n");
290 DPRINT("VolumeDescriptorSetTerminator found!\n");
294 DPRINT1("Unknown volume descriptor type %u found!\n", VdHeader
->VdType
);
301 return(STATUS_SUCCESS
);
306 CdfsMountVolume(PDEVICE_OBJECT DeviceObject
,
309 PDEVICE_EXTENSION DeviceExt
= NULL
;
310 PDEVICE_OBJECT NewDeviceObject
= NULL
;
311 PDEVICE_OBJECT DeviceToMount
;
312 PIO_STACK_LOCATION Stack
;
319 DPRINT("CdfsMountVolume() called\n");
321 if (DeviceObject
!= CdfsGlobalData
->DeviceObject
)
323 Status
= STATUS_INVALID_DEVICE_REQUEST
;
327 Stack
= IoGetCurrentIrpStackLocation(Irp
);
328 DeviceToMount
= Stack
->Parameters
.MountVolume
.DeviceObject
;
329 Vpb
= Stack
->Parameters
.MountVolume
.Vpb
;
331 Status
= CdfsGetVolumeData(DeviceToMount
, &CdInfo
);
332 if (!NT_SUCCESS(Status
))
337 Status
= IoCreateDevice(CdfsGlobalData
->DriverObject
,
338 sizeof(DEVICE_EXTENSION
),
340 FILE_DEVICE_CD_ROM_FILE_SYSTEM
,
341 DeviceToMount
->Characteristics
,
344 if (!NT_SUCCESS(Status
))
347 NewDeviceObject
->Flags
= NewDeviceObject
->Flags
| DO_DIRECT_IO
;
348 NewDeviceObject
->Flags
&= ~DO_VERIFY_VOLUME
;
349 DeviceExt
= (PVOID
)NewDeviceObject
->DeviceExtension
;
350 RtlZeroMemory(DeviceExt
,
351 sizeof(DEVICE_EXTENSION
));
353 Vpb
->SerialNumber
= CdInfo
.SerialNumber
;
354 Vpb
->VolumeLabelLength
= CdInfo
.VolumeLabelLength
;
355 RtlCopyMemory(Vpb
->VolumeLabel
, CdInfo
.VolumeLabel
, CdInfo
.VolumeLabelLength
);
356 RtlCopyMemory(&DeviceExt
->CdInfo
, &CdInfo
, sizeof(CDINFO
));
358 NewDeviceObject
->Vpb
= DeviceToMount
->Vpb
;
360 DeviceExt
->VolumeDevice
= NewDeviceObject
;
361 DeviceExt
->StorageDevice
= DeviceToMount
;
362 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= NewDeviceObject
;
363 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
364 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
365 NewDeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
366 NewDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
368 /* Close (and cleanup) might be called from IoCreateStreamFileObject
369 * but we use this resource from CdfsCleanup, therefore it should be
370 * initialized no later than this. */
371 ExInitializeResourceLite(&DeviceExt
->DirResource
);
373 DeviceExt
->StreamFileObject
= IoCreateStreamFileObject(NULL
,
374 DeviceExt
->StorageDevice
);
376 Fcb
= CdfsCreateFCB(NULL
);
379 Status
= STATUS_INSUFFICIENT_RESOURCES
;
383 Ccb
= ExAllocatePoolWithTag(NonPagedPool
,
388 Status
= STATUS_INSUFFICIENT_RESOURCES
;
394 DeviceExt
->StreamFileObject
->ReadAccess
= TRUE
;
395 DeviceExt
->StreamFileObject
->WriteAccess
= FALSE
;
396 DeviceExt
->StreamFileObject
->DeleteAccess
= FALSE
;
397 DeviceExt
->StreamFileObject
->FsContext
= Fcb
;
398 DeviceExt
->StreamFileObject
->FsContext2
= Ccb
;
399 DeviceExt
->StreamFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
400 DeviceExt
->StreamFileObject
->PrivateCacheMap
= NULL
;
401 DeviceExt
->StreamFileObject
->Vpb
= DeviceExt
->Vpb
;
402 Ccb
->PtrFileObject
= DeviceExt
->StreamFileObject
;
403 Fcb
->FileObject
= DeviceExt
->StreamFileObject
;
404 Fcb
->DevExt
= (PDEVICE_EXTENSION
)DeviceExt
->StorageDevice
;
406 Fcb
->Flags
= FCB_IS_VOLUME_STREAM
;
408 Fcb
->RFCB
.FileSize
.QuadPart
= (DeviceExt
->CdInfo
.VolumeSpaceSize
+ DeviceExt
->CdInfo
.VolumeOffset
) * BLOCKSIZE
;
409 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
411 Fcb
->Entry
.ExtentLocationL
= 0;
412 Fcb
->Entry
.DataLengthL
= (DeviceExt
->CdInfo
.VolumeSpaceSize
+ DeviceExt
->CdInfo
.VolumeOffset
) * BLOCKSIZE
;
414 CcInitializeCacheMap(DeviceExt
->StreamFileObject
,
415 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
417 &(CdfsGlobalData
->CacheMgrCallbacks
),
420 ExInitializeResourceLite(&DeviceExt
->VcbResource
);
422 KeInitializeSpinLock(&DeviceExt
->FcbListLock
);
423 InitializeListHead(&DeviceExt
->FcbListHead
);
425 FsRtlNotifyInitializeSync(&DeviceExt
->NotifySync
);
426 InitializeListHead(&DeviceExt
->NotifyList
);
428 Status
= STATUS_SUCCESS
;
431 if (!NT_SUCCESS(Status
))
434 if (DeviceExt
&& DeviceExt
->StreamFileObject
)
435 ObDereferenceObject(DeviceExt
->StreamFileObject
);
439 IoDeleteDevice(NewDeviceObject
);
442 DPRINT("CdfsMountVolume() done (Status: %lx)\n", Status
);
449 CdfsVerifyVolume(PDEVICE_OBJECT DeviceObject
,
452 PDEVICE_EXTENSION DeviceExt
;
453 PDEVICE_OBJECT DeviceToVerify
;
454 PIO_STACK_LOCATION Stack
;
461 DPRINT1 ("CdfsVerifyVolume() called\n");
464 if (DeviceObject
!= CdfsGlobalData
->DeviceObject
)
466 DPRINT1("DeviceObject != CdfsGlobalData->DeviceObject\n");
467 return(STATUS_INVALID_DEVICE_REQUEST
);
471 DeviceExt
= DeviceObject
->DeviceExtension
;
473 Stack
= IoGetCurrentIrpStackLocation (Irp
);
474 DeviceToVerify
= Stack
->Parameters
.VerifyVolume
.DeviceObject
;
476 FsRtlEnterFileSystem();
477 ExAcquireResourceExclusiveLite (&DeviceExt
->VcbResource
,
480 if (!(DeviceToVerify
->Flags
& DO_VERIFY_VOLUME
))
482 DPRINT1 ("Volume has been verified!\n");
483 ExReleaseResourceLite (&DeviceExt
->VcbResource
);
484 FsRtlExitFileSystem();
485 return STATUS_SUCCESS
;
488 DPRINT1 ("Device object %p Device to verify %p\n", DeviceObject
, DeviceToVerify
);
490 Status
= CdfsGetVolumeData (DeviceToVerify
,
492 if (NT_SUCCESS(Status
) &&
493 CdInfo
.SerialNumber
== DeviceToVerify
->Vpb
->SerialNumber
&&
494 CdInfo
.VolumeLabelLength
== DeviceToVerify
->Vpb
->VolumeLabelLength
&&
495 !wcsncmp (CdInfo
.VolumeLabel
, DeviceToVerify
->Vpb
->VolumeLabel
, CdInfo
.VolumeLabelLength
))
497 DPRINT1 ("Same volume!\n");
499 /* FIXME: Flush and purge metadata */
501 Status
= STATUS_SUCCESS
;
505 DPRINT1 ("Different volume!\n");
507 /* FIXME: force volume dismount */
508 Entry
= DeviceExt
->FcbListHead
.Flink
;
509 while (Entry
!= &DeviceExt
->FcbListHead
)
511 Fcb
= (PFCB
)CONTAINING_RECORD(Entry
, FCB
, FcbListEntry
);
512 DPRINT1("OpenFile %wZ RefCount %ld\n", &Fcb
->PathName
, Fcb
->RefCount
);
514 Entry
= Entry
->Flink
;
517 Status
= STATUS_WRONG_VOLUME
;
520 DeviceToVerify
->Flags
&= ~DO_VERIFY_VOLUME
;
522 ExReleaseResourceLite (&DeviceExt
->VcbResource
);
523 FsRtlExitFileSystem();
531 IN PDEVICE_OBJECT DeviceObject
,
534 PIO_STACK_LOCATION Stack
;
535 USHORT CompressionState
;
537 UNREFERENCED_PARAMETER(DeviceObject
);
539 Stack
= IoGetCurrentIrpStackLocation(Irp
);
541 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
!= sizeof(CompressionState
))
542 return STATUS_INVALID_DEVICE_REQUEST
;
544 CompressionState
= *(USHORT
*)Irp
->AssociatedIrp
.SystemBuffer
;
545 if (CompressionState
!= COMPRESSION_FORMAT_NONE
)
546 return STATUS_INVALID_PARAMETER
;
548 return STATUS_SUCCESS
;
553 CdfsFileSystemControl(PDEVICE_OBJECT DeviceObject
,
556 PIO_STACK_LOCATION Stack
;
559 DPRINT("CdfsFileSystemControl() called\n");
561 Stack
= IoGetCurrentIrpStackLocation(Irp
);
563 switch (Stack
->MinorFunction
)
565 case IRP_MN_KERNEL_CALL
:
566 case IRP_MN_USER_FS_REQUEST
:
567 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
569 case FSCTL_SET_COMPRESSION
:
570 DPRINT("CDFS: IRP_MN_USER_FS_REQUEST / FSCTL_SET_COMPRESSION\n");
571 Status
= CdfsSetCompression(DeviceObject
, Irp
);
575 DPRINT1("CDFS: IRP_MN_USER_FS_REQUEST / Unknown IoControlCode 0x%x\n",
576 Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
577 Status
= STATUS_INVALID_DEVICE_REQUEST
;
581 case IRP_MN_MOUNT_VOLUME
:
582 DPRINT("CDFS: IRP_MN_MOUNT_VOLUME\n");
583 Status
= CdfsMountVolume(DeviceObject
, Irp
);
586 case IRP_MN_VERIFY_VOLUME
:
587 DPRINT1("CDFS: IRP_MN_VERIFY_VOLUME\n");
588 Status
= CdfsVerifyVolume(DeviceObject
, Irp
);
592 DPRINT1("CDFS FSC: MinorFunction %u\n", Stack
->MinorFunction
);
593 Status
= STATUS_INVALID_DEVICE_REQUEST
;
597 Irp
->IoStatus
.Status
= Status
;
598 Irp
->IoStatus
.Information
= 0;
600 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);