[NTFS]
[reactos.git] / drivers / filesystems / ntfs / rw.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002, 2014 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/rw.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Art Yerkes
24 * Pierre Schweitzer (pierre@reactos.org)
25 * Trevor Thompson
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <ntddk.h>
31 #include "ntfs.h"
32
33 #define NDEBUG
34 #include <debug.h>
35
36 /* FUNCTIONS ****************************************************************/
37
38 /*
39 * FUNCTION: Reads data from a file
40 */
41 static
42 NTSTATUS
43 NtfsReadFile(PDEVICE_EXTENSION DeviceExt,
44 PFILE_OBJECT FileObject,
45 PUCHAR Buffer,
46 ULONG Length,
47 ULONG ReadOffset,
48 ULONG IrpFlags,
49 PULONG LengthRead)
50 {
51 NTSTATUS Status = STATUS_SUCCESS;
52 PNTFS_FCB Fcb;
53 PFILE_RECORD_HEADER FileRecord;
54 PNTFS_ATTR_CONTEXT DataContext;
55 ULONG RealLength;
56 ULONG RealReadOffset;
57 ULONG RealLengthRead;
58 ULONG ToRead;
59 BOOLEAN AllocatedBuffer = FALSE;
60 PCHAR ReadBuffer = (PCHAR)Buffer;
61 ULONGLONG StreamSize;
62
63 DPRINT1("NtfsReadFile(%p, %p, %p, %u, %u, %x, %p)\n", DeviceExt, FileObject, Buffer, Length, ReadOffset, IrpFlags, LengthRead);
64
65 *LengthRead = 0;
66
67 if (Length == 0)
68 {
69 DPRINT1("Null read!\n");
70 return STATUS_SUCCESS;
71 }
72
73 Fcb = (PNTFS_FCB)FileObject->FsContext;
74
75 if (NtfsFCBIsCompressed(Fcb))
76 {
77 DPRINT1("Compressed file!\n");
78 UNIMPLEMENTED;
79 return STATUS_NOT_IMPLEMENTED;
80 }
81
82 FileRecord = ExAllocatePoolWithTag(NonPagedPool, DeviceExt->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
83 if (FileRecord == NULL)
84 {
85 DPRINT1("Not enough memory!\n");
86 return STATUS_INSUFFICIENT_RESOURCES;
87 }
88
89 Status = ReadFileRecord(DeviceExt, Fcb->MFTIndex, FileRecord);
90 if (!NT_SUCCESS(Status))
91 {
92 DPRINT1("Can't find record!\n");
93 ExFreePoolWithTag(FileRecord, TAG_NTFS);
94 return Status;
95 }
96
97
98 Status = FindAttribute(DeviceExt, FileRecord, AttributeData, Fcb->Stream, wcslen(Fcb->Stream), &DataContext, NULL);
99 if (!NT_SUCCESS(Status))
100 {
101 NTSTATUS BrowseStatus;
102 FIND_ATTR_CONTXT Context;
103 PNTFS_ATTR_RECORD Attribute;
104
105 DPRINT1("No '%S' data stream associated with file!\n", Fcb->Stream);
106
107 BrowseStatus = FindFirstAttribute(&Context, DeviceExt, FileRecord, FALSE, &Attribute);
108 while (NT_SUCCESS(BrowseStatus))
109 {
110 if (Attribute->Type == AttributeData)
111 {
112 UNICODE_STRING Name;
113
114 Name.Length = Attribute->NameLength * sizeof(WCHAR);
115 Name.MaximumLength = Name.Length;
116 Name.Buffer = (PWCHAR)((ULONG_PTR)Attribute + Attribute->NameOffset);
117 DPRINT1("Data stream: '%wZ' available\n", &Name);
118 }
119
120 BrowseStatus = FindNextAttribute(&Context, &Attribute);
121 }
122 FindCloseAttribute(&Context);
123
124 ReleaseAttributeContext(DataContext);
125 ExFreePoolWithTag(FileRecord, TAG_NTFS);
126 return Status;
127 }
128
129 StreamSize = AttributeDataLength(&DataContext->Record);
130 if (ReadOffset >= StreamSize)
131 {
132 DPRINT1("Reading beyond stream end!\n");
133 ReleaseAttributeContext(DataContext);
134 ExFreePoolWithTag(FileRecord, TAG_NTFS);
135 return STATUS_END_OF_FILE;
136 }
137
138 ToRead = Length;
139 if (ReadOffset + Length > StreamSize)
140 ToRead = StreamSize - ReadOffset;
141
142 RealReadOffset = ReadOffset;
143 RealLength = ToRead;
144
145 if ((ReadOffset % DeviceExt->NtfsInfo.BytesPerSector) != 0 || (ToRead % DeviceExt->NtfsInfo.BytesPerSector) != 0)
146 {
147 RealReadOffset = ROUND_DOWN(ReadOffset, DeviceExt->NtfsInfo.BytesPerSector);
148 RealLength = ROUND_UP(ToRead, DeviceExt->NtfsInfo.BytesPerSector);
149 /* do we need to extend RealLength by one sector? */
150 if (RealLength + RealReadOffset < ReadOffset + Length)
151 {
152 if (RealReadOffset + RealLength + DeviceExt->NtfsInfo.BytesPerSector <= AttributeAllocatedLength(&DataContext->Record))
153 RealLength += DeviceExt->NtfsInfo.BytesPerSector;
154 }
155
156
157 ReadBuffer = ExAllocatePoolWithTag(NonPagedPool, RealLength, TAG_NTFS);
158 if (ReadBuffer == NULL)
159 {
160 DPRINT1("Not enough memory!\n");
161 ReleaseAttributeContext(DataContext);
162 ExFreePoolWithTag(FileRecord, TAG_NTFS);
163 return STATUS_INSUFFICIENT_RESOURCES;
164 }
165 AllocatedBuffer = TRUE;
166 }
167
168 DPRINT1("Effective read: %lu at %lu for stream '%S'\n", RealLength, RealReadOffset, Fcb->Stream);
169 RealLengthRead = ReadAttribute(DeviceExt, DataContext, RealReadOffset, (PCHAR)ReadBuffer, RealLength);
170 if (RealLengthRead == 0)
171 {
172 DPRINT1("Read failure!\n");
173 ReleaseAttributeContext(DataContext);
174 ExFreePoolWithTag(FileRecord, TAG_NTFS);
175 if (AllocatedBuffer)
176 {
177 ExFreePoolWithTag(ReadBuffer, TAG_NTFS);
178 }
179 return Status;
180 }
181
182 ReleaseAttributeContext(DataContext);
183 ExFreePoolWithTag(FileRecord, TAG_NTFS);
184
185 *LengthRead = ToRead;
186
187 DPRINT1("%lu got read\n", *LengthRead);
188
189 if (AllocatedBuffer)
190 {
191 RtlCopyMemory(Buffer, ReadBuffer + (ReadOffset - RealReadOffset), ToRead);
192 }
193
194 if (ToRead != Length)
195 {
196 RtlZeroMemory(Buffer + ToRead, Length - ToRead);
197 }
198
199 if (AllocatedBuffer)
200 {
201 ExFreePoolWithTag(ReadBuffer, TAG_NTFS);
202 }
203
204 return STATUS_SUCCESS;
205 }
206
207
208 NTSTATUS
209 NtfsRead(PNTFS_IRP_CONTEXT IrpContext)
210 {
211 PDEVICE_EXTENSION DeviceExt;
212 PIO_STACK_LOCATION Stack;
213 PFILE_OBJECT FileObject;
214 PVOID Buffer;
215 ULONG ReadLength;
216 LARGE_INTEGER ReadOffset;
217 ULONG ReturnedReadLength = 0;
218 NTSTATUS Status = STATUS_SUCCESS;
219 PIRP Irp;
220 PDEVICE_OBJECT DeviceObject;
221
222 DPRINT("NtfsRead(IrpContext %p)\n", IrpContext);
223
224 DeviceObject = IrpContext->DeviceObject;
225 Irp = IrpContext->Irp;
226 Stack = IrpContext->Stack;
227 FileObject = IrpContext->FileObject;
228
229 DeviceExt = DeviceObject->DeviceExtension;
230 ReadLength = Stack->Parameters.Read.Length;
231 ReadOffset = Stack->Parameters.Read.ByteOffset;
232 Buffer = NtfsGetUserBuffer(Irp, BooleanFlagOn(Irp->Flags, IRP_PAGING_IO));
233
234 Status = NtfsReadFile(DeviceExt,
235 FileObject,
236 Buffer,
237 ReadLength,
238 ReadOffset.u.LowPart,
239 Irp->Flags,
240 &ReturnedReadLength);
241 if (NT_SUCCESS(Status))
242 {
243 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
244 {
245 FileObject->CurrentByteOffset.QuadPart =
246 ReadOffset.QuadPart + ReturnedReadLength;
247 }
248
249 Irp->IoStatus.Information = ReturnedReadLength;
250 }
251 else
252 {
253 Irp->IoStatus.Information = 0;
254 }
255
256 return Status;
257 }
258
259 /**
260 * @name NtfsWriteFile
261 * @implemented
262 *
263 * Writes a file to the disk. It presently borrows a lot of code from NtfsReadFile() and
264 * VFatWriteFileData(). It needs some more work before it will be complete; it won't handle
265 * page files, asnyc io, cached writes, etc.
266 *
267 * @param DeviceExt
268 * Points to the target disk's DEVICE_EXTENSION
269 *
270 * @param FileObject
271 * Pointer to a FILE_OBJECT describing the target file
272 *
273 * @param Buffer
274 * The data that's being written to the file
275 *
276 * @Param Length
277 * The size of the data buffer being written, in bytes
278 *
279 * @param WriteOffset
280 * Offset, in bytes, from the beginning of the file. Indicates where to start
281 * writing data.
282 *
283 * @param IrpFlags
284 * TODO: flags are presently ignored in code.
285 *
286 * @param LengthWritten
287 * Pointer to a ULONG. This ULONG will be set to the number of bytes successfully written.
288 *
289 * @return
290 * STATUS_SUCCESS if successful, STATUS_NOT_IMPLEMENTED if a required feature isn't implemented,
291 * STATUS_INSUFFICIENT_RESOURCES if an allocation failed, STATUS_ACCESS_DENIED if the write itself fails,
292 * STATUS_PARTIAL_COPY or STATUS_UNSUCCESSFUL if ReadFileRecord() fails, or
293 * STATUS_OBJECT_NAME_NOT_FOUND if the file's data stream could not be found.
294 *
295 * @remarks Called by NtfsWrite(). It may perform a read-modify-write operation if the requested write is
296 * not sector-aligned. LengthWritten only refers to how much of the requested data has been written;
297 * extra data that needs to be written to make the write sector-aligned will not affect it.
298 *
299 */
300 NTSTATUS NtfsWriteFile(PDEVICE_EXTENSION DeviceExt,
301 PFILE_OBJECT FileObject,
302 const PUCHAR Buffer,
303 ULONG Length,
304 ULONG WriteOffset,
305 ULONG IrpFlags,
306 PULONG LengthWritten)
307 {
308 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
309 PNTFS_FCB Fcb;
310 PFILE_RECORD_HEADER FileRecord;
311 PNTFS_ATTR_CONTEXT DataContext;
312 ULONG AttributeOffset;
313 ULONGLONG StreamSize;
314
315 DPRINT("NtfsWriteFile(%p, %p, %p, %u, %u, %x, %p)\n", DeviceExt, FileObject, Buffer, Length, WriteOffset, IrpFlags, LengthWritten);
316
317 *LengthWritten = 0;
318
319 ASSERT(DeviceExt);
320
321 if (Length == 0)
322 {
323 if (Buffer == NULL)
324 return STATUS_SUCCESS;
325 else
326 return STATUS_INVALID_PARAMETER;
327 }
328
329 // get the File control block
330 Fcb = (PNTFS_FCB)FileObject->FsContext;
331 ASSERT(Fcb);
332
333 DPRINT("Fcb->PathName: %wS\n", Fcb->PathName);
334 DPRINT("Fcb->ObjectName: %wS\n", Fcb->ObjectName);
335
336 // we don't yet handle compression
337 if (NtfsFCBIsCompressed(Fcb))
338 {
339 DPRINT("Compressed file!\n");
340 UNIMPLEMENTED;
341 return STATUS_NOT_IMPLEMENTED;
342 }
343
344 // allocate non-paged memory for the FILE_RECORD_HEADER
345 FileRecord = ExAllocatePoolWithTag(NonPagedPool, DeviceExt->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
346 if (FileRecord == NULL)
347 {
348 DPRINT1("Not enough memory! Can't write %wS!\n", Fcb->PathName);
349 return STATUS_INSUFFICIENT_RESOURCES;
350 }
351
352 // read the FILE_RECORD_HEADER from the drive (or cache)
353 DPRINT("Reading file record...\n");
354 Status = ReadFileRecord(DeviceExt, Fcb->MFTIndex, FileRecord);
355 if (!NT_SUCCESS(Status))
356 {
357 // We couldn't get the file's record. Free the memory and return the error
358 DPRINT1("Can't find record for %wS!\n", Fcb->ObjectName);
359 ExFreePoolWithTag(FileRecord, TAG_NTFS);
360 return Status;
361 }
362
363 DPRINT("Found record for %wS\n", Fcb->ObjectName);
364
365 // Find the attribute with the data stream for our file
366 DPRINT("Finding Data Attribute...\n");
367 Status = FindAttribute(DeviceExt, FileRecord, AttributeData, Fcb->Stream, wcslen(Fcb->Stream), &DataContext,
368 &AttributeOffset);
369
370 // Did we fail to find the attribute?
371 if (!NT_SUCCESS(Status))
372 {
373 NTSTATUS BrowseStatus;
374 FIND_ATTR_CONTXT Context;
375 PNTFS_ATTR_RECORD Attribute;
376
377 DPRINT1("No '%S' data stream associated with file!\n", Fcb->Stream);
378
379 // Couldn't find the requested data stream; print a list of streams available
380 BrowseStatus = FindFirstAttribute(&Context, DeviceExt, FileRecord, FALSE, &Attribute);
381 while (NT_SUCCESS(BrowseStatus))
382 {
383 if (Attribute->Type == AttributeData)
384 {
385 UNICODE_STRING Name;
386
387 Name.Length = Attribute->NameLength * sizeof(WCHAR);
388 Name.MaximumLength = Name.Length;
389 Name.Buffer = (PWCHAR)((ULONG_PTR)Attribute + Attribute->NameOffset);
390 DPRINT1("Data stream: '%wZ' available\n", &Name);
391 }
392
393 BrowseStatus = FindNextAttribute(&Context, &Attribute);
394 }
395 FindCloseAttribute(&Context);
396
397 ReleaseAttributeContext(DataContext);
398 ExFreePoolWithTag(FileRecord, TAG_NTFS);
399 return Status;
400 }
401
402 // Get the size of the stream on disk
403 StreamSize = AttributeDataLength(&DataContext->Record);
404
405 DPRINT("WriteOffset: %lu\tStreamSize: %I64u\n", WriteOffset, StreamSize);
406
407 // Are we trying to write beyond the end of the stream?
408 if (WriteOffset + Length > StreamSize)
409 {
410 // is increasing the stream size allowed?
411 if (!(Fcb->Flags & FCB_IS_VOLUME) &&
412 !(IrpFlags & IRP_PAGING_IO))
413 {
414 LARGE_INTEGER DataSize;
415 ULONGLONG AllocationSize;
416
417 DataSize.QuadPart = WriteOffset + Length;
418
419 AllocationSize = ROUND_UP(DataSize.QuadPart, Fcb->Vcb->NtfsInfo.BytesPerCluster);
420
421 // set the attribute data length
422 Status = SetAttributeDataLength(FileObject, Fcb, DataContext, AttributeOffset, FileRecord, DeviceExt, &DataSize);
423
424 if (!NT_SUCCESS(Status))
425 {
426 ReleaseAttributeContext(DataContext);
427 ExFreePoolWithTag(FileRecord, TAG_NTFS);
428 *LengthWritten = 0;
429 return Status;
430 }
431
432 // now we need to update this file's size in every directory index entry that references it
433 // (saved for a later commit)
434 }
435 else
436 {
437 // TODO - just fail for now
438 ReleaseAttributeContext(DataContext);
439 ExFreePoolWithTag(FileRecord, TAG_NTFS);
440 *LengthWritten = 0;
441 return STATUS_ACCESS_DENIED;
442 }
443 }
444
445 DPRINT("Length: %lu\tWriteOffset: %lu\tStreamSize: %I64u\n", Length, WriteOffset, StreamSize);
446
447 // Write the data to the attribute
448 Status = WriteAttribute(DeviceExt, DataContext, WriteOffset, Buffer, Length, LengthWritten);
449
450 // Did the write fail?
451 if (!NT_SUCCESS(Status))
452 {
453 DPRINT1("Write failure!\n");
454 ReleaseAttributeContext(DataContext);
455 ExFreePoolWithTag(FileRecord, TAG_NTFS);
456
457 return Status;
458 }
459
460 // This should never happen:
461 if (*LengthWritten != Length)
462 {
463 DPRINT1("\a\tNTFS DRIVER ERROR: length written (%lu) differs from requested (%lu), but no error was indicated!\n",
464 *LengthWritten, Length);
465 Status = STATUS_UNEXPECTED_IO_ERROR;
466 }
467
468 ReleaseAttributeContext(DataContext);
469 ExFreePoolWithTag(FileRecord, TAG_NTFS);
470
471 return Status;
472 }
473
474 /**
475 * @name NtfsWrite
476 * @implemented
477 *
478 * Handles IRP_MJ_WRITE I/O Request Packets for NTFS. This code borrows a lot from
479 * VfatWrite, and needs a lot of cleaning up. It also needs a lot more of the code
480 * from VfatWrite integrated.
481 *
482 * @param IrpContext
483 * Points to an NTFS_IRP_CONTEXT which describes the write
484 *
485 * @return
486 * STATUS_SUCCESS if successful,
487 * STATUS_INSUFFICIENT_RESOURCES if an allocation failed,
488 * STATUS_INVALID_DEVICE_REQUEST if called on the main device object,
489 * STATUS_NOT_IMPLEMENTED or STATUS_ACCESS_DENIED if a required feature isn't implemented.
490 * STATUS_PARTIAL_COPY, STATUS_UNSUCCESSFUL, or STATUS_OBJECT_NAME_NOT_FOUND if NtfsWriteFile() fails.
491 *
492 * @remarks Called by NtfsDispatch() in response to an IRP_MJ_WRITE request. Page files are not implemented.
493 * Support for large files (>4gb) is not implemented. Cached writes, file locks, transactions, etc - not implemented.
494 *
495 */
496 NTSTATUS
497 NtfsWrite(PNTFS_IRP_CONTEXT IrpContext)
498 {
499 PNTFS_FCB Fcb;
500 PERESOURCE Resource = NULL;
501 LARGE_INTEGER ByteOffset;
502 PUCHAR Buffer;
503 NTSTATUS Status = STATUS_SUCCESS;
504 ULONG Length = 0;
505 ULONG ReturnedWriteLength = 0;
506 PDEVICE_OBJECT DeviceObject = NULL;
507 PDEVICE_EXTENSION DeviceExt = NULL;
508 PFILE_OBJECT FileObject = NULL;
509 PIRP Irp = NULL;
510 ULONG BytesPerSector;
511
512 DPRINT("NtfsWrite(IrpContext %p)\n", IrpContext);
513 ASSERT(IrpContext);
514
515 // This request is not allowed on the main device object
516 if (IrpContext->DeviceObject == NtfsGlobalData->DeviceObject)
517 {
518 DPRINT1("\t\t\t\tNtfsWrite is called with the main device object.\n");
519
520 Irp->IoStatus.Information = 0;
521 return STATUS_INVALID_DEVICE_REQUEST;
522 }
523
524 // get the I/O request packet
525 Irp = IrpContext->Irp;
526
527 // get the File control block
528 Fcb = (PNTFS_FCB)IrpContext->FileObject->FsContext;
529 ASSERT(Fcb);
530
531 DPRINT("About to write %wS\n", Fcb->ObjectName);
532 DPRINT("NTFS Version: %d.%d\n", Fcb->Vcb->NtfsInfo.MajorVersion, Fcb->Vcb->NtfsInfo.MinorVersion);
533
534 // setup some more locals
535 FileObject = IrpContext->FileObject;
536 DeviceObject = IrpContext->DeviceObject;
537 DeviceExt = DeviceObject->DeviceExtension;
538 BytesPerSector = DeviceExt->StorageDevice->SectorSize;
539 Length = IrpContext->Stack->Parameters.Write.Length;
540
541 // get the file offset we'll be writing to
542 ByteOffset = IrpContext->Stack->Parameters.Write.ByteOffset;
543 if (ByteOffset.u.LowPart == FILE_WRITE_TO_END_OF_FILE &&
544 ByteOffset.u.HighPart == -1)
545 {
546 ByteOffset.QuadPart = Fcb->RFCB.FileSize.QuadPart;
547 }
548
549 DPRINT("ByteOffset: %I64u\tLength: %lu\tBytes per sector: %lu\n", ByteOffset.QuadPart,
550 Length, BytesPerSector);
551
552 if (ByteOffset.u.HighPart && !(Fcb->Flags & FCB_IS_VOLUME))
553 {
554 // TODO: Support large files
555 DPRINT1("FIXME: Writing to large files is not yet supported at this time.\n");
556 return STATUS_INVALID_PARAMETER;
557 }
558
559 // Is this a non-cached write? A non-buffered write?
560 if (IrpContext->Irp->Flags & (IRP_PAGING_IO | IRP_NOCACHE) || (Fcb->Flags & FCB_IS_VOLUME) ||
561 IrpContext->FileObject->Flags & FILE_NO_INTERMEDIATE_BUFFERING)
562 {
563 // non-cached and non-buffered writes must be sector aligned
564 if (ByteOffset.u.LowPart % BytesPerSector != 0 || Length % BytesPerSector != 0)
565 {
566 DPRINT1("Non-cached writes and non-buffered writes must be sector aligned!\n");
567 return STATUS_INVALID_PARAMETER;
568 }
569 }
570
571 if (Length == 0)
572 {
573 DPRINT1("Null write!\n");
574
575 IrpContext->Irp->IoStatus.Information = 0;
576
577 // FIXME: Doesn't accurately detect when a user passes NULL to WriteFile() for the buffer
578 if (Irp->UserBuffer == NULL && Irp->MdlAddress == NULL)
579 {
580 // FIXME: Update last write time
581 return STATUS_SUCCESS;
582 }
583
584 return STATUS_INVALID_PARAMETER;
585 }
586
587 // get the Resource
588 if (Fcb->Flags & FCB_IS_VOLUME)
589 {
590 Resource = &DeviceExt->DirResource;
591 }
592 else if (IrpContext->Irp->Flags & IRP_PAGING_IO)
593 {
594 Resource = &Fcb->PagingIoResource;
595 }
596 else
597 {
598 Resource = &Fcb->MainResource;
599 }
600
601 // acquire exclusive access to the Resource
602 if (!ExAcquireResourceExclusiveLite(Resource, BooleanFlagOn(IrpContext->Flags, IRPCONTEXT_CANWAIT)))
603 {
604 return STATUS_CANT_WAIT;
605 }
606
607 /* From VfatWrite(). Todo: Handle file locks
608 if (!(IrpContext->Irp->Flags & IRP_PAGING_IO) &&
609 FsRtlAreThereCurrentFileLocks(&Fcb->FileLock))
610 {
611 if (!FsRtlCheckLockForWriteAccess(&Fcb->FileLock, IrpContext->Irp))
612 {
613 Status = STATUS_FILE_LOCK_CONFLICT;
614 goto ByeBye;
615 }
616 }*/
617
618 // Is this an async request to a file?
619 if (!(IrpContext->Flags & IRPCONTEXT_CANWAIT) && !(Fcb->Flags & FCB_IS_VOLUME))
620 {
621 DPRINT1("FIXME: Async writes not supported in NTFS!\n");
622
623 ExReleaseResourceLite(Resource);
624 return STATUS_NOT_IMPLEMENTED;
625 }
626
627 // get the buffer of data the user is trying to write
628 Buffer = NtfsGetUserBuffer(Irp, BooleanFlagOn(Irp->Flags, IRP_PAGING_IO));
629 ASSERT(Buffer);
630
631 // lock the buffer
632 Status = NtfsLockUserBuffer(Irp, Length, IoReadAccess);
633
634 // were we unable to lock the buffer?
635 if (!NT_SUCCESS(Status))
636 {
637 DPRINT1("Unable to lock user buffer!\n");
638
639 ExReleaseResourceLite(Resource);
640 return Status;
641 }
642
643 DPRINT("Existing File Size(Fcb->RFCB.FileSize.QuadPart): %I64u\n", Fcb->RFCB.FileSize.QuadPart);
644 DPRINT("About to write the data. Length: %lu\n", Length);
645
646 // TODO: handle HighPart of ByteOffset (large files)
647
648 // write the file
649 Status = NtfsWriteFile(DeviceExt,
650 FileObject,
651 Buffer,
652 Length,
653 ByteOffset.LowPart,
654 Irp->Flags,
655 &ReturnedWriteLength);
656
657 IrpContext->Irp->IoStatus.Status = Status;
658
659 // was the write successful?
660 if (NT_SUCCESS(Status))
661 {
662 // TODO: Update timestamps
663
664 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
665 {
666 // advance the file pointer
667 FileObject->CurrentByteOffset.QuadPart = ByteOffset.QuadPart + ReturnedWriteLength;
668 }
669
670 IrpContext->PriorityBoost = IO_DISK_INCREMENT;
671 }
672 else
673 {
674 DPRINT1("Write not Succesful!\tReturned length: %lu\n", ReturnedWriteLength);
675 }
676
677 Irp->IoStatus.Information = ReturnedWriteLength;
678
679 // Note: We leave the user buffer that we locked alone, it's up to the I/O manager to unlock and free it
680
681 ExReleaseResourceLite(Resource);
682
683 return Status;
684 }
685
686 /* EOF */