how to make a virtual disk drive read-only?

Hi. I have a driver that creates virtual disk drives in the operating system. Everything works
fine with it, until I try to make the drives read-only. I do this by replying to
IOCTL_DISK_IS_WRITABLE with STATUS_MEDIA_WRITE_PROTECTED. However, after
the read-only virtual drive is mounted in the system, the driver stiil receives IRP_MJ_WRITE.
The file system on the drive is FAT. I do not realize why the system is still trying to write
to the device, after I tell it that the device is not writable. Did anyone encounter this
behavior? Thanks.

Hi,
Check your read code. How do you read data? If you create MDLs for the
caller’s buffer( instead using Irp->MdlAddress ) , lock buffer with the
write access and this buffer is a memory mapped file, then the pages are
marked as dirty, and the Mapped Page Writer tries to write them on the disk.
This is a design feature of the Windows kernel. Use the MDL provided in the
IRP or separate buffers.

wrote in message news:xxxxx@ntdev…
> Hi. I have a driver that creates virtual disk drives in the operating
> system. Everything works
> fine with it, until I try to make the drives read-only. I do this by
> replying to
> IOCTL_DISK_IS_WRITABLE with STATUS_MEDIA_WRITE_PROTECTED. However, after
> the read-only virtual drive is mounted in the system, the driver stiil
> receives IRP_MJ_WRITE.
> The file system on the drive is FAT. I do not realize why the system is
> still trying to write
> to the device, after I tell it that the device is not writable. Did anyone
> encounter this
> behavior? Thanks.
>

Really? Locked pages will be written back before there are unlocked??

–PA

“Slava Imameyev” wrote in message news:xxxxx@ntdev…
> Hi,
> Check your read code. How do you read data? If you create MDLs for the caller’s buffer( instead using Irp->MdlAddress ) , lock
> buffer with the write access and this buffer is a memory mapped file, then the pages are marked as dirty, and the Mapped Page
> Writer tries to write them on the disk. This is a design feature of the Windows kernel. Use the MDL provided in the IRP or
> separate buffers.
>
> wrote in message news:xxxxx@ntdev…
>> Hi. I have a driver that creates virtual disk drives in the operating system. Everything works
>> fine with it, until I try to make the drives read-only. I do this by replying to
>> IOCTL_DISK_IS_WRITABLE with STATUS_MEDIA_WRITE_PROTECTED. However, after
>> the read-only virtual drive is mounted in the system, the driver stiil receives IRP_MJ_WRITE.
>> The file system on the drive is FAT. I do not realize why the system is still trying to write
>> to the device, after I tell it that the device is not writable. Did anyone encounter this
>> behavior? Thanks.
>>
>
>
>

There are two reasons to write a page to disk:

  1. to flush changes to the on-disk copy
  2. in preparation for reusing the physical page

In the first case there’s no reason a locked page should be different
than an unlocked page.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Pavel A.
Sent: Monday, July 31, 2006 5:21 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] how to make a virtual disk drive read-only?

Really? Locked pages will be written back before there are unlocked??

–PA

“Slava Imameyev” wrote in message
news:xxxxx@ntdev…
> Hi,
> Check your read code. How do you read data? If you create MDLs for the

> caller’s buffer( instead using Irp->MdlAddress ) , lock buffer with
> the write access and this buffer is a memory mapped file, then the
> pages are marked as dirty, and the Mapped Page Writer tries to write
them on the disk. This is a design feature of the Windows kernel. Use
the MDL provided in the IRP or separate buffers.
>
> wrote in message news:xxxxx@ntdev…
>> Hi. I have a driver that creates virtual disk drives in the operating

>> system. Everything works fine with it, until I try to make the drives

>> read-only. I do this by replying to IOCTL_DISK_IS_WRITABLE with
>> STATUS_MEDIA_WRITE_PROTECTED. However, after the read-only virtual
drive is mounted in the system, the driver stiil receives IRP_MJ_WRITE.
>> The file system on the drive is FAT. I do not realize why the system
>> is still trying to write to the device, after I tell it that the
>> device is not writable. Did anyone encounter this behavior? Thanks.
>>
>
>
>


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

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

When they are unlocked they are marked as dirty.

“Pavel A.” wrote in message news:xxxxx@ntdev…
> Really? Locked pages will be written back before there are unlocked??
>
> --PA
>
>
> “Slava Imameyev” wrote in message
> news:xxxxx@ntdev…
>> Hi,
>> Check your read code. How do you read data? If you create MDLs for the
>> caller’s buffer( instead using Irp->MdlAddress ) , lock buffer with the
>> write access and this buffer is a memory mapped file, then the pages are
>> marked as dirty, and the Mapped Page Writer tries to write them on the
>> disk. This is a design feature of the Windows kernel. Use the MDL
>> provided in the IRP or separate buffers.
>>
>> wrote in message news:xxxxx@ntdev…
>>> Hi. I have a driver that creates virtual disk drives in the operating
>>> system. Everything works
>>> fine with it, until I try to make the drives read-only. I do this by
>>> replying to
>>> IOCTL_DISK_IS_WRITABLE with STATUS_MEDIA_WRITE_PROTECTED. However, after
>>> the read-only virtual drive is mounted in the system, the driver stiil
>>> receives IRP_MJ_WRITE.
>>> The file system on the drive is FAT. I do not realize why the system is
>>> still trying to write
>>> to the device, after I tell it that the device is not writable. Did
>>> anyone encounter this
>>> behavior? Thanks.
>>>
>>
>>
>>
>
>
>

“Peter Wieland” wrote in message news:xxxxx@ntdev…
>There are two reasons to write a page to disk:
>
>1) to flush changes to the on-disk copy
>2) in preparation for reusing the physical page
>
>In the first case there’s no reason a locked page should be different
>than an unlocked page.
>

IMHO there is a very good reason not to flush a locked page.
It can not be reused until unlocked, and it’s owner can write to it
any time (so the sync will be wasted) or even have active DMA.

Only when the system hibernates, locked memory should be copied to disk?

Regards,
–PA

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Pavel A.
Sent: Monday, July 31, 2006 5:21 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] how to make a virtual disk drive read-only?

Really? Locked pages will be written back before there are unlocked??

–PA

“Slava Imameyev” wrote in message
news:xxxxx@ntdev…
> Hi,
> Check your read code. How do you read data? If you create MDLs for the

> caller’s buffer( instead using Irp->MdlAddress ) , lock buffer with
> the write access and this buffer is a memory mapped file, then the
> pages are marked as dirty, and the Mapped Page Writer tries to write
them on the disk. This is a design feature of the Windows kernel. Use
the MDL provided in the IRP or separate buffers.
>
> wrote in message news:xxxxx@ntdev…
>> Hi. I have a driver that creates virtual disk drives in the operating

>> system. Everything works fine with it, until I try to make the drives

>> read-only. I do this by replying to IOCTL_DISK_IS_WRITABLE with
>> STATUS_MEDIA_WRITE_PROTECTED. However, after the read-only virtual
drive is mounted in the system, the driver stiil receives IRP_MJ_WRITE.
>> The file system on the drive is FAT. I do not realize why the system
>> is still trying to write to the device, after I tell it that the
>> device is not writable. Did anyone encounter this behavior? Thanks.
>>
>
>
>


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

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

Flushing memory mapped files is often just capturing a transient state.
But if the client wants it to happen then it needs to happen, regardless
of who might change the page’s state only moments later.

Remember pages get locked for all sorts of non-driver reasons too, like
being active in a working set. You wouldn’t want to skip flushing
changes to a memory mapped file until it happened to age out of every
process’s address space since there’s no guarantee that will happen.

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Pavel A.
Sent: Tuesday, August 01, 2006 5:21 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] how to make a virtual disk drive read-only?

“Peter Wieland” wrote in message
news:xxxxx@ntdev…
>There are two reasons to write a page to disk:
>
>1) to flush changes to the on-disk copy
>2) in preparation for reusing the physical page
>
>In the first case there’s no reason a locked page should be different
>than an unlocked page.
>

IMHO there is a very good reason not to flush a locked page.
It can not be reused until unlocked, and it’s owner can write to it any
time (so the sync will be wasted) or even have active DMA.

Only when the system hibernates, locked memory should be copied to disk?

Regards,
–PA

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Pavel A.
Sent: Monday, July 31, 2006 5:21 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] how to make a virtual disk drive read-only?

Really? Locked pages will be written back before there are unlocked??

–PA

“Slava Imameyev” wrote in message
news:xxxxx@ntdev…
> Hi,
> Check your read code. How do you read data? If you create MDLs for the

> caller’s buffer( instead using Irp->MdlAddress ) , lock buffer with
> the write access and this buffer is a memory mapped file, then the
> pages are marked as dirty, and the Mapped Page Writer tries to write
them on the disk. This is a design feature of the Windows kernel. Use
the MDL provided in the IRP or separate buffers.
>
> wrote in message news:xxxxx@ntdev…
>> Hi. I have a driver that creates virtual disk drives in the operating

>> system. Everything works fine with it, until I try to make the drives

>> read-only. I do this by replying to IOCTL_DISK_IS_WRITABLE with
>> STATUS_MEDIA_WRITE_PROTECTED. However, after the read-only virtual
drive is mounted in the system, the driver stiil receives IRP_MJ_WRITE.
>> The file system on the drive is FAT. I do not realize why the system
>> is still trying to write to the device, after I tell it that the
>> device is not writable. Did anyone encounter this behavior? Thanks.
>>
>
>
>


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

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


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

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

Your suggestion was correct. I checked my read code and
I changed it to use separate buffers. After doing that, the
IRP_MJ_WRITEs have dissapeared. Thanks!