Hi all,
when sending IRP_MJ_SET_VOLUME_INFORMATION (to change the volume label) in my file disk driver down to my mounted file disk device which was formatted with fast fat, system stops with 0x00000023. I cannot recognize what’s going wrong. Any help would gratefully appreciated. Here is my code snippet:
NTSTATUS
DiskSetVolumeLabel (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
{
PVOLUME_LABEL pvolume_label = NULL;
WCHAR filename = L"\DosDevices\XxxDisk0\";
UNICODE_STRING fileNameUnicodeString;
HANDLE ntFileHandle;
OBJECT_ATTRIBUTES objectAttributes;
IO_STATUS_BLOCK ioStatus;
PFILE_FS_LABEL_INFORMATION plabelinfo = NULL;
ULONG Length = 0L;
PFILE_OBJECT FileObject;
PDEVICE_OBJECT VolDeviceObject;
PIRP VolIrp;
KEVENT event;
PEXTENDED_IO_STACK_LOCATION StackPtr;
NTSTATUS status = STATUS_SUCCESS;
// get the volume label
pvolume_label = (PVOLUME_LABEL) Irp->AssociatedIrp.SystemBuffer;
KdPrint((“XxxDisk: DiskSetVolumeLabel: %S \n”, filename));
RtlInitUnicodeString( &fileNameUnicodeString, filename );
InitializeObjectAttributes( &objectAttributes, &fileNameUnicodeString,
OBJ_CASE_INSENSITIVE, NULL, NULL );
// open volume root dir
status = ZwCreateFile( &ntFileHandle, GENERIC_READ | GENERIC_WRITE,
&objectAttributes, &ioStatus, NULL, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
FILE_OPEN,
FILE_ATTRIBUTE_NORMAL, NULL, 0 );
if(!NT_SUCCESS( status ) ) {
// invalid handle
KdPrint((“XxxDisk: DiskSetVolumeLabel ZwCreateFile failed: %x \n”, status));
return status;
}
Length = sizeof(FILE_FS_LABEL_INFORMATION) + pvolume_label->VolumeLabelLength * sizeof(WCHAR);
plabelinfo = (PFILE_FS_LABEL_INFORMATION)ExAllocatePool(NonPagedPool, Length);
if (!plabelinfo)
{
KdPrint((“XxxDisk: DiskSetVolumeLabel ExAllocatePool failed!! \n”));
ZwClose(ntFileHandle);
return STATUS_NO_MEMORY;
}
plabelinfo->VolumeLabelLength = (ULONG)pvolume_label->VolumeLabelLength * sizeof(WCHAR); // in bytes
// copy uchar volume label string to wide char destination
swprintf(plabelinfo->VolumeLabel, L"%S", pvolume_label->VolumeLabel);
KdPrint((“XxxDisk: DiskSetVolumeLabel VolumeLabel: %S Length: %u \n”, plabelinfo->VolumeLabel, plabelinfo->VolumeLabelLength));
status = ObReferenceObjectByHandle(ntFileHandle,
FILE_WRITE_DATA,
NULL,
KernelMode,
(PVOID*)&FileObject,
NULL);
if (status != STATUS_SUCCESS)
{
ExFreePool(plabelinfo);
ZwClose(ntFileHandle);
return status;
}
VolDeviceObject = IoGetRelatedDeviceObject(FileObject);
if (VolDeviceObject == NULL)
{
ObDereferenceObject(FileObject);
ExFreePool(plabelinfo);
ZwClose(ntFileHandle);
return STATUS_UNSUCCESSFUL;
}
ZwClose(ntFileHandle);
KeInitializeEvent(&event, SynchronizationEvent, FALSE);
VolIrp = IoAllocateIrp(VolDeviceObject->StackSize,FALSE);
if (VolIrp == NULL)
{
ObDereferenceObject(FileObject);
ExFreePool(plabelinfo);
return(STATUS_INSUFFICIENT_RESOURCES);
}
VolIrp->AssociatedIrp.SystemBuffer = plabelinfo;
VolIrp->UserEvent = &event;
VolIrp->UserIosb = &ioStatus;
VolIrp->Tail.Overlay.Thread = PsGetCurrentThread();
VolIrp->Tail.Overlay.OriginalFileObject = FileObject;
VolIrp->RequestorMode = KernelMode;
StackPtr = (PEXTENDED_IO_STACK_LOCATION)IoGetNextIrpStackLocation(VolIrp);
StackPtr->MajorFunction = IRP_MJ_SET_VOLUME_INFORMATION;
StackPtr->MinorFunction = 0;
StackPtr->Flags = 0;
StackPtr->Control = 0;
StackPtr->DeviceObject = VolDeviceObject;
StackPtr->FileObject = FileObject;
StackPtr->Parameters.SetVolume.Length = Length;
StackPtr->Parameters.SetVolume.FsInformationClass = FileFsLabelInformation;
IoSetCompletionRoutine(VolIrp, SetVolumeLabelComplete, 0, TRUE, TRUE, TRUE);
(void) IoCallDriver(VolDeviceObject, VolIrp);
KeWaitForSingleObject(&event, Executive, KernelMode, TRUE, 0);
ObDereferenceObject( FileObject );
ExFreePool(plabelinfo);
// completion routine frees the IRP
return ioStatus.Status;
}