Need help with NTWriteFile

I wrote a upper filter disk driver for usb device. If I try to write a sector to the first sector after a partition, it does not change the data in the sector, although the write request is being processed. Writing to the sector before the partition is normal. What could be the reason?
swprintf(buffer, L"\Device\Harddisk%d\Partition0", storageDeviceNumber.DeviceNumber); // L"\??\PhysicalDrive%d" don’t work too
RtlInitUnicodeString(&deviceName, buffer);
InitializeObjectAttributes(&oa, &deviceName,
OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
status = ZwOpenFile(&pdHandle,
FILE_READ_ACCESS | SYNCHRONIZE |FILE_WRITE_ACCESS,
&oa, &iost,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS(status)) {
pdHandle = NULL;
DbgPrint(“UNSUCCESS!!!\n”);
return;
}
RtlZeroMemory(buf, 512);
position.QuadPart = devCont->LastByteoffset.QuadPart + 1;
status = ZwReadFile(pdHandle, NULL, NULL, NULL, &iost, buf, sizeof(buf), &position, NULL);
if (!NT_SUCCESS(status)) {
DbgPrint(“UNSUCCESS!!!\n”);
return;
}
LOG_HEADER LogHeader;
LogHeader.WritedRecords = 10;
RtlCopyBytes(&buf, (VOID*)&LogHeader, 8);
status = ZwWriteFile(pdHandle, NULL, NULL, NULL, &iost, buf, sizeof(buf), &position, NULL);
if (!NT_SUCCESS(status)) {
DbgPrint(“UNSUCCESS!!!\n”);
return;
}
RtlZeroMemory(buf, 512);
status = ZwReadFile(pdHandle, NULL, NULL, NULL, &iost, buf, sizeof(buf), &position, NULL);
if (!NT_SUCCESS(status)) {
DbgPrint(“UNSUCCESS!!!\n”);
return;
}
for (int i = 0; i < 16; i++)
{
DbgPrint(" %02x", buf[i]);
}

Alexey_Yerakhavets wrote:

I wrote a upper filter disk driver for usb device. If I try to write a sector to the first sector after a partition, it does not change the data in the sector, although the write request is being processed. Writing to the sector before the partition is normal.

Is your filter part of a different stack?  What I mean is, do you
actually have a filter for mass storage device X that is trying to send
open a handle to and send write requests to mass storage device X?  Or
is this to another mass storage device?

Why would you expect writes beyond the end of the partition to work? 
The partition knows how large it is.  If the partition is 3MB long and
you write to 4MB, why would you be surprised that it doesn’t work?

How did you write to before the start of the partition?  And how do you
know what data was there that you overwrote?  Your whole concept seems
fatally flawed.

@Tim_Roberts said:
Is your filter part of a different stack? What I mean is, do you
actually have a filter for mass storage device X that is trying to send
open a handle to and send write requests to mass storage device X? Or
is this to another mass storage device?
Yes, all operations occur with this device X.

Why would you expect writes beyond the end of the partition to work?
The partition knows how large it is. If the partition is 3MB long and
you write to 4MB, why would you be surprised that it doesn’t work?
I’m not trying to write part of the data first into the partition, and then another part after a partition. I just want to write one sector of independent data after a partition.

How did you write to before the start of the partition? And how do you
know what data was there that you overwrote? Your whole concept seems
fatally flawed.

NTSTATUS st = IoctlRead(devCont->PartitionDO, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, pDiskExtent, sizeof(*pDiskExtent));
if (!NT_SUCCESS(st)) return st;
if (!pDiskExtent->NumberOfDiskExtents) return STATUS_UNSUCCESSFUL;
devCont->DiskNumber = pDiskExtent->Extents[0].DiskNumber;
swprintf(buf, L"\??\PhysicalDrive%d", pDiskExtent->Extents[0].DiskNumber);
RtlInitUnicodeString(&PhysDrvName, buf);

InitializeObjectAttributes(&oa, &PhysDrvName,
	OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);

st = ZwOpenFile(pPhysDiskHandle,
	FILE_READ_ACCESS | SYNCHRONIZE | ForWrite ? FILE_WRITE_ACCESS : 0,
	&oa, &iost,
	FILE_SHARE_READ | FILE_SHARE_WRITE,
	FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS(st))
	*pPhysDiskHandle = NULL;

pos.QuadPart = pDiskExtent->Extents[0].StartingOffset.QuadPart - 512;
st = ZwReadFile(*pPhysDiskHandle, NULL, NULL, NULL, &iost, sector, sizeof(sector), &pos, NULL);
RtlCopyMemory(sector + 512 - TEST_SEC_SIGN_SIZE, TEST_SECTOR_SIGNATURE, TEST_SEC_SIGN_SIZE);
st = ZwWriteFile(*pPhysDiskHandle, NULL, NULL, NULL, &iost, sector, sizeof(sector), &pos, NULL);
ZwClose(pdHandle);

Question solved) I forgot to create an unpartitioned area after the first partition.

@Alexey_Yerakhavets said:
Question solved) I forgot to create an unpartitioned area after the first partition.

Hey , Alexey.
What do you mean about "create an unpartitioned area "?

write volume directly? maybe you need this: Blocking Direct Write Operations to Volumes and Disks