*How can I copy one hard disk partition using a kernel mode driver?*
Hello ?.
I?m a new comer to the world of device driver development
.Here I want to develop a device driver program for copy partition of a
block device such as hard disk .I have made a driver that is doing file by
file copy ;but that is so slow; Then want to make it fast. Then I try to use
Factored(), FastIoWrite(), FastIoLock(), FastIoDeviceControl(),
FastIoAcquireFile(), FastIoReleaseFile(), FastIoAcquireForCcFlush() IO
Routines??But actually I don?t know I ?m doing the correct way .Anybody can
help me?Any sample code will be available?..
*First technique I used:-*
First time I tried by using kernel mode driver (file by file) it takes so
much of time. With the
ZwCreateFile(),ZwOpenFile(),ZwReadFile(),ZwWriteFile()…But this is too slow
process.
*Second technique I used:-*
After that I used win32 application, In that I took buffer size up to 60 mb;
when I give greater than 60 mb program shows errors. It?s sample code is
given below.
Source code (sector by sector : but not single sector by single sector )
DWORD BytesReturned;
LPVOID bMBR2 = VirtualAlloc(NULL,52428800,MEM_COMMIT,PAGE_READWRITE);
HANDLE h_MyDevice_src2,h_MyDevice_dest2;
BOOL bResult,bResult1,bResult_unlock,bResult_unlock1;
DWORD dwRetBytes1,dwRetBytes2;
//Locking both volumes
bResult=DeviceIoControl(h_MyDevice_src2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL
,&BytesReturned,NULL);
if(!bResult)
{
printf(“Lock Volume Failed %d \n”,GetLastError());
}
bResult1=DeviceIoControl(h_MyDevice_dest2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NU
LL,&BytesReturned,NULL);
if(!bResult1)
{
printf(“Lock Volume Failed %d \n”,GetLastError());
}
//Source Partition drive ,in read mode
h_MyDevice_src2 = CreateFile( _T(“\\.\H: <file:></file:>”),
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (h_MyDevice_src2==INVALID_HANDLE_VALUE)
{
printf(“\nUnable to Open the Device: Error Number is :
%d\n”,GetLastError());
}
//Destination Partition drive ,in write mode
h_MyDevice_dest2=CreateFile( L"\\.\J: <file:></file:>",
GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (h_MyDevice_dest2==INVALID_HANDLE_VALUE)
{
printf(“\nUnable to Open the Device: Error Number is :
%d\n”,GetLastError());
}
//write file set pointer
DWORD dwPtr1=SetFilePointer(h_MyDevice_src2,lDistance11,NULL,FILE_CURRENT);
if (dwPtr1 == INVALID_SET_FILE_POINTER) // Test for failure
{
DWORD dwError1 = GetLastError();
cout<<“\n Error occured “<
}
//For eg. Here I took source partition of 945 mb & chunk size 50 mb
//loop wiil execute 945mb/50mb times
for( LONGLONG i=1;i<=990904320/52428800;i+=1) //945mb/50mb
{
if(!ReadFile(h_MyDevice_src2, bMBR2,52428800, &dwRetBytes1,0))
{
printf(”\nUnable to Read the Drive with offset 57,952,452,096 Error
Number is : %d\n”,GetLastError());
}
WriteFile(h_MyDevice_dest2,bMBR2,52428800,&dwRetBytes2,0);
lDistance11+=(LONGLONG)dwRetBytes1; // offset increments for source
}
CloseHandle(h_MyDevice_src2); //close source
CloseHandle(h_MyDevice_dest2);//close destination
bResult_unlock =
DeviceIoControl(h_MyDevice_src2,FSCTL_UNLOCK_VOLUME,NULL,
NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult_unlock)
{
printf(“UnLock Volume Failed %d \n”,GetLastError());
}
bResult_unlock1 = DeviceIoControl(h_MyDevice_dest2,FSCTL_UNLOCK_VOLUME,
NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult_unlock1)
{
printf(“UnLock Volume Failed %d \n”,GetLastError());
}
Third technique I?m going to use :-
I have heard
Fast IO = A means of reading or writing a cached file without going
through the work of generating I/O request packet (IRP).**
Fast I/O is specifically designed for rapid synchronous I/O on cached
files. In fast I/O operations, data is transferred directly between user
buffers and the system cache, bypassing the file system and the storage
driver stack. (Storage drivers do not use fast I/O.)
I failed to get faster copy hard disk partition performane in first two
cases .Presently I concentrate on Fast IO routuines like
FastIoCheckIfPossible(),FastIoRead(),FastIoWrite(),FastIoLock(),IoDetachDevice()
, IoCreateDevice(),IoAttachDevice() etc?But I don?t know by using this
method can I succeed. I tried some Third party software like Acronis Disk
Director. It copies the partition very fastly.So here I wanna make it
fast…Anybody can help me?Any sample codes…
IF I use the FAST I/O routines ?.Whether it improve my copy
performance???
With
regards
Krish (xxxxx@gmail.com)