20#define UDF_BUG_CHECK_ID UDF_FILE_WRITE
22#ifndef UDF_READ_ONLY_BUILD
71 Irp->IoStatus.Status = RC;
72 Irp->IoStatus.Information = 0;
120 SIZE_T NumberBytesWritten = 0;
163 UDFPrint((
" FSRTL_FSP_TOP_LEVEL_IRP\n"));
166 UDFPrint((
" FSRTL_CACHE_TOP_LEVEL_IRP\n"));
169 UDFPrint((
" FSRTL_MOD_WRITE_TOP_LEVEL_IRP\n"));
172 UDFPrint((
" FSRTL_FAST_IO_TOP_LEVEL_IRP\n"));
176 UDFPrint((
" NULL TOP_LEVEL_IRP\n"));
182 UDFPrint((
" RECURSIVE_IRP, TOP = %x\n", TopIrp));
190 MmPrint((
" Enter Irp, MDL=%x\n",
Irp->MdlAddress));
191 if(
Irp->MdlAddress) {
242 UDFPrint((
" Flags: %s; %s; %s; %s; Irp(W): %8.8x\n",
243 CanWait ?
"Wt" :
"nw", PagingIo ?
"Pg" :
"np",
244 NonBufferedIo ?
"NBuf" :
"buff", SynchronousIo ?
"Snc" :
"Asc",
276 if (SynchronousIo && !PagingIo &&
NT_SUCCESS(RC)) {
306 UDFPrint((
" UDF_IRP_CONTEXT_FLUSH2_REQUIRED\n"));
312#ifdef UDF_DELAYED_CLOSE
320 PtrResourceAcquired = &(
Vcb->VCBResource);
327#if defined(_MSC_VER) && !defined(__clang__)
352 &NumberBytesWritten);
364 if(
Vcb->VerifyCtx.QueuedCount ||
384 if (!NonBufferedIo) {
390 UDFPrint((
"UDFCommonWrite: Defer write\n"));
391 MmPrint((
" CcDeferWrite()\n"));
400 NonBufferedIo =
TRUE;
447#if defined(_MSC_VER) && !defined(__clang__)
465 if ( NonBufferedIo &&
467 NtReqFcb->SectionObject.DataSectionObject &&
476 PtrResourceAcquired = &(
NtReqFcb->MainResource);
483 PtrResourceAcquired2 = &(
NtReqFcb->PagingIoResource);
487 if((
ByteOffset.QuadPart + TruncatedLength) >
NtReqFcb->CommonFCBHeader.FileSize.QuadPart) {
488 NumberBytesWritten = TruncatedLength;
493 MmPrint((
" CcFlushCache()\n"));
496 if(PtrResourceAcquired2) {
498 PtrResourceAcquired2 =
NULL;
502 NumberBytesWritten = 0;
515 MmPrint((
" CcPurgeCacheSection()\n"));
517 NumberBytesWritten,
FALSE);
518 NumberBytesWritten = 0;
520 if(PtrResourceAcquired) {
522 PtrResourceAcquired =
NULL;
535 if(!IsThisADeferredWrite) {
542 PtrResourceAcquired2 = &(
NtReqFcb->PagingIoResource);
553 PtrResourceAcquired2 = &(
NtReqFcb->PagingIoResource);
562 PtrResourceAcquired = &(
NtReqFcb->MainResource);
590 RecursiveWriteThrough =
TRUE;
618 OldVDL =
NtReqFcb->CommonFCBHeader.ValidDataLength.QuadPart;
622 !RecursiveWriteThrough &&
623 !IsThisADeferredWrite) {
627 ExtendFS = (
ByteOffset.QuadPart + TruncatedLength >
NtReqFcb->CommonFCBHeader.FileSize.QuadPart);
629 if( WriteToEOF || ZeroBlock || ExtendFS) {
636 if (PtrResourceAcquired2) {
638 PtrResourceAcquired2 =
NULL;
640 if (PtrResourceAcquired) {
642 PtrResourceAcquired =
NULL;
655 PtrResourceAcquired = &(
NtReqFcb->MainResource);
660 AdPrint((
" Try to acquire PagingIoRes\n"));
662 PtrResourceAcquired2 = &(
NtReqFcb->PagingIoResource);
664 AdPrint((
" PagingIoRes Ok, Resizing...\n"));
670 if(PtrResourceAcquired2) {
672 PtrResourceAcquired2 =
NULL;
680 if(!
Vcb->LowFreeSpace) {
688 UDFPrint((
"UDFCommonWrite: Set size %x (alloc size %x)\n",
ByteOffset.LowPart + TruncatedLength,
NtReqFcb->CommonFCBHeader.AllocationSize.LowPart));
691 MmPrint((
" CcSetFileSizes()\n"));
702 ThPrint((
" UDFZeroDataEx(1)\n"));
705 NtReqFcb->CommonFCBHeader.FileSize.QuadPart - OldVDL,
708 ZeroBlockDone =
TRUE;
712 if (PtrResourceAcquired2) {
714 PtrResourceAcquired2 =
NULL;
731#ifdef UDF_DISABLE_SYSTEM_CACHE_MANAGER
732 NonBufferedIo =
TRUE;
734 if(
Fcb &&
Fcb->FileInfo &&
Fcb->FileInfo->Dloc) {
735 AdPrint((
"UDFCommonWrite: DataLoc %x, Mapping %x\n",
Fcb->FileInfo->Dloc->DataLoc,
Fcb->FileInfo->Dloc->DataLoc.Mapping));
739 if (!NonBufferedIo) {
746 UDFPrint((
"UDFCommonWrite: Init system cache\n"));
747 MmPrint((
" CcInitializeCacheMap()\n"));
752 MmPrint((
" CcSetReadAheadGranularity()\n"));
757 if(ZeroBlock && !ZeroBlockDone) {
758 ThPrint((
" UDFZeroDataEx(2)\n"));
761 ByteOffset.QuadPart + TruncatedLength - OldVDL,
773 MmPrint((
" CcPrepareMdlWrite()\n"));
778 NumberBytesWritten = 0;
784 if(
NtReqFcb->SectionObject.DataSectionObject &&
785 TruncatedLength >= 0x10000 &&
792 MmPrint((
" CcFlushCache() 16Mb\n"));
810 PerfPrint((
"UDFCommonWrite: CcCopyWrite %x bytes at %x\n", TruncatedLength,
ByteOffset.LowPart));
822 NumberBytesWritten = TruncatedLength;
828 MmPrint((
" Write NonBufferedIo\n"));
833 if (!RecursiveWriteThrough &&
834 !IsThisADeferredWrite &&
844 if(OldVDL < (
ByteOffset.QuadPart + TruncatedLength)) {
845 NtReqFcb->CommonFCBHeader.ValidDataLength.QuadPart =
ByteOffset.QuadPart + TruncatedLength;
850 UDFPrint((
"FSRTL_MOD_WRITE_TOP_LEVEL_IRP => CanWait\n"));
854 UDFPrint((
"FSRTL_CACHE_TOP_LEVEL_IRP => CanWait\n"));
869 MmPrint((
" !PASSIVE_LEVEL\n"));
875 UDFPrint((
"UDFCommonWrite: Cached => CanWait\n"));
881 UDFPrint((
"UDFCommonWrite: Post physical write %x bytes at %x\n", TruncatedLength,
ByteOffset.LowPart));
888 PtrResourceAcquired2 = &(
NtReqFcb->PagingIoResource);
892 PerfPrint((
"UDFCommonWrite: Physical write %x bytes at %x\n", TruncatedLength,
ByteOffset.LowPart));
905 CacheLocked, (
PCHAR)SystemBuffer, &NumberBytesWritten);
909#if defined(_MSC_VER) && !defined(__clang__)
917 WriteFileSizeToDirNdx =
TRUE;
931 if(PtrResourceAcquired2) {
934 if(PtrResourceAcquired) {
936 (PtrResourceAcquired ==
971 if (SynchronousIo && !PagingIo &&
NT_SUCCESS(RC)) {
983 if(!WriteFileSizeToDirNdx) {
991 if(!IsThisADeferredWrite &&
993 if(
NtReqFcb->CommonFCBHeader.ValidDataLength.QuadPart < (
ByteOffset.QuadPart + NumberBytesWritten)) {
995 NtReqFcb->CommonFCBHeader.ValidDataLength.QuadPart =
1010 Irp->IoStatus.Status = RC;
1011 Irp->IoStatus.Information = NumberBytesWritten;
1013 MmPrint((
" Complete Irp, MDL=%x\n",
Irp->MdlAddress));
1014 if(
Irp->MdlAddress) {
1053 UDFPrint((
"UDFDeferredWriteCallBack\n"));
1067#define USE_CcCopyWrite_TO_ZERO
1082#ifdef USE_CcCopyWrite_TO_ZERO
1090#define PURGE_BLOCK_SZ 0x10000000
1109#ifndef USE_CcCopyWrite_TO_ZERO
1111 MmPrint((
" CcFlushCache(s) Offs %I64x, Len %x\n", Offset0, Off_l));
1121 MmPrint((
" ZeroCache (CcWrite) Offs %I64x, Len %x\n",
Offset, PgLen));
1136 MmPrint((
" Can't use CcWrite to zero cache\n"));
1141 VDL =
NtReqFcb->CommonFCBHeader.ValidDataLength.QuadPart;
1146#ifndef USE_CcCopyWrite_TO_ZERO
1149 ((
ULONG)(VDL-OffsetX));
1151 MmPrint((
" CcFlushCache(e) Offs %I64x, Len %x\n", OffsetX, Off_l));
1161 MmPrint((
" ZeroCache (CcWrite - 2) Offs %I64x, Len %x\n", OffsetX, PgLen));
1172 MmPrint((
" Can't use CcWrite to zero cache (2)\n"));
1178#ifndef USE_CcCopyWrite_TO_ZERO
1184 MmPrint((
" CcPurgeCacheSection()\n"));
1216#ifndef USE_CcCopyWrite_TO_ZERO
static PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(PIRP Irp)
VOID NTAPI CcSetReadAheadGranularity(IN PFILE_OBJECT FileObject, IN ULONG Granularity)
VOID NTAPI CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN OPTIONAL PLARGE_INTEGER FileOffset, IN ULONG Length, OUT OPTIONAL PIO_STATUS_BLOCK IoStatus)
#define CcIsFileCached(FO)
_Inout_ PFILE_OBJECT _In_ TYPE_OF_OPEN PFCB _In_opt_ PCCB Ccb
VOID UDFCloseAllDelayed(IN PVCB Vcb)
#define NT_SUCCESS(StatCode)
_In_ PIO_STACK_LOCATION IrpSp
NTSTATUS UDFExceptionHandler(PtrUDFIrpContext PtrIrpContext, PIRP Irp)
BOOLEAN UDFAcquireResourceExclusiveWithCheck(IN PERESOURCE Resource)
BOOLEAN __fastcall UDFIsIrpTopLevel(PIRP Irp)
VOID UDFLogEvent(NTSTATUS UDFEventLogId, NTSTATUS RC)
NTSTATUS UDFPostRequest(IN PtrUDFIrpContext PtrIrpContext, IN PIRP Irp)
PtrUDFIrpContext UDFAllocateIrpContext(PIRP Irp, PDEVICE_OBJECT PtrTargetDeviceObject)
VOID UDFReleaseIrpContext(PtrUDFIrpContext PtrIrpContext)
ULONG UDFIsResourceAcquired(IN PERESOURCE Resource)
long UDFExceptionFilter(PtrUDFIrpContext PtrIrpContext, PEXCEPTION_POINTERS PtrExceptionPointers)
#define CollectStatistics(VCB, Field)
__inline VOID UDFNotifyFullReportChange(PVCB V, PUDF_FILE_INFO FI, ULONG E, ULONG A)
#define CollectStatisticsEx(VCB, Field, a)
#define CollectStatistics2(VCB, Field)
#define UDFReleaseResource(Resource)
#define PsGetCurrentThread()
#define UDFAcquireResourceShared(Resource, CanWait)
#define UDFAcquireResourceExclusive(Resource, CanWait)
#define KeGetCurrentIrql()
#define UDFAcquireSharedStarveExclusive(Resource, CanWait)
#define UDF_ERROR_INTERNAL_ERROR
#define FILE_WRITE_TO_END_OF_FILE
FAST_IO_POSSIBLE NTAPI UDFIsFastIoPossible(IN PtrUDFFCB Fcb)
IN PDCB IN PCCB IN VBO IN OUT PULONG OUT PDIRENT OUT PBCB OUT PVBO ByteOffset
BOOLEAN NTAPI FsRtlCheckLockForWriteAccess(IN PFILE_LOCK FileLock, IN PIRP Irp)
ULONG UDFFlushLogicalVolume(IN PtrUDFIrpContext PtrIrpContext, IN PIRP Irp, IN PVCB Vcb, IN ULONG FlushFlags)
#define FsRtlEnterFileSystem
#define FsRtlExitFileSystem
_Must_inspect_result_ _Outptr_ PVOID * SectionObject
#define FSRTL_FSP_TOP_LEVEL_IRP
#define FSRTL_CACHE_TOP_LEVEL_IRP
#define FSRTL_MAX_TOP_LEVEL_IRP_FLAG
#define FSRTL_MOD_WRITE_TOP_LEVEL_IRP
#define FSRTL_FAST_IO_TOP_LEVEL_IRP
VOID NTAPI CcSetFileSizes(IN PFILE_OBJECT FileObject, IN PCC_FILE_SIZES FileSizes)
VOID NTAPI CcInitializeCacheMap(IN PFILE_OBJECT FileObject, IN PCC_FILE_SIZES FileSizes, IN BOOLEAN PinAccess, IN PCACHE_MANAGER_CALLBACKS Callbacks, IN PVOID LazyWriteContext)
BOOLEAN NTAPI CcPurgeCacheSection(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN OPTIONAL PLARGE_INTEGER FileOffset, IN ULONG Length, IN BOOLEAN UninitializeCacheMaps)
#define EXCEPTION_EXECUTE_HANDLER
#define FILE_USE_FILE_POINTER_POSITION
_In_ ULONG _In_ ULONG Offset
_In_ ULONG _In_ ULONG _In_ ULONG Length
BOOLEAN NTAPI CcCopyWrite(IN PFILE_OBJECT FileObject, IN PLARGE_INTEGER FileOffset, IN ULONG Length, IN BOOLEAN Wait, IN PVOID Buffer)
BOOLEAN NTAPI CcCanIWrite(IN PFILE_OBJECT FileObject, IN ULONG BytesToWrite, IN BOOLEAN Wait, IN UCHAR Retrying)
VOID NTAPI CcDeferWrite(IN PFILE_OBJECT FileObject, IN PCC_POST_DEFERRED_WRITE PostRoutine, IN PVOID Context1, IN PVOID Context2, IN ULONG BytesToWrite, IN BOOLEAN Retrying)
#define IoCompleteRequest
PIRP NTAPI IoGetTopLevelIrp(VOID)
VOID NTAPI IoSetTopLevelIrp(IN PIRP Irp)
#define STATUS_FILE_LOCK_CONFLICT
OSSTATUS UDFTWrite(IN void *_Vcb, IN void *Buffer, IN SIZE_T Length, IN uint32 LBA, OUT PSIZE_T WrittenBytes, IN uint32 Flags)
#define UDFZeroDataEx(NtReqFcb, Offset, Length, CanWait, Vcb, FileObject)
NTSTATUS UDFLockCallersBuffer(PtrUDFIrpContext PtrIrpContext, PIRP Irp, BOOLEAN IsReadOperation, uint32 Length)
VOID UDFMdlComplete(PtrUDFIrpContext PtrIrpContext, PIRP Irp, PIO_STACK_LOCATION IrpSp, BOOLEAN ReadCompletion)
PVOID UDFGetCallersBuffer(PtrUDFIrpContext PtrIrpContext, PIRP Irp)
NTSTATUS UDFUnlockCallersBuffer(PtrUDFIrpContext PtrIrpContext, PIRP Irp, PVOID SystemBuffer)
#define UDFCloseAllSystemDelayedInDir(Vcb, FI)
#define _SEH2_AbnormalTermination()
#define _SEH2_EXCEPT(...)
#define _SEH2_GetExceptionInformation()
VOID UDFVVerify(IN PVCB Vcb, IN ULONG Flags)
#define UDF_IRP_CONTEXT_RES1_ACQ
#define UDF_IRP_CONTEXT_DEFERRED_WRITE
#define UDF_IRP_CONTEXT_NOT_TOP_LEVEL
#define UDF_IRP_CONTEXT_FLUSH2_REQUIRED
#define UDF_FCB_PAGE_FILE
#define UDF_IRP_CONTEXT_CAN_BLOCK
#define UDF_IRP_CONTEXT_FORCED_POST
struct _UDFContextControlBlock * PtrUDFCCB
#define UDF_IRP_CONTEXT_WRITE_THROUGH
#define UDF_NTREQ_FCB_MODIFIED
#define UDF_NODE_TYPE_VCB
#define UDF_FCB_DIRECTORY
#define UDF_IRP_CONTEXT_RES2_ACQ
struct _FCB::@731::@734 Fcb
union _IO_STACK_LOCATION::@1581 Parameters
struct _IO_STACK_LOCATION::@3980::@3985 Write
_In_ PNET_PNP_EVENT _In_ PTDI_PNP_CONTEXT Context1
_In_ PNET_PNP_EVENT _In_ PTDI_PNP_CONTEXT _In_ PTDI_PNP_CONTEXT Context2
#define UDF_VCB_FLAGS_VOLUME_MOUNTED
#define UDF_VCB_FLAGS_VOLUME_LOCKED
#define UDF_VCB_FLAGS_UNSAFE_IOCTL
#define UDF_VCB_FLAGS_VOLUME_READ_ONLY
#define UDF_VCB_FLAGS_RAW_DISK
#define UDF_VCB_FLAGS_MEDIA_READ_ONLY
#define UDF_VCB_FLAGS_SHUTDOWN
#define UDF_VCB_SKIP_EJECT_CHECK
OSSTATUS UDFWriteFile__(IN PVCB Vcb, IN PUDF_FILE_INFO FileInfo, IN int64 Offset, IN SIZE_T Length, IN BOOLEAN Direct, IN int8 *Buffer, OUT PSIZE_T WrittenBytes)
void UDFSetFileSizeInDirNdx(IN PVCB Vcb, IN PUDF_FILE_INFO FileInfo, IN int64 *ASize)
OSSTATUS UDFResizeFile__(IN PVCB Vcb, IN OUT PUDF_FILE_INFO FileInfo, IN int64 NewLength)
#define UDFIsFileCached__(Vcb, FileInfo, Offset, Length, ForWrite)
#define UDF_MAX_VERIFY_CACHE
#define UFD_VERIFY_FLAG_WAIT
#define UDFGetFileAllocationSize(Vcb, FileInfo)
#define UDF_SYS_CACHE_STOP_THR
#define STATUS_INVALID_DEVICE_REQUEST
#define STATUS_ACCESS_DENIED
#define STATUS_INVALID_PARAMETER
#define STATUS_INVALID_USER_BUFFER
#define STATUS_INSUFFICIENT_RESOURCES
#define UDF_CHECK_PAGING_IO_RESOURCE(NTReqFCB)
OSSTATUS WCacheEODirect__(IN PW_CACHE Cache, IN PVOID Context)
_In_ PDEVICE_OBJECT DeviceObject
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
NTSTATUS NTAPI UDFWrite(PDEVICE_OBJECT DeviceObject, PIRP Irp)
VOID NTAPI UDFDeferredWriteCallBack(IN PVOID Context1, IN PVOID Context2)
NTSTATUS UDFCommonWrite(PtrUDFIrpContext PtrIrpContext, PIRP Irp)
VOID UDFPurgeCacheEx_(PtrUDFNTRequiredFCB NtReqFcb, LONGLONG Offset, LONGLONG Length, BOOLEAN CanWait, PVCB Vcb, PFILE_OBJECT FileObject)
#define FILE_ACTION_MODIFIED_STREAM
#define FILE_NOTIFY_CHANGE_SIZE
#define FILE_ACTION_MODIFIED
#define FILE_NOTIFY_CHANGE_LAST_ACCESS
#define FILE_NOTIFY_CHANGE_STREAM_WRITE
#define FILE_NOTIFY_CHANGE_STREAM_SIZE
#define FO_FILE_SIZE_CHANGED
#define IO_DISK_INCREMENT
#define FO_SYNCHRONOUS_IO
#define FILE_NOTIFY_CHANGE_LAST_WRITE
#define IRP_SYNCHRONOUS_PAGING_IO