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
[0] += Buffer
[i
+3];
67 Serial
.Part
[1] += Buffer
[i
+2];
68 Serial
.Part
[2] += Buffer
[i
+1];
69 Serial
.Part
[3] += Buffer
[i
+0];
71 CdInfo
->SerialNumber
= Serial
.Value
;
73 /* Extract the volume label */
75 pw
= CdInfo
->VolumeLabel
;
76 for (i
= 0; i
< MAXIMUM_VOLUME_LABEL_LENGTH
&& *pc
!= ' '; i
++)
81 CdInfo
->VolumeLabelLength
= i
* sizeof(WCHAR
);
83 CdInfo
->VolumeSpaceSize
= Pvd
->VolumeSpaceSizeL
;
84 CdInfo
->RootStart
= Pvd
->RootDirRecord
.ExtentLocationL
;
85 CdInfo
->RootSize
= Pvd
->RootDirRecord
.DataLengthL
;
87 DPRINT("VolumeSerial: %08lx\n", CdInfo
->SerialNumber
);
88 DPRINT("VolumeLabel: '%S'\n", CdInfo
->VolumeLabel
);
89 DPRINT("VolumeLabelLength: %lu\n", CdInfo
->VolumeLabelLength
);
90 DPRINT("VolumeSize: %lu\n", Pvd
->VolumeSpaceSizeL
);
91 DPRINT("RootStart: %lu\n", Pvd
->RootDirRecord
.ExtentLocationL
);
92 DPRINT("RootSize: %lu\n", Pvd
->RootDirRecord
.DataLengthL
);
93 DPRINT("PathTableSize: %lu\n", Pvd
->PathTableSizeL
);
94 DPRINT("PathTablePos: %lu\n", Pvd
->LPathTablePos
);
95 DPRINT("OptPathTablePos: %lu\n", Pvd
->LOptPathTablePos
);
98 DbgPrint("******** PVD **********\n");
99 DbgPrint("VdType: %d\n", Pvd
->VdType
);
100 DbgPrint("StandardId: '%.*s'\n", 5, Pvd
->StandardId
);
101 DbgPrint("VdVersion: %d\n", Pvd
->VdVersion
);
102 DbgPrint("SystemId: '%.*s'\n", 32, Pvd
->SystemId
);
103 DbgPrint("VolumeId: '%.*s'\n", 32, Pvd
->VolumeId
);
104 DbgPrint("VolumeSpaceSizeL: %d (%x)\n", Pvd
->VolumeSpaceSizeL
, Pvd
->VolumeSpaceSizeL
);
105 DbgPrint("VolumeSpaceSizeM: %d (%x)\n", Pvd
->VolumeSpaceSizeM
, Pvd
->VolumeSpaceSizeM
);
106 DbgPrint("VolumeSetSize: %d (%x)\n", Pvd
->VolumeSequenceNumber
, Pvd
->VolumeSequenceNumber
);
107 DbgPrint("VolumeSequenceNumber: %d (%x)\n", Pvd
->VolumeSequenceNumber
, Pvd
->VolumeSequenceNumber
);
108 DbgPrint("LogicalBlockSize: %d (%x)\n", Pvd
->LogicalBlockSize
, Pvd
->LogicalBlockSize
);
109 DbgPrint("PathTableSizeL: %d (%x)\n", Pvd
->PathTableSizeL
, Pvd
->PathTableSizeL
);
110 DbgPrint("PathTableSizeM: %d (%x)\n", Pvd
->PathTableSizeM
, Pvd
->PathTableSizeM
);
111 DbgPrint("LPathTablePos: %d (%x)\n", Pvd
->LPathTablePos
, Pvd
->LPathTablePos
);
112 DbgPrint("LOptPathTablePos: %d (%x)\n", Pvd
->LOptPathTablePos
, Pvd
->LOptPathTablePos
);
113 DbgPrint("MPathTablePos: %d (%x)\n", Pvd
->MPathTablePos
, Pvd
->MPathTablePos
);
114 DbgPrint("MOptPathTablePos: %d (%x)\n", Pvd
->MOptPathTablePos
, Pvd
->MOptPathTablePos
);
115 DbgPrint("VolumeSetIdentifier: '%.*s'\n", 128, Pvd
->VolumeSetIdentifier
);
116 DbgPrint("PublisherIdentifier: '%.*s'\n", 128, Pvd
->PublisherIdentifier
);
117 DbgPrint("******** Root *********\n");
118 DbgPrint("RecordLength: %d\n", Pvd
->RootDirRecord
.RecordLength
);
119 DbgPrint("ExtAttrRecordLength: %d\n", Pvd
->RootDirRecord
.ExtAttrRecordLength
);
120 DbgPrint("ExtentLocationL: %d\n", Pvd
->RootDirRecord
.ExtentLocationL
);
121 DbgPrint("DataLengthL: %d\n", Pvd
->RootDirRecord
.DataLengthL
);
122 DbgPrint("Year: %d\n", Pvd
->RootDirRecord
.Year
);
123 DbgPrint("Month: %d\n", Pvd
->RootDirRecord
.Month
);
124 DbgPrint("Day: %d\n", Pvd
->RootDirRecord
.Day
);
125 DbgPrint("Hour: %d\n", Pvd
->RootDirRecord
.Hour
);
126 DbgPrint("Minute: %d\n", Pvd
->RootDirRecord
.Minute
);
127 DbgPrint("Second: %d\n", Pvd
->RootDirRecord
.Second
);
128 DbgPrint("TimeZone: %d\n", Pvd
->RootDirRecord
.TimeZone
);
129 DbgPrint("FileFlags: %d\n", Pvd
->RootDirRecord
.FileFlags
);
130 DbgPrint("FileUnitSize: %d\n", Pvd
->RootDirRecord
.FileUnitSize
);
131 DbgPrint("InterleaveGapSize: %d\n", Pvd
->RootDirRecord
.InterleaveGapSize
);
132 DbgPrint("VolumeSequenceNumber: %d\n", Pvd
->RootDirRecord
.VolumeSequenceNumber
);
133 DbgPrint("FileIdLength: %d\n", Pvd
->RootDirRecord
.FileIdLength
);
134 DbgPrint("FileId: '%.*s'\n", Pvd
->RootDirRecord
.FileId
);
135 DbgPrint("***********************\n");
141 CdfsGetSVDData(PUCHAR Buffer
,
145 ULONG JolietLevel
= 0;
149 DPRINT("EscapeSequences: '%.32s'\n", Svd
->EscapeSequences
);
151 if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/@", 3) == 0)
153 DPRINT("Joliet extension found (UCS-2 Level 1)\n");
156 else if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/C", 3) == 0)
158 DPRINT("Joliet extension found (UCS-2 Level 2)\n");
161 else if (strncmp((PCHAR
)Svd
->EscapeSequences
, "%/E", 3) == 0)
163 DPRINT("Joliet extension found (UCS-2 Level 3)\n");
167 CdInfo
->JolietLevel
= JolietLevel
;
169 if (JolietLevel
!= 0)
171 CdInfo
->RootStart
= Svd
->RootDirRecord
.ExtentLocationL
;
172 CdInfo
->RootSize
= Svd
->RootDirRecord
.DataLengthL
;
174 DPRINT("RootStart: %lu\n", Svd
->RootDirRecord
.ExtentLocationL
);
175 DPRINT("RootSize: %lu\n", Svd
->RootDirRecord
.DataLengthL
);
181 CdfsGetVolumeData(PDEVICE_OBJECT DeviceObject
,
196 TRACK_DATA TrackData
;
200 DPRINT("CdfsGetVolumeData\n");
202 Buffer
= ExAllocatePool(NonPagedPool
,
205 return STATUS_INSUFFICIENT_RESOURCES
;
208 Status
= CdfsDeviceIoControl(DeviceObject
,
209 IOCTL_CDROM_GET_LAST_SESSION
,
215 if (!NT_SUCCESS(Status
))
221 DPRINT("FirstSession %d, LastSession %d, FirstTrack %d\n",
222 Toc
.FirstSession
, Toc
.LastSession
, Toc
.TrackData
.TrackNumber
);
225 for (i
= 0; i
< 4; i
++)
227 Offset
= (Offset
<< 8) + Toc
.TrackData
.Address
[i
];
229 CdInfo
->VolumeOffset
= Offset
;
231 DPRINT("Offset of first track in last session %d\n", Offset
);
233 CdInfo
->JolietLevel
= 0;
234 VdHeader
= (PVD_HEADER
)Buffer
;
237 for (Sector
= CDFS_PRIMARY_DESCRIPTOR_LOCATION
; Sector
< 100 && Buffer
[0] != 255; Sector
++)
239 /* Read the Primary Volume Descriptor (PVD) */
240 Status
= CdfsReadSectors (DeviceObject
,
245 if (!NT_SUCCESS(Status
))
251 if (Sector
== CDFS_PRIMARY_DESCRIPTOR_LOCATION
)
253 DPRINT("CD-identifier: [%.5s]\n", Buffer
+ 1);
255 if (Buffer
[0] != 1 || Buffer
[1] != 'C' || Buffer
[2] != 'D' ||
256 Buffer
[3] != '0' || Buffer
[4] != '0' || Buffer
[5] != '1')
259 return STATUS_UNRECOGNIZED_VOLUME
;
263 switch (VdHeader
->VdType
)
266 DPRINT("BootVolumeDescriptor found!\n");
270 DPRINT("PrimaryVolumeDescriptor found!\n");
271 CdfsGetPVDData(Buffer
, CdInfo
);
275 DPRINT("SupplementaryVolumeDescriptor found!\n");
276 CdfsGetSVDData(Buffer
, CdInfo
);
280 DPRINT("VolumePartitionDescriptor found!\n");
284 DPRINT("VolumeDescriptorSetTerminator found!\n");
288 DPRINT1("Unknown volume descriptor type %u found!\n", VdHeader
->VdType
);
295 return(STATUS_SUCCESS
);
300 CdfsMountVolume(PDEVICE_OBJECT DeviceObject
,
303 PDEVICE_EXTENSION DeviceExt
= NULL
;
304 PDEVICE_OBJECT NewDeviceObject
= NULL
;
305 PDEVICE_OBJECT DeviceToMount
;
306 PIO_STACK_LOCATION Stack
;
313 DPRINT("CdfsMountVolume() called\n");
315 if (DeviceObject
!= CdfsGlobalData
->DeviceObject
)
317 Status
= STATUS_INVALID_DEVICE_REQUEST
;
321 Stack
= IoGetCurrentIrpStackLocation(Irp
);
322 DeviceToMount
= Stack
->Parameters
.MountVolume
.DeviceObject
;
323 Vpb
= Stack
->Parameters
.MountVolume
.Vpb
;
325 Status
= CdfsGetVolumeData(DeviceToMount
, &CdInfo
);
326 if (!NT_SUCCESS(Status
))
331 Status
= IoCreateDevice(CdfsGlobalData
->DriverObject
,
332 sizeof(DEVICE_EXTENSION
),
334 FILE_DEVICE_CD_ROM_FILE_SYSTEM
,
335 DeviceToMount
->Characteristics
,
338 if (!NT_SUCCESS(Status
))
341 NewDeviceObject
->Flags
= NewDeviceObject
->Flags
| DO_DIRECT_IO
;
342 NewDeviceObject
->Flags
&= ~DO_VERIFY_VOLUME
;
343 DeviceExt
= (PVOID
)NewDeviceObject
->DeviceExtension
;
344 RtlZeroMemory(DeviceExt
,
345 sizeof(DEVICE_EXTENSION
));
347 Vpb
->SerialNumber
= CdInfo
.SerialNumber
;
348 Vpb
->VolumeLabelLength
= CdInfo
.VolumeLabelLength
;
349 RtlCopyMemory(Vpb
->VolumeLabel
, CdInfo
.VolumeLabel
, CdInfo
.VolumeLabelLength
* sizeof(WCHAR
));
350 RtlCopyMemory(&DeviceExt
->CdInfo
, &CdInfo
, sizeof(CDINFO
));
352 NewDeviceObject
->Vpb
= DeviceToMount
->Vpb
;
354 DeviceExt
->VolumeDevice
= NewDeviceObject
;
355 DeviceExt
->StorageDevice
= DeviceToMount
;
356 DeviceExt
->StorageDevice
->Vpb
->DeviceObject
= NewDeviceObject
;
357 DeviceExt
->StorageDevice
->Vpb
->RealDevice
= DeviceExt
->StorageDevice
;
358 DeviceExt
->StorageDevice
->Vpb
->Flags
|= VPB_MOUNTED
;
359 NewDeviceObject
->StackSize
= DeviceExt
->StorageDevice
->StackSize
+ 1;
360 NewDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
362 /* Close (and cleanup) might be called from IoCreateStreamFileObject
363 * but we use this resource from CdfsCleanup, therefore it should be
364 * initialized no later than this. */
365 ExInitializeResourceLite(&DeviceExt
->DirResource
);
367 DeviceExt
->StreamFileObject
= IoCreateStreamFileObject(NULL
,
368 DeviceExt
->StorageDevice
);
370 Fcb
= CdfsCreateFCB(NULL
);
373 Status
= STATUS_INSUFFICIENT_RESOURCES
;
377 Ccb
= ExAllocatePoolWithTag(NonPagedPool
,
382 Status
= STATUS_INSUFFICIENT_RESOURCES
;
388 DeviceExt
->StreamFileObject
->ReadAccess
= TRUE
;
389 DeviceExt
->StreamFileObject
->WriteAccess
= FALSE
;
390 DeviceExt
->StreamFileObject
->DeleteAccess
= FALSE
;
391 DeviceExt
->StreamFileObject
->FsContext
= Fcb
;
392 DeviceExt
->StreamFileObject
->FsContext2
= Ccb
;
393 DeviceExt
->StreamFileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
394 DeviceExt
->StreamFileObject
->PrivateCacheMap
= NULL
;
395 DeviceExt
->StreamFileObject
->Vpb
= DeviceExt
->Vpb
;
396 Ccb
->PtrFileObject
= DeviceExt
->StreamFileObject
;
397 Fcb
->FileObject
= DeviceExt
->StreamFileObject
;
398 Fcb
->DevExt
= (PDEVICE_EXTENSION
)DeviceExt
->StorageDevice
;
400 Fcb
->Flags
= FCB_IS_VOLUME_STREAM
;
402 Fcb
->RFCB
.FileSize
.QuadPart
= (DeviceExt
->CdInfo
.VolumeSpaceSize
+ DeviceExt
->CdInfo
.VolumeOffset
) * BLOCKSIZE
;
403 Fcb
->RFCB
.ValidDataLength
= Fcb
->RFCB
.AllocationSize
= Fcb
->RFCB
.FileSize
;
405 Fcb
->Entry
.ExtentLocationL
= 0;
406 Fcb
->Entry
.DataLengthL
= (DeviceExt
->CdInfo
.VolumeSpaceSize
+ DeviceExt
->CdInfo
.VolumeOffset
) * BLOCKSIZE
;
408 CcInitializeCacheMap(DeviceExt
->StreamFileObject
,
409 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
411 &(CdfsGlobalData
->CacheMgrCallbacks
),
414 ExInitializeResourceLite(&DeviceExt
->VcbResource
);
416 KeInitializeSpinLock(&DeviceExt
->FcbListLock
);
417 InitializeListHead(&DeviceExt
->FcbListHead
);
419 Status
= STATUS_SUCCESS
;
422 if (!NT_SUCCESS(Status
))
425 if (DeviceExt
&& DeviceExt
->StreamFileObject
)
426 ObDereferenceObject(DeviceExt
->StreamFileObject
);
432 IoDeleteDevice(NewDeviceObject
);
435 DPRINT("CdfsMountVolume() done (Status: %lx)\n", Status
);
442 CdfsVerifyVolume(PDEVICE_OBJECT DeviceObject
,
445 PDEVICE_EXTENSION DeviceExt
;
446 PDEVICE_OBJECT DeviceToVerify
;
447 PIO_STACK_LOCATION Stack
;
454 DPRINT1 ("CdfsVerifyVolume() called\n");
457 if (DeviceObject
!= CdfsGlobalData
->DeviceObject
)
459 DPRINT1("DeviceObject != CdfsGlobalData->DeviceObject\n");
460 return(STATUS_INVALID_DEVICE_REQUEST
);
464 DeviceExt
= DeviceObject
->DeviceExtension
;
466 Stack
= IoGetCurrentIrpStackLocation (Irp
);
467 DeviceToVerify
= Stack
->Parameters
.VerifyVolume
.DeviceObject
;
469 FsRtlEnterFileSystem();
470 ExAcquireResourceExclusiveLite (&DeviceExt
->VcbResource
,
473 if (!(DeviceToVerify
->Flags
& DO_VERIFY_VOLUME
))
475 DPRINT1 ("Volume has been verified!\n");
476 ExReleaseResourceLite (&DeviceExt
->VcbResource
);
477 FsRtlExitFileSystem();
478 return STATUS_SUCCESS
;
481 DPRINT1 ("Device object %p Device to verify %p\n", DeviceObject
, DeviceToVerify
);
483 Status
= CdfsGetVolumeData (DeviceToVerify
,
485 if (NT_SUCCESS(Status
) &&
486 CdInfo
.SerialNumber
== DeviceToVerify
->Vpb
->SerialNumber
&&
487 CdInfo
.VolumeLabelLength
== DeviceToVerify
->Vpb
->VolumeLabelLength
&&
488 !wcsncmp (CdInfo
.VolumeLabel
, DeviceToVerify
->Vpb
->VolumeLabel
, CdInfo
.VolumeLabelLength
))
490 DPRINT1 ("Same volume!\n");
492 /* FIXME: Flush and purge metadata */
494 Status
= STATUS_SUCCESS
;
498 DPRINT1 ("Different volume!\n");
500 /* FIXME: force volume dismount */
501 Entry
= DeviceExt
->FcbListHead
.Flink
;
502 while (Entry
!= &DeviceExt
->FcbListHead
)
504 Fcb
= (PFCB
)CONTAINING_RECORD(Entry
, FCB
, FcbListEntry
);
505 DPRINT1("OpenFile %S RefCount %ld\n", Fcb
->PathName
, Fcb
->RefCount
);
507 Entry
= Entry
->Flink
;
510 Status
= STATUS_WRONG_VOLUME
;
513 DeviceToVerify
->Flags
&= ~DO_VERIFY_VOLUME
;
515 ExReleaseResourceLite (&DeviceExt
->VcbResource
);
516 FsRtlExitFileSystem();
524 IN PDEVICE_OBJECT DeviceObject
,
527 PIO_STACK_LOCATION Stack
;
528 USHORT CompressionState
;
530 Stack
= IoGetCurrentIrpStackLocation(Irp
);
532 if (Stack
->Parameters
.DeviceIoControl
.InputBufferLength
!= sizeof(CompressionState
))
533 return STATUS_INVALID_DEVICE_REQUEST
;
535 CompressionState
= *(USHORT
*)Irp
->AssociatedIrp
.SystemBuffer
;
536 if (CompressionState
!= COMPRESSION_FORMAT_NONE
)
537 return STATUS_INVALID_PARAMETER
;
539 return STATUS_SUCCESS
;
544 CdfsFileSystemControl(PDEVICE_OBJECT DeviceObject
,
547 PIO_STACK_LOCATION Stack
;
550 DPRINT("CdfsFileSystemControl() called\n");
552 Stack
= IoGetCurrentIrpStackLocation(Irp
);
554 switch (Stack
->MinorFunction
)
556 case IRP_MN_USER_FS_REQUEST
:
557 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
559 case FSCTL_SET_COMPRESSION
:
560 DPRINT("CDFS: IRP_MN_USER_FS_REQUEST / FSCTL_SET_COMPRESSION\n");
561 Status
= CdfsSetCompression(DeviceObject
, Irp
);
565 DPRINT1("CDFS: IRP_MN_USER_FS_REQUEST / Unknown IoControlCode 0x%x\n",
566 Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
567 Status
= STATUS_INVALID_DEVICE_REQUEST
;
571 case IRP_MN_MOUNT_VOLUME
:
572 DPRINT("CDFS: IRP_MN_MOUNT_VOLUME\n");
573 Status
= CdfsMountVolume(DeviceObject
, Irp
);
576 case IRP_MN_VERIFY_VOLUME
:
577 DPRINT1("CDFS: IRP_MN_VERIFY_VOLUME\n");
578 Status
= CdfsVerifyVolume(DeviceObject
, Irp
);
582 DPRINT1("CDFS FSC: MinorFunction %d\n", Stack
->MinorFunction
);
583 Status
= STATUS_INVALID_DEVICE_REQUEST
;
587 Irp
->IoStatus
.Status
= Status
;
588 Irp
->IoStatus
.Information
= 0;
590 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);