How can I copy one hard disk partition using a kernel mode driver?

*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)

FastIo routines are used internally by system routines like NtWriteFile/NtReadFile etc. It is not intended to use them directly. These system routines decide if they can use FastIo or not. E.g. file must be cached by Cc. You really shouldn’t call them directly.

Bronislav Gabrhelik

Open the disk raw, and read a large block (4MB or more) then write it. If
the partitions are on differring disks have two buffers reading one, and
writing the other in parallel.


Don Burn (MVP, Windows DDK)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

“krishna kumar” wrote in message
news:xxxxx@ntfsd…
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)

Information from ESET NOD32 Antivirus, version of virus signature
database 4102 (20090525)


The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Information from ESET NOD32 Antivirus, version of virus signature database 4102 (20090525)

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Just do it in user mode. Kernel mode does not make it go faster. I told this to you earlier this week when you emailed me privately. I guess my answer was not good enough for you.

d

Sent from my phone with no t9, all spilling mistakes are not intentional.

-----Original Message-----
From: Don Burn
Sent: Monday, May 25, 2009 1:55 PM
To: Windows File Systems Devs Interest List
Subject: Re:[ntfsd] How can I copy one hard disk partition using a kernel mode driver?

Open the disk raw, and read a large block (4MB or more) then write it. If
the partitions are on differring disks have two buffers reading one, and
writing the other in parallel.


Don Burn (MVP, Windows DDK)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

“krishna kumar” wrote in message
news:xxxxx@ntfsd…
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)

Information from ESET NOD32 Antivirus, version of virus signature
database 4102 (20090525)


The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Information from ESET NOD32 Antivirus, version of virus signature database 4102 (20090525)

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


NTFSD is sponsored by OSR

For our schedule of debugging and file system seminars
(including our new fs mini-filter seminar) visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Then Can I use the following items to copy the files…fatwslyy…???

CcFastCopyWrite(),CcFastCopyRead(),CcInitializeCacheMap()

**

**

krishna kumar wrote:

Then Can I use the following items to copy the files…fatwslyy…???

CcFastCopyWrite(),CcFastCopyRead(),CcInitializeCacheMap()

These are for manipulating the cache, not copying files. If you want to
own caching on a file, rather than trust the filesystem, you can use
these. Note that using the system cache manually will not give you any
increase in performance rather than using the system cache via the file
system.

I suggest you listen to Doron. It is unwise to send questions like this
to this list.

  • M


This posting is provided “AS IS” with no warranties, and confers no rights.

First thanks to all…

Sorry for the mistake’s form my side…

I did it in user mode…And I used the IO Overlapped methods( used FLAG_OVERLAPPED in Createfile() to open the device ) and I’m getting high speed .
For example to copy 1 GB(totally filled ) partition takes 10 seconds.So I satisfy with this speed.
I my problem I wanna copy only occupied sectors…I’m trying with this.

Mr. Vijay pothireddy suggested that use " FSCTL_GET_VOLUME_BITMAP" to get the cluster occupied details… And in my method I use sector by sector copy(not single sector by sector copy) multiples of sector.Presently I use 50 MB as Chunk/block size…
That’s why bit confused… the FSCTL volume bitmap will be useful for me…

Otherwise I have to leave this method and do copy "Files by files "method…???

Waiting for a reply

with regards
Krish…