Sample Code
windows driver samples/ cdfs file system driver/ C++/ write.c/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | /*++ Copyright (c) 1989-2000 Microsoft Corporation Module Name: Write.c Abstract: This module implements the File Write routine for Write called by the Fsd/Fsp dispatch drivers. --*/ #include "CdProcs.h" // // The Bug check file id for this module // #define BugCheckFileId (CDFS_BUG_CHECK_WRITE) // // VOID // SafeZeroMemory ( // _Out_ PUCHAR At, // _In_ ULONG ByteCount // ); // // // This macro just puts a nice little try-except around RtlZeroMemory // #define SafeZeroMemory(IC,AT,BYTE_COUNT) { \ try { \ RtlZeroMemory( (AT), (BYTE_COUNT) ); \ __pragma(warning(suppress: 6320)) \ } except( EXCEPTION_EXECUTE_HANDLER ) { \ CdRaiseStatus( IC, STATUS_INVALID_USER_BUFFER ); \ } \ } #ifdef ALLOC_PRAGMA #pragma alloc_text(PAGE, CdCommonWrite) #endif
_Requires_lock_held_(_Global_critical_region_) NTSTATUS CdCommonWrite ( _Inout_ PIRP_CONTEXT IrpContext, _Inout_ PIRP Irp ) /*++ Routine Description: This is the common entry point for NtWriteFile calls. For synchronous requests, CommonWrite will complete the request in the current thread. If not synchronous the request will be passed to the Fsp if there is a need to block. Arguments: Irp - Supplies the Irp to process Return Value: NTSTATUS - The result of this operation. --*/ { NTSTATUS Status = STATUS_SUCCESS; PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp ); TYPE_OF_OPEN TypeOfOpen; PFCB Fcb; PCCB Ccb; BOOLEAN Wait; ULONG SynchronousIo; PVOID UserBuffer; LONGLONG StartingOffset; LONGLONG ByteRange; ULONG ByteCount; ULONG WriteByteCount; ULONG OriginalByteCount; BOOLEAN ReleaseFile = TRUE; CD_IO_CONTEXT LocalIoContext; PAGED_CODE(); // // If this is a zero length write then return SUCCESS immediately. // if (IrpSp->Parameters.Write.Length == 0) { CdCompleteRequest( IrpContext, Irp, STATUS_SUCCESS ); return STATUS_SUCCESS; } // // Decode the file object and verify we support write on this. It // must be a volume file. // TypeOfOpen = CdDecodeFileObject( IrpContext, IrpSp->FileObject, &Fcb, &Ccb ); // Internal lock object is acquired if return status is STATUS_PENDING _Analysis_suppress_lock_checking_(Fcb->Resource); if (TypeOfOpen != UserVolumeOpen) { CdCompleteRequest( IrpContext, Irp, STATUS_INVALID_DEVICE_REQUEST ); return STATUS_INVALID_DEVICE_REQUEST; } // // Examine our input parameters to determine if this is noncached and/or // a paging io operation. // Wait = BooleanFlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT ); SynchronousIo = FlagOn( IrpSp->FileObject->Flags, FO_SYNCHRONOUS_IO ); // // Extract the range of the Io. // StartingOffset = IrpSp->Parameters.Write.ByteOffset.QuadPart; OriginalByteCount = ByteCount = IrpSp->Parameters.Write.Length; ByteRange = StartingOffset + ByteCount; // // Acquire the file shared to perform the write. // CdAcquireFileShared( IrpContext, Fcb ); // // Use a try-finally to facilitate cleanup. // try { // // Verify the Fcb. Allow writes if this is a DASD handle that is // dismounting the volume. // if (!FlagOn( Ccb->Flags, CCB_FLAG_DISMOUNT_ON_CLOSE )) { CdVerifyFcbOperation( IrpContext, Fcb ); } if (!FlagOn( Ccb->Flags, CCB_FLAG_ALLOW_EXTENDED_DASD_IO )) { // // Complete the request if it begins beyond the end of file. // if (StartingOffset >= Fcb->FileSize.QuadPart) { try_return( Status = STATUS_END_OF_FILE ); } // // Truncate the write if it extends beyond the end of the file. // if (ByteRange > Fcb->FileSize.QuadPart) { ByteCount = ( ULONG ) (Fcb->FileSize.QuadPart - StartingOffset); ByteRange = Fcb->FileSize.QuadPart; } } // // If we have an unaligned transfer then post this request if // we can't wait. Unaligned means that the starting offset // is not on a sector boundary or the write is not integral // sectors. // WriteByteCount = BlockAlign( Fcb->Vcb, ByteCount ); if (SectorOffset( StartingOffset ) || SectorOffset( WriteByteCount ) || (WriteByteCount > OriginalByteCount)) { if (!Wait) { CdRaiseStatus( IrpContext, STATUS_CANT_WAIT ); } // // Make sure we don't overwrite the buffer. // WriteByteCount = ByteCount; } // // Initialize the IoContext for the write. // If there is a context pointer, we need to make sure it was // allocated and not a stale stack pointer. // if (IrpContext->IoContext == NULL || !FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO )) { // // If we can wait, use the context on the stack. Otherwise // we need to allocate one. // if (Wait) { IrpContext->IoContext = &LocalIoContext; ClearFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO ); } else { IrpContext->IoContext = CdAllocateIoContext(); SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO ); } } RtlZeroMemory( IrpContext->IoContext, sizeof ( CD_IO_CONTEXT ) ); // // Store whether we allocated this context structure in the structure // itself. // IrpContext->IoContext->AllocatedContext = BooleanFlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_ALLOC_IO ); if (Wait) { KeInitializeEvent( &IrpContext->IoContext->SyncEvent, NotificationEvent, FALSE ); } else { IrpContext->IoContext->ResourceThreadId = ExGetCurrentResourceThread(); IrpContext->IoContext->Resource = Fcb->Resource; IrpContext->IoContext->RequestedByteCount = ByteCount; } Irp->IoStatus.Information = WriteByteCount; // // Set the FO_MODIFIED flag here to trigger a verify when this // handle is closed. Note that we can err on the conservative // side with no problem, i.e. if we accidently do an extra // verify there is no problem. // SetFlag( IrpSp->FileObject->Flags, FO_FILE_MODIFIED ); // // Dasd access is always non-cached. Call the Dasd write routine to // perform the actual write. // Status = CdVolumeDasdWrite( IrpContext, Fcb, StartingOffset, WriteByteCount ); // // Don't complete this request now if STATUS_PENDING was returned. // if (Status == STATUS_PENDING) { Irp = NULL; ReleaseFile = FALSE; // // Test is we should zero part of the buffer or update the // synchronous file position. // } else { // // Convert any unknown error code to IO_ERROR. // if (!NT_SUCCESS( Status )) { // // Set the information field to zero. // Irp->IoStatus.Information = 0; // // Raise if this is a user induced error. // if (IoIsErrorUserInduced( Status )) { CdRaiseStatus( IrpContext, Status ); } Status = FsRtlNormalizeNtstatus( Status, STATUS_UNEXPECTED_IO_ERROR ); // // Check if there is any portion of the user's buffer to zero. // } else if (WriteByteCount != ByteCount) { CdMapUserBuffer( IrpContext, &UserBuffer ); SafeZeroMemory( IrpContext, Add2Ptr( UserBuffer, ByteCount, PVOID ), WriteByteCount - ByteCount ); Irp->IoStatus.Information = ByteCount; } // // Update the file position if this is a synchronous request. // if (SynchronousIo && NT_SUCCESS( Status )) { IrpSp->FileObject->CurrentByteOffset.QuadPart = ByteRange; } } try_exit: NOTHING; } finally { // // Release the Fcb. // if (ReleaseFile) { CdReleaseFile( IrpContext, Fcb ); } } // // Post the request if we got CANT_WAIT. // if (Status == STATUS_CANT_WAIT) { Status = CdFsdPostRequest( IrpContext, Irp ); // // Otherwise complete the request. // } else { CdCompleteRequest( IrpContext, Irp, Status ); } return Status; } |
Our Services
-
What our customers say about us?
Read our customer testimonials to find out why our clients keep returning for their projects.
View Testimonials