3 * Copyright (C) 2002, 2014 ReactOS Team
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.
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.
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.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/mft.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
25 * Pierre Schweitzer (pierre@reactos.org)
26 * Hervé Poussineau (hpoussin@reactos.org)
30 /* INCLUDES *****************************************************************/
38 /* FUNCTIONS ****************************************************************/
41 PrepareAttributeContext(PNTFS_ATTR_RECORD AttrRecord
)
43 PNTFS_ATTR_CONTEXT Context
;
45 Context
= ExAllocatePoolWithTag(NonPagedPool
,
46 FIELD_OFFSET(NTFS_ATTR_CONTEXT
, Record
) + AttrRecord
->Length
,
48 RtlCopyMemory(&Context
->Record
, AttrRecord
, AttrRecord
->Length
);
49 if (AttrRecord
->IsNonResident
)
51 LONGLONG DataRunOffset
;
52 ULONGLONG DataRunLength
;
54 Context
->CacheRun
= (PUCHAR
)&Context
->Record
+ Context
->Record
.NonResident
.MappingPairsOffset
;
55 Context
->CacheRunOffset
= 0;
56 Context
->CacheRun
= DecodeRun(Context
->CacheRun
, &DataRunOffset
, &DataRunLength
);
57 Context
->CacheRunLength
= DataRunLength
;
58 if (DataRunOffset
!= -1)
61 Context
->CacheRunStartLCN
=
62 Context
->CacheRunLastLCN
= DataRunOffset
;
67 Context
->CacheRunStartLCN
= -1;
68 Context
->CacheRunLastLCN
= 0;
70 Context
->CacheRunCurrentOffset
= 0;
78 ReleaseAttributeContext(PNTFS_ATTR_CONTEXT Context
)
80 ExFreePoolWithTag(Context
, TAG_NTFS
);
88 * Searches a file record for an attribute matching the given type and name.
91 * Optional pointer to a ULONG that will receive the offset of the found attribute
92 * from the beginning of the record. Can be set to NULL.
95 FindAttribute(PDEVICE_EXTENSION Vcb
,
96 PFILE_RECORD_HEADER MftRecord
,
100 PNTFS_ATTR_CONTEXT
* AttrCtx
,
105 FIND_ATTR_CONTXT Context
;
106 PNTFS_ATTR_RECORD Attribute
;
108 DPRINT("FindAttribute(%p, %p, 0x%x, %S, %u, %p)\n", Vcb
, MftRecord
, Type
, Name
, NameLength
, AttrCtx
);
111 Status
= FindFirstAttribute(&Context
, Vcb
, MftRecord
, FALSE
, &Attribute
);
112 while (NT_SUCCESS(Status
))
114 if (Attribute
->Type
== Type
&& Attribute
->NameLength
== NameLength
)
120 AttrName
= (PWCHAR
)((PCHAR
)Attribute
+ Attribute
->NameOffset
);
121 DPRINT("%.*S, %.*S\n", Attribute
->NameLength
, AttrName
, NameLength
, Name
);
122 if (RtlCompareMemory(AttrName
, Name
, NameLength
<< 1) == (NameLength
<< 1))
134 /* Found it, fill up the context and return. */
135 DPRINT("Found context\n");
136 *AttrCtx
= PrepareAttributeContext(Attribute
);
138 (*AttrCtx
)->FileMFTIndex
= MftRecord
->MFTRecordNumber
;
141 *Offset
= Context
.Offset
;
143 FindCloseAttribute(&Context
);
144 return STATUS_SUCCESS
;
148 Status
= FindNextAttribute(&Context
, &Attribute
);
151 FindCloseAttribute(&Context
);
152 return STATUS_OBJECT_NAME_NOT_FOUND
;
157 AttributeAllocatedLength(PNTFS_ATTR_RECORD AttrRecord
)
159 if (AttrRecord
->IsNonResident
)
160 return AttrRecord
->NonResident
.AllocatedSize
;
162 return AttrRecord
->Resident
.ValueLength
;
167 AttributeDataLength(PNTFS_ATTR_RECORD AttrRecord
)
169 if (AttrRecord
->IsNonResident
)
170 return AttrRecord
->NonResident
.DataSize
;
172 return AttrRecord
->Resident
.ValueLength
;
176 InternalSetResidentAttributeLength(PNTFS_ATTR_CONTEXT AttrContext
,
177 PFILE_RECORD_HEADER FileRecord
,
181 ULONG EndMarker
= AttributeEnd
;
182 ULONG FinalMarker
= FILE_RECORD_END
;
183 ULONG NextAttributeOffset
;
187 DPRINT("InternalSetResidentAttributeLength( %p, %p, %lu, %lu )\n", AttrContext
, FileRecord
, AttrOffset
, DataSize
);
189 // update ValueLength Field
190 AttrContext
->Record
.Resident
.ValueLength
= DataSize
;
191 Offset
= AttrOffset
+ FIELD_OFFSET(NTFS_ATTR_RECORD
, Resident
.ValueLength
);
192 RtlCopyMemory((PCHAR
)FileRecord
+ Offset
, &DataSize
, sizeof(ULONG
));
194 // calculate the record length and end marker offset
195 AttrContext
->Record
.Length
= DataSize
+ AttrContext
->Record
.Resident
.ValueOffset
;
196 NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.Length
;
198 // Ensure NextAttributeOffset is aligned to an 8-byte boundary
199 if (NextAttributeOffset
% 8 != 0)
201 Padding
= 8 - (NextAttributeOffset
% 8);
202 NextAttributeOffset
+= Padding
;
203 AttrContext
->Record
.Length
+= Padding
;
206 // update the record length
207 Offset
= AttrOffset
+ FIELD_OFFSET(NTFS_ATTR_RECORD
, Length
);
208 RtlCopyMemory((PCHAR
)FileRecord
+ Offset
, &AttrContext
->Record
.Length
, sizeof(ULONG
));
210 // write the end marker
211 RtlCopyMemory((PCHAR
)FileRecord
+ NextAttributeOffset
, &EndMarker
, sizeof(ULONG
));
213 // write the final marker
214 Offset
= NextAttributeOffset
+ sizeof(ULONG
);
215 RtlCopyMemory((PCHAR
)FileRecord
+ Offset
, &FinalMarker
, sizeof(ULONG
));
217 FileRecord
->BytesInUse
= Offset
+ sizeof(ULONG
);
221 SetAttributeDataLength(PFILE_OBJECT FileObject
,
223 PNTFS_ATTR_CONTEXT AttrContext
,
225 PFILE_RECORD_HEADER FileRecord
,
226 PLARGE_INTEGER DataSize
)
228 NTSTATUS Status
= STATUS_SUCCESS
;
230 // are we truncating the file?
231 if (DataSize
->QuadPart
< AttributeDataLength(&AttrContext
->Record
)
233 if (!MmCanFileBeTruncated(FileObject
->SectionObjectPointer
, (PLARGE_INTEGER
)&AllocationSize
))
235 DPRINT1("Can't truncate a memory-mapped file!\n");
236 return STATUS_USER_MAPPED_FILE
;
240 if (AttrContext
->Record
.IsNonResident
)
242 ULONG BytesPerCluster
= Fcb
->Vcb
->NtfsInfo
.BytesPerCluster
;
243 ULONGLONG AllocationSize
= ROUND_UP(DataSize
->QuadPart
, BytesPerCluster
);
245 // do we need to increase the allocation size?
246 if (AttrContext
->Record
.NonResident
.AllocatedSize
< AllocationSize
)
248 ULONG ExistingClusters
= AttrContext
->Record
.NonResident
.AllocatedSize
/ BytesPerCluster
;
249 ULONG ClustersNeeded
= (AllocationSize
/ BytesPerCluster
) - ExistingClusters
;
250 LARGE_INTEGER LastClusterInDataRun
;
251 ULONG NextAssignedCluster
;
252 ULONG AssignedClusters
;
254 NTSTATUS Status
= GetLastClusterInDataRun(Fcb
->Vcb
, &AttrContext
->Record
, &LastClusterInDataRun
.QuadPart
);
256 DPRINT1("GetLastClusterInDataRun returned: %I64u\n", LastClusterInDataRun
.QuadPart
);
257 DPRINT1("Highest VCN of record: %I64u\n", AttrContext
->Record
.NonResident
.HighestVCN
);
259 while (ClustersNeeded
> 0)
261 Status
= NtfsAllocateClusters(Fcb
->Vcb
,
262 LastClusterInDataRun
.LowPart
+ 1,
264 &NextAssignedCluster
,
267 if (!NT_SUCCESS(Status
))
269 DPRINT1("Error: Unable to allocate requested clusters!\n");
273 // now we need to add the clusters we allocated to the data run
274 Status
= AddRun(AttrContext
, NextAssignedCluster
, AssignedClusters
);
275 if (!NT_SUCCESS(Status
))
277 DPRINT1("Error: Unable to add data run!\n");
281 ClustersNeeded
-= AssignedClusters
;
282 LastClusterInDataRun
.LowPart
= NextAssignedCluster
+ AssignedClusters
- 1;
285 DPRINT1("FixMe: Increasing allocation size is unimplemented!\n");
286 return STATUS_NOT_IMPLEMENTED
;
289 // TODO: is the file compressed, encrypted, or sparse?
291 // NOTE: we need to have acquired the main resource exclusively, as well as(?) the PagingIoResource
293 // TODO: update the allocated size on-disk
294 DPRINT("Allocated Size: %I64u\n", AttrContext
->Record
.NonResident
.AllocatedSize
);
296 AttrContext
->Record
.NonResident
.DataSize
= DataSize
->QuadPart
;
297 AttrContext
->Record
.NonResident
.InitializedSize
= DataSize
->QuadPart
;
299 // copy the attribute record back into the FileRecord
300 RtlCopyMemory((PCHAR
)FileRecord
+ AttrOffset
, &AttrContext
->Record
, AttrContext
->Record
.Length
);
304 // resident attribute
306 // find the next attribute
307 ULONG NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.Length
;
308 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((PCHAR
)FileRecord
+ NextAttributeOffset
);
310 //NtfsDumpFileAttributes(Fcb->Vcb, FileRecord);
312 // Do we need to increase the data length?
313 if (DataSize
->QuadPart
> AttrContext
->Record
.Resident
.ValueLength
)
315 // There's usually padding at the end of a record. Do we need to extend past it?
316 ULONG MaxValueLength
= AttrContext
->Record
.Length
- AttrContext
->Record
.Resident
.ValueOffset
;
317 if (MaxValueLength
< DataSize
->LowPart
)
319 // If this is the last attribute, we could move the end marker to the very end of the file record
320 MaxValueLength
+= Fcb
->Vcb
->NtfsInfo
.BytesPerFileRecord
- NextAttributeOffset
- (sizeof(ULONG
) * 2);
322 if (MaxValueLength
< DataSize
->LowPart
|| NextAttribute
->Type
!= AttributeEnd
)
324 DPRINT1("FIXME: Need to convert attribute to non-resident!\n");
325 return STATUS_NOT_IMPLEMENTED
;
329 else if (DataSize
->LowPart
< AttrContext
->Record
.Resident
.ValueLength
)
331 // we need to decrease the length
332 if (NextAttribute
->Type
!= AttributeEnd
)
334 DPRINT1("FIXME: Don't know how to decrease length of resident attribute unless it's the final attribute!\n");
335 return STATUS_NOT_IMPLEMENTED
;
339 InternalSetResidentAttributeLength(AttrContext
, FileRecord
, AttrOffset
, DataSize
->LowPart
);
342 //NtfsDumpFileAttributes(Fcb->Vcb, FileRecord);
344 // write the updated file record back to disk
345 Status
= UpdateFileRecord(Fcb
->Vcb
, Fcb
->MFTIndex
, FileRecord
);
347 if (NT_SUCCESS(Status
))
349 Fcb
->RFCB
.FileSize
= *DataSize
;
350 Fcb
->RFCB
.ValidDataLength
= *DataSize
;
351 CcSetFileSizes(FileObject
, (PCC_FILE_SIZES
)&Fcb
->RFCB
.AllocationSize
);
354 return STATUS_SUCCESS
;
358 ReadAttribute(PDEVICE_EXTENSION Vcb
,
359 PNTFS_ATTR_CONTEXT Context
,
366 LONGLONG DataRunOffset
;
367 ULONGLONG DataRunLength
;
368 LONGLONG DataRunStartLCN
;
369 ULONGLONG CurrentOffset
;
374 if (!Context
->Record
.IsNonResident
)
376 if (Offset
> Context
->Record
.Resident
.ValueLength
)
378 if (Offset
+ Length
> Context
->Record
.Resident
.ValueLength
)
379 Length
= (ULONG
)(Context
->Record
.Resident
.ValueLength
- Offset
);
380 RtlCopyMemory(Buffer
, (PCHAR
)&Context
->Record
+ Context
->Record
.Resident
.ValueOffset
+ Offset
, Length
);
385 * Non-resident attribute
389 * I. Find the corresponding start data run.
394 // FIXME: Cache seems to be non-working. Disable it for now
395 //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
398 DataRun
= Context
->CacheRun
;
399 LastLCN
= Context
->CacheRunLastLCN
;
400 DataRunStartLCN
= Context
->CacheRunStartLCN
;
401 DataRunLength
= Context
->CacheRunLength
;
402 CurrentOffset
= Context
->CacheRunCurrentOffset
;
407 DataRun
= (PUCHAR
)&Context
->Record
+ Context
->Record
.NonResident
.MappingPairsOffset
;
412 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
413 if (DataRunOffset
!= -1)
415 /* Normal data run. */
416 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
417 LastLCN
= DataRunStartLCN
;
421 /* Sparse data run. */
422 DataRunStartLCN
= -1;
425 if (Offset
>= CurrentOffset
&&
426 Offset
< CurrentOffset
+ (DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
))
436 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
441 * II. Go through the run list and read the data
444 ReadLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
), Length
);
445 if (DataRunStartLCN
== -1)
447 RtlZeroMemory(Buffer
, ReadLength
);
448 Status
= STATUS_SUCCESS
;
452 Status
= NtfsReadDisk(Vcb
->StorageDevice
,
453 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
+ Offset
- CurrentOffset
,
455 Vcb
->NtfsInfo
.BytesPerSector
,
459 if (NT_SUCCESS(Status
))
461 Length
-= ReadLength
;
462 Buffer
+= ReadLength
;
463 AlreadyRead
+= ReadLength
;
465 if (ReadLength
== DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
))
467 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
468 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
469 if (DataRunOffset
!= (ULONGLONG
)-1)
471 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
472 LastLCN
= DataRunStartLCN
;
475 DataRunStartLCN
= -1;
480 ReadLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
, Length
);
481 if (DataRunStartLCN
== -1)
482 RtlZeroMemory(Buffer
, ReadLength
);
485 Status
= NtfsReadDisk(Vcb
->StorageDevice
,
486 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
,
488 Vcb
->NtfsInfo
.BytesPerSector
,
491 if (!NT_SUCCESS(Status
))
495 Length
-= ReadLength
;
496 Buffer
+= ReadLength
;
497 AlreadyRead
+= ReadLength
;
499 /* We finished this request, but there still data in this data run. */
500 if (Length
== 0 && ReadLength
!= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
)
504 * Go to next run in the list.
509 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
510 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
511 if (DataRunOffset
!= -1)
513 /* Normal data run. */
514 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
515 LastLCN
= DataRunStartLCN
;
519 /* Sparse data run. */
520 DataRunStartLCN
= -1;
526 Context
->CacheRun
= DataRun
;
527 Context
->CacheRunOffset
= Offset
+ AlreadyRead
;
528 Context
->CacheRunStartLCN
= DataRunStartLCN
;
529 Context
->CacheRunLength
= DataRunLength
;
530 Context
->CacheRunLastLCN
= LastLCN
;
531 Context
->CacheRunCurrentOffset
= CurrentOffset
;
538 * @name WriteAttribute
541 * Writes an NTFS attribute to the disk. It presently borrows a lot of code from ReadAttribute(),
542 * and it still needs more documentation / cleaning up.
545 * Volume Control Block indicating which volume to write the attribute to
548 * Pointer to an NTFS_ATTR_CONTEXT that has information about the attribute
551 * Offset, in bytes, from the beginning of the attribute indicating where to start
555 * The data that's being written to the device
558 * How much data will be written, in bytes
560 * @param RealLengthWritten
561 * Pointer to a ULONG which will receive how much data was written, in bytes
564 * STATUS_SUCCESS if successful, an error code otherwise. STATUS_NOT_IMPLEMENTED if
565 * writing to a sparse file.
567 * @remarks Note that in this context the word "attribute" isn't referring read-only, hidden,
568 * etc. - the file's data is actually stored in an attribute in NTFS parlance.
573 WriteAttribute(PDEVICE_EXTENSION Vcb
,
574 PNTFS_ATTR_CONTEXT Context
,
578 PULONG RealLengthWritten
)
582 LONGLONG DataRunOffset
;
583 ULONGLONG DataRunLength
;
584 LONGLONG DataRunStartLCN
;
585 ULONGLONG CurrentOffset
;
588 PUCHAR SourceBuffer
= Buffer
;
589 LONGLONG StartingOffset
;
591 DPRINT("WriteAttribute(%p, %p, %I64u, %p, %lu, %p)\n", Vcb
, Context
, Offset
, Buffer
, Length
, RealLengthWritten
);
593 *RealLengthWritten
= 0;
595 // is this a resident attribute?
596 if (!Context
->Record
.IsNonResident
)
598 ULONG AttributeOffset
;
599 PNTFS_ATTR_CONTEXT FoundContext
;
600 PFILE_RECORD_HEADER FileRecord
;
602 if (Offset
+ Length
> Context
->Record
.Resident
.ValueLength
)
604 DPRINT1("DRIVER ERROR: Attribute is too small!\n");
605 return STATUS_INVALID_PARAMETER
;
608 FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
612 DPRINT1("Error: Couldn't allocate file record!\n");
613 return STATUS_NO_MEMORY
;
616 // read the file record
617 ReadFileRecord(Vcb
, Context
->FileMFTIndex
, FileRecord
);
619 // find where to write the attribute data to
620 Status
= FindAttribute(Vcb
, FileRecord
,
621 Context
->Record
.Type
,
622 (PCWSTR
)((PCHAR
)&Context
->Record
+ Context
->Record
.NameOffset
),
623 Context
->Record
.NameLength
,
627 if (!NT_SUCCESS(Status
))
629 DPRINT1("ERROR: Couldn't find matching attribute!\n");
630 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
634 DPRINT("Offset: %I64u, AttributeOffset: %u, ValueOffset: %u\n", Offset
, AttributeOffset
, Context
->Record
.Resident
.ValueLength
);
635 Offset
+= AttributeOffset
+ Context
->Record
.Resident
.ValueOffset
;
637 if (Offset
+ Length
> Vcb
->NtfsInfo
.BytesPerFileRecord
)
639 DPRINT1("DRIVER ERROR: Data being written extends past end of file record!\n");
640 ReleaseAttributeContext(FoundContext
);
641 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
642 return STATUS_INVALID_PARAMETER
;
645 // copy the data being written into the file record
646 RtlCopyMemory((PCHAR
)FileRecord
+ Offset
, Buffer
, Length
);
648 Status
= UpdateFileRecord(Vcb
, Context
->FileMFTIndex
, FileRecord
);
650 ReleaseAttributeContext(FoundContext
);
651 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
653 if (NT_SUCCESS(Status
))
654 *RealLengthWritten
= Length
;
659 // This is a non-resident attribute.
661 // I. Find the corresponding start data run.
663 // FIXME: Cache seems to be non-working. Disable it for now
664 //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
667 DataRun = Context->CacheRun;
668 LastLCN = Context->CacheRunLastLCN;
669 DataRunStartLCN = Context->CacheRunStartLCN;
670 DataRunLength = Context->CacheRunLength;
671 CurrentOffset = Context->CacheRunCurrentOffset;
676 DataRun
= (PUCHAR
)&Context
->Record
+ Context
->Record
.NonResident
.MappingPairsOffset
;
681 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
682 if (DataRunOffset
!= -1)
685 // DPRINT1("Writing to normal data run, LastLCN %I64u DataRunOffset %I64d\n", LastLCN, DataRunOffset);
686 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
687 LastLCN
= DataRunStartLCN
;
691 // Sparse data run. We can't support writing to sparse files yet
692 // (it may require increasing the allocation size).
693 DataRunStartLCN
= -1;
694 DPRINT1("FIXME: Writing to sparse files is not supported yet!\n");
695 return STATUS_NOT_IMPLEMENTED
;
698 // Have we reached the data run we're trying to write to?
699 if (Offset
>= CurrentOffset
&&
700 Offset
< CurrentOffset
+ (DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
))
707 // We reached the last assigned cluster
708 // TODO: assign new clusters to the end of the file.
709 // (Presently, this code will never be reached, the write should have already failed by now)
710 return STATUS_END_OF_FILE
;
713 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
717 // II. Go through the run list and write the data
719 /* REVIEWME -- As adapted from NtfsReadAttribute():
720 We seem to be making a special case for the first applicable data run, but I'm not sure why.
721 Does it have something to do with (not) caching? Is this strategy equally applicable to writing? */
723 WriteLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
), Length
);
725 StartingOffset
= DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
+ Offset
- CurrentOffset
;
727 // Write the data to the disk
728 Status
= NtfsWriteDisk(Vcb
->StorageDevice
,
731 Vcb
->NtfsInfo
.BytesPerSector
,
732 (PVOID
)SourceBuffer
);
734 // Did the write fail?
735 if (!NT_SUCCESS(Status
))
737 Context
->CacheRun
= DataRun
;
738 Context
->CacheRunOffset
= Offset
;
739 Context
->CacheRunStartLCN
= DataRunStartLCN
;
740 Context
->CacheRunLength
= DataRunLength
;
741 Context
->CacheRunLastLCN
= LastLCN
;
742 Context
->CacheRunCurrentOffset
= CurrentOffset
;
747 Length
-= WriteLength
;
748 SourceBuffer
+= WriteLength
;
749 *RealLengthWritten
+= WriteLength
;
751 // Did we write to the end of the data run?
752 if (WriteLength
== DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
))
754 // Advance to the next data run
755 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
756 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
758 if (DataRunOffset
!= (ULONGLONG
)-1)
760 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
761 LastLCN
= DataRunStartLCN
;
764 DataRunStartLCN
= -1;
767 // Do we have more data to write?
770 // Make sure we don't write past the end of the current data run
771 WriteLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
, Length
);
773 // Are we dealing with a sparse data run?
774 if (DataRunStartLCN
== -1)
776 DPRINT1("FIXME: Don't know how to write to sparse files yet! (DataRunStartLCN == -1)\n");
777 return STATUS_NOT_IMPLEMENTED
;
781 // write the data to the disk
782 Status
= NtfsWriteDisk(Vcb
->StorageDevice
,
783 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
,
785 Vcb
->NtfsInfo
.BytesPerSector
,
786 (PVOID
)SourceBuffer
);
787 if (!NT_SUCCESS(Status
))
791 Length
-= WriteLength
;
792 SourceBuffer
+= WriteLength
;
793 *RealLengthWritten
+= WriteLength
;
795 // We finished this request, but there's still data in this data run.
796 if (Length
== 0 && WriteLength
!= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
)
799 // Go to next run in the list.
803 // that was the last run
806 // Failed sanity check.
807 DPRINT1("Encountered EOF before expected!\n");
808 return STATUS_END_OF_FILE
;
814 // Advance to the next data run
815 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
816 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
817 if (DataRunOffset
!= -1)
820 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
821 LastLCN
= DataRunStartLCN
;
826 DataRunStartLCN
= -1;
828 } // end while (Length > 0) [more data to write]
830 Context
->CacheRun
= DataRun
;
831 Context
->CacheRunOffset
= Offset
+ *RealLengthWritten
;
832 Context
->CacheRunStartLCN
= DataRunStartLCN
;
833 Context
->CacheRunLength
= DataRunLength
;
834 Context
->CacheRunLastLCN
= LastLCN
;
835 Context
->CacheRunCurrentOffset
= CurrentOffset
;
841 ReadFileRecord(PDEVICE_EXTENSION Vcb
,
843 PFILE_RECORD_HEADER file
)
847 DPRINT("ReadFileRecord(%p, %I64x, %p)\n", Vcb
, index
, file
);
849 BytesRead
= ReadAttribute(Vcb
, Vcb
->MFTContext
, index
* Vcb
->NtfsInfo
.BytesPerFileRecord
, (PCHAR
)file
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
850 if (BytesRead
!= Vcb
->NtfsInfo
.BytesPerFileRecord
)
852 DPRINT1("ReadFileRecord failed: %I64u read, %u expected\n", BytesRead
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
853 return STATUS_PARTIAL_COPY
;
856 /* Apply update sequence array fixups. */
857 DPRINT("Sequence number: %u\n", file
->SequenceNumber
);
858 return FixupUpdateSequenceArray(Vcb
, &file
->Ntfs
);
863 * Searches a file's parent directory (given the parent's index in the mft)
864 * for the given file. Upon finding an index entry for that file, updates
865 * Data Size and Allocated Size values in the $FILE_NAME attribute of that entry.
867 * (Most of this code was copied from NtfsFindMftRecord)
870 UpdateFileNameRecord(PDEVICE_EXTENSION Vcb
,
871 ULONGLONG ParentMFTIndex
,
872 PUNICODE_STRING FileName
,
874 ULONGLONG NewDataSize
,
875 ULONGLONG NewAllocationSize
)
877 PFILE_RECORD_HEADER MftRecord
;
878 PNTFS_ATTR_CONTEXT IndexRootCtx
;
879 PINDEX_ROOT_ATTRIBUTE IndexRoot
;
881 PINDEX_ENTRY_ATTRIBUTE IndexEntry
, IndexEntryEnd
;
883 ULONG CurrentEntry
= 0;
885 DPRINT("UpdateFileNameRecord(%p, %I64d, %wZ, %u, %I64u, %I64u)\n", Vcb
, ParentMFTIndex
, FileName
, DirSearch
, NewDataSize
, NewAllocationSize
);
887 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
888 Vcb
->NtfsInfo
.BytesPerFileRecord
,
890 if (MftRecord
== NULL
)
892 return STATUS_INSUFFICIENT_RESOURCES
;
895 Status
= ReadFileRecord(Vcb
, ParentMFTIndex
, MftRecord
);
896 if (!NT_SUCCESS(Status
))
898 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
902 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
903 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexRoot
, L
"$I30", 4, &IndexRootCtx
, NULL
);
904 if (!NT_SUCCESS(Status
))
906 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
910 IndexRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerIndexRecord
, TAG_NTFS
);
911 if (IndexRecord
== NULL
)
913 ReleaseAttributeContext(IndexRootCtx
);
914 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
915 return STATUS_INSUFFICIENT_RESOURCES
;
918 ReadAttribute(Vcb
, IndexRootCtx
, 0, IndexRecord
, Vcb
->NtfsInfo
.BytesPerIndexRecord
);
919 IndexRoot
= (PINDEX_ROOT_ATTRIBUTE
)IndexRecord
;
920 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)&IndexRoot
->Header
+ IndexRoot
->Header
.FirstEntryOffset
);
921 // Index root is always resident.
922 IndexEntryEnd
= (PINDEX_ENTRY_ATTRIBUTE
)(IndexRecord
+ IndexRoot
->Header
.TotalSizeOfEntries
);
924 DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb
->NtfsInfo
.BytesPerIndexRecord
, IndexRoot
->SizeOfEntry
);
926 Status
= UpdateIndexEntryFileNameSize(Vcb
,
929 IndexRoot
->SizeOfEntry
,
939 ReleaseAttributeContext(IndexRootCtx
);
940 ExFreePoolWithTag(IndexRecord
, TAG_NTFS
);
941 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
947 * Recursively searches directory index and applies the size update to the $FILE_NAME attribute of the
948 * proper index entry.
949 * (Heavily based on BrowseIndexEntries)
952 UpdateIndexEntryFileNameSize(PDEVICE_EXTENSION Vcb
,
953 PFILE_RECORD_HEADER MftRecord
,
955 ULONG IndexBlockSize
,
956 PINDEX_ENTRY_ATTRIBUTE FirstEntry
,
957 PINDEX_ENTRY_ATTRIBUTE LastEntry
,
958 PUNICODE_STRING FileName
,
962 ULONGLONG NewDataSize
,
963 ULONGLONG NewAllocatedSize
)
967 PINDEX_ENTRY_ATTRIBUTE IndexEntry
;
968 PNTFS_ATTR_CONTEXT IndexAllocationCtx
;
969 ULONGLONG IndexAllocationSize
;
970 PINDEX_BUFFER IndexBuffer
;
972 DPRINT("UpdateIndexEntrySize(%p, %p, %p, %u, %p, %p, %wZ, %u, %u, %u, %I64u, %I64u)\n", Vcb
, MftRecord
, IndexRecord
, IndexBlockSize
, FirstEntry
, LastEntry
, FileName
, *StartEntry
, *CurrentEntry
, DirSearch
, NewDataSize
, NewAllocatedSize
);
974 // find the index entry responsible for the file we're trying to update
975 IndexEntry
= FirstEntry
;
976 while (IndexEntry
< LastEntry
&&
977 !(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_END
))
979 if ((IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
) > 0x10 &&
980 *CurrentEntry
>= *StartEntry
&&
981 IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_DOS
&&
982 CompareFileName(FileName
, IndexEntry
, DirSearch
))
984 *StartEntry
= *CurrentEntry
;
985 IndexEntry
->FileName
.DataSize
= NewDataSize
;
986 IndexEntry
->FileName
.AllocatedSize
= NewAllocatedSize
;
987 // indicate that the caller will still need to write the structure to the disk
988 return STATUS_PENDING
;
991 (*CurrentEntry
) += 1;
992 ASSERT(IndexEntry
->Length
>= sizeof(INDEX_ENTRY_ATTRIBUTE
));
993 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)IndexEntry
+ IndexEntry
->Length
);
996 /* If we're already browsing a subnode */
997 if (IndexRecord
== NULL
)
999 return STATUS_OBJECT_PATH_NOT_FOUND
;
1002 /* If there's no subnode */
1003 if (!(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_NODE
))
1005 return STATUS_OBJECT_PATH_NOT_FOUND
;
1008 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexAllocation
, L
"$I30", 4, &IndexAllocationCtx
, NULL
);
1009 if (!NT_SUCCESS(Status
))
1011 DPRINT("Corrupted filesystem!\n");
1015 IndexAllocationSize
= AttributeDataLength(&IndexAllocationCtx
->Record
);
1016 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
1017 for (RecordOffset
= 0; RecordOffset
< IndexAllocationSize
; RecordOffset
+= IndexBlockSize
)
1019 ReadAttribute(Vcb
, IndexAllocationCtx
, RecordOffset
, IndexRecord
, IndexBlockSize
);
1020 Status
= FixupUpdateSequenceArray(Vcb
, &((PFILE_RECORD_HEADER
)IndexRecord
)->Ntfs
);
1021 if (!NT_SUCCESS(Status
))
1026 IndexBuffer
= (PINDEX_BUFFER
)IndexRecord
;
1027 ASSERT(IndexBuffer
->Ntfs
.Type
== NRH_INDX_TYPE
);
1028 ASSERT(IndexBuffer
->Header
.AllocatedSize
+ FIELD_OFFSET(INDEX_BUFFER
, Header
) == IndexBlockSize
);
1029 FirstEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.FirstEntryOffset
);
1030 LastEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.TotalSizeOfEntries
);
1031 ASSERT(LastEntry
<= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)IndexBuffer
+ IndexBlockSize
));
1033 Status
= UpdateIndexEntryFileNameSize(NULL
, NULL
, NULL
, 0, FirstEntry
, LastEntry
, FileName
, StartEntry
, CurrentEntry
, DirSearch
, NewDataSize
, NewAllocatedSize
);
1034 if (Status
== STATUS_PENDING
)
1036 // write the index record back to disk
1039 // first we need to update the fixup values for the index block
1040 Status
= AddFixupArray(Vcb
, &((PFILE_RECORD_HEADER
)IndexRecord
)->Ntfs
);
1041 if (!NT_SUCCESS(Status
))
1043 DPRINT1("Error: Failed to update fixup sequence array!\n");
1047 Status
= WriteAttribute(Vcb
, IndexAllocationCtx
, RecordOffset
, (const PUCHAR
)IndexRecord
, IndexBlockSize
, &Written
);
1048 if (!NT_SUCCESS(Status
))
1050 DPRINT1("ERROR Performing write!\n");
1054 Status
= STATUS_SUCCESS
;
1057 if (NT_SUCCESS(Status
))
1063 ReleaseAttributeContext(IndexAllocationCtx
);
1070 * Writes a file record to the master file table, at a given index.
1073 UpdateFileRecord(PDEVICE_EXTENSION Vcb
,
1075 PFILE_RECORD_HEADER file
)
1078 NTSTATUS Status
= STATUS_SUCCESS
;
1080 DPRINT("UpdateFileRecord(%p, %I64x, %p)\n", Vcb
, index
, file
);
1082 // Add the fixup array to prepare the data for writing to disk
1083 AddFixupArray(Vcb
, &file
->Ntfs
);
1085 // write the file record to the master file table
1086 Status
= WriteAttribute(Vcb
, Vcb
->MFTContext
, index
* Vcb
->NtfsInfo
.BytesPerFileRecord
, (const PUCHAR
)file
, Vcb
->NtfsInfo
.BytesPerFileRecord
, &BytesWritten
);
1088 if (!NT_SUCCESS(Status
))
1090 DPRINT1("UpdateFileRecord failed: %I64u written, %u expected\n", BytesWritten
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
1098 FixupUpdateSequenceArray(PDEVICE_EXTENSION Vcb
,
1099 PNTFS_RECORD_HEADER Record
)
1106 USA
= (USHORT
*)((PCHAR
)Record
+ Record
->UsaOffset
);
1107 USANumber
= *(USA
++);
1108 USACount
= Record
->UsaCount
- 1; /* Exclude the USA Number. */
1109 Block
= (USHORT
*)((PCHAR
)Record
+ Vcb
->NtfsInfo
.BytesPerSector
- 2);
1111 DPRINT("FixupUpdateSequenceArray(%p, %p)\nUSANumber: %u\tUSACount: %u\n", Vcb
, Record
, USANumber
, USACount
);
1115 if (*Block
!= USANumber
)
1117 DPRINT1("Mismatch with USA: %u read, %u expected\n" , *Block
, USANumber
);
1118 return STATUS_UNSUCCESSFUL
;
1121 Block
= (USHORT
*)((PCHAR
)Block
+ Vcb
->NtfsInfo
.BytesPerSector
);
1125 return STATUS_SUCCESS
;
1129 AddFixupArray(PDEVICE_EXTENSION Vcb
,
1130 PNTFS_RECORD_HEADER Record
)
1132 USHORT
*pShortToFixUp
;
1133 unsigned int ArrayEntryCount
= Record
->UsaCount
- 1;
1134 unsigned int Offset
= Vcb
->NtfsInfo
.BytesPerSector
- 2;
1137 PFIXUP_ARRAY fixupArray
= (PFIXUP_ARRAY
)((UCHAR
*)Record
+ Record
->UsaOffset
);
1139 DPRINT("AddFixupArray(%p, %p)\n fixupArray->USN: %u, ArrayEntryCount: %u\n", Vcb
, Record
, fixupArray
->USN
, ArrayEntryCount
);
1143 for (i
= 0; i
< ArrayEntryCount
; i
++)
1145 DPRINT("USN: %u\tOffset: %u\n", fixupArray
->USN
, Offset
);
1147 pShortToFixUp
= (USHORT
*)((PCHAR
)Record
+ Offset
);
1148 fixupArray
->Array
[i
] = *pShortToFixUp
;
1149 *pShortToFixUp
= fixupArray
->USN
;
1150 Offset
+= Vcb
->NtfsInfo
.BytesPerSector
;
1153 return STATUS_SUCCESS
;
1157 ReadLCN(PDEVICE_EXTENSION Vcb
,
1162 LARGE_INTEGER DiskSector
;
1164 DiskSector
.QuadPart
= lcn
;
1166 return NtfsReadSectors(Vcb
->StorageDevice
,
1167 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
1168 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
1169 Vcb
->NtfsInfo
.BytesPerSector
,
1176 CompareFileName(PUNICODE_STRING FileName
,
1177 PINDEX_ENTRY_ATTRIBUTE IndexEntry
,
1180 BOOLEAN Ret
, Alloc
= FALSE
;
1181 UNICODE_STRING EntryName
;
1183 EntryName
.Buffer
= IndexEntry
->FileName
.Name
;
1185 EntryName
.MaximumLength
= IndexEntry
->FileName
.NameLength
* sizeof(WCHAR
);
1189 UNICODE_STRING IntFileName
;
1190 if (IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_POSIX
)
1192 NT_VERIFY(NT_SUCCESS(RtlUpcaseUnicodeString(&IntFileName
, FileName
, TRUE
)));
1197 IntFileName
= *FileName
;
1200 Ret
= FsRtlIsNameInExpression(&IntFileName
, &EntryName
, (IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_POSIX
), NULL
);
1204 RtlFreeUnicodeString(&IntFileName
);
1211 return (RtlCompareUnicodeString(FileName
, &EntryName
, (IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_POSIX
)) == 0);
1218 DumpIndexEntry(PINDEX_ENTRY_ATTRIBUTE IndexEntry
)
1220 DPRINT1("Entry: %p\n", IndexEntry
);
1221 DPRINT1("\tData.Directory.IndexedFile: %I64x\n", IndexEntry
->Data
.Directory
.IndexedFile
);
1222 DPRINT1("\tLength: %u\n", IndexEntry
->Length
);
1223 DPRINT1("\tKeyLength: %u\n", IndexEntry
->KeyLength
);
1224 DPRINT1("\tFlags: %x\n", IndexEntry
->Flags
);
1225 DPRINT1("\tReserved: %x\n", IndexEntry
->Reserved
);
1226 DPRINT1("\t\tDirectoryFileReferenceNumber: %I64x\n", IndexEntry
->FileName
.DirectoryFileReferenceNumber
);
1227 DPRINT1("\t\tCreationTime: %I64u\n", IndexEntry
->FileName
.CreationTime
);
1228 DPRINT1("\t\tChangeTime: %I64u\n", IndexEntry
->FileName
.ChangeTime
);
1229 DPRINT1("\t\tLastWriteTime: %I64u\n", IndexEntry
->FileName
.LastWriteTime
);
1230 DPRINT1("\t\tLastAccessTime: %I64u\n", IndexEntry
->FileName
.LastAccessTime
);
1231 DPRINT1("\t\tAllocatedSize: %I64u\n", IndexEntry
->FileName
.AllocatedSize
);
1232 DPRINT1("\t\tDataSize: %I64u\n", IndexEntry
->FileName
.DataSize
);
1233 DPRINT1("\t\tFileAttributes: %x\n", IndexEntry
->FileName
.FileAttributes
);
1234 DPRINT1("\t\tNameLength: %u\n", IndexEntry
->FileName
.NameLength
);
1235 DPRINT1("\t\tNameType: %x\n", IndexEntry
->FileName
.NameType
);
1236 DPRINT1("\t\tName: %.*S\n", IndexEntry
->FileName
.NameLength
, IndexEntry
->FileName
.Name
);
1241 BrowseIndexEntries(PDEVICE_EXTENSION Vcb
,
1242 PFILE_RECORD_HEADER MftRecord
,
1244 ULONG IndexBlockSize
,
1245 PINDEX_ENTRY_ATTRIBUTE FirstEntry
,
1246 PINDEX_ENTRY_ATTRIBUTE LastEntry
,
1247 PUNICODE_STRING FileName
,
1249 PULONG CurrentEntry
,
1251 ULONGLONG
*OutMFTIndex
)
1255 PINDEX_ENTRY_ATTRIBUTE IndexEntry
;
1256 PNTFS_ATTR_CONTEXT IndexAllocationCtx
;
1257 ULONGLONG IndexAllocationSize
;
1258 PINDEX_BUFFER IndexBuffer
;
1260 DPRINT("BrowseIndexEntries(%p, %p, %p, %u, %p, %p, %wZ, %u, %u, %u, %p)\n", Vcb
, MftRecord
, IndexRecord
, IndexBlockSize
, FirstEntry
, LastEntry
, FileName
, *StartEntry
, *CurrentEntry
, DirSearch
, OutMFTIndex
);
1262 IndexEntry
= FirstEntry
;
1263 while (IndexEntry
< LastEntry
&&
1264 !(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_END
))
1266 if ((IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
) > 0x10 &&
1267 *CurrentEntry
>= *StartEntry
&&
1268 IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_DOS
&&
1269 CompareFileName(FileName
, IndexEntry
, DirSearch
))
1271 *StartEntry
= *CurrentEntry
;
1272 *OutMFTIndex
= (IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
);
1273 return STATUS_SUCCESS
;
1276 (*CurrentEntry
) += 1;
1277 ASSERT(IndexEntry
->Length
>= sizeof(INDEX_ENTRY_ATTRIBUTE
));
1278 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)IndexEntry
+ IndexEntry
->Length
);
1281 /* If we're already browsing a subnode */
1282 if (IndexRecord
== NULL
)
1284 return STATUS_OBJECT_PATH_NOT_FOUND
;
1287 /* If there's no subnode */
1288 if (!(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_NODE
))
1290 return STATUS_OBJECT_PATH_NOT_FOUND
;
1293 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexAllocation
, L
"$I30", 4, &IndexAllocationCtx
, NULL
);
1294 if (!NT_SUCCESS(Status
))
1296 DPRINT("Corrupted filesystem!\n");
1300 IndexAllocationSize
= AttributeDataLength(&IndexAllocationCtx
->Record
);
1301 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
1302 for (RecordOffset
= 0; RecordOffset
< IndexAllocationSize
; RecordOffset
+= IndexBlockSize
)
1304 ReadAttribute(Vcb
, IndexAllocationCtx
, RecordOffset
, IndexRecord
, IndexBlockSize
);
1305 Status
= FixupUpdateSequenceArray(Vcb
, &((PFILE_RECORD_HEADER
)IndexRecord
)->Ntfs
);
1306 if (!NT_SUCCESS(Status
))
1311 IndexBuffer
= (PINDEX_BUFFER
)IndexRecord
;
1312 ASSERT(IndexBuffer
->Ntfs
.Type
== NRH_INDX_TYPE
);
1313 ASSERT(IndexBuffer
->Header
.AllocatedSize
+ FIELD_OFFSET(INDEX_BUFFER
, Header
) == IndexBlockSize
);
1314 FirstEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.FirstEntryOffset
);
1315 LastEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.TotalSizeOfEntries
);
1316 ASSERT(LastEntry
<= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)IndexBuffer
+ IndexBlockSize
));
1318 Status
= BrowseIndexEntries(NULL
, NULL
, NULL
, 0, FirstEntry
, LastEntry
, FileName
, StartEntry
, CurrentEntry
, DirSearch
, OutMFTIndex
);
1319 if (NT_SUCCESS(Status
))
1325 ReleaseAttributeContext(IndexAllocationCtx
);
1330 NtfsFindMftRecord(PDEVICE_EXTENSION Vcb
,
1332 PUNICODE_STRING FileName
,
1335 ULONGLONG
*OutMFTIndex
)
1337 PFILE_RECORD_HEADER MftRecord
;
1338 PNTFS_ATTR_CONTEXT IndexRootCtx
;
1339 PINDEX_ROOT_ATTRIBUTE IndexRoot
;
1341 PINDEX_ENTRY_ATTRIBUTE IndexEntry
, IndexEntryEnd
;
1343 ULONG CurrentEntry
= 0;
1345 DPRINT("NtfsFindMftRecord(%p, %I64d, %wZ, %u, %u, %p)\n", Vcb
, MFTIndex
, FileName
, *FirstEntry
, DirSearch
, OutMFTIndex
);
1347 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
1348 Vcb
->NtfsInfo
.BytesPerFileRecord
,
1350 if (MftRecord
== NULL
)
1352 return STATUS_INSUFFICIENT_RESOURCES
;
1355 Status
= ReadFileRecord(Vcb
, MFTIndex
, MftRecord
);
1356 if (!NT_SUCCESS(Status
))
1358 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1362 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
1363 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexRoot
, L
"$I30", 4, &IndexRootCtx
, NULL
);
1364 if (!NT_SUCCESS(Status
))
1366 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1370 IndexRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerIndexRecord
, TAG_NTFS
);
1371 if (IndexRecord
== NULL
)
1373 ReleaseAttributeContext(IndexRootCtx
);
1374 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1375 return STATUS_INSUFFICIENT_RESOURCES
;
1378 ReadAttribute(Vcb
, IndexRootCtx
, 0, IndexRecord
, Vcb
->NtfsInfo
.BytesPerIndexRecord
);
1379 IndexRoot
= (PINDEX_ROOT_ATTRIBUTE
)IndexRecord
;
1380 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)&IndexRoot
->Header
+ IndexRoot
->Header
.FirstEntryOffset
);
1381 /* Index root is always resident. */
1382 IndexEntryEnd
= (PINDEX_ENTRY_ATTRIBUTE
)(IndexRecord
+ IndexRoot
->Header
.TotalSizeOfEntries
);
1383 ReleaseAttributeContext(IndexRootCtx
);
1385 DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb
->NtfsInfo
.BytesPerIndexRecord
, IndexRoot
->SizeOfEntry
);
1387 Status
= BrowseIndexEntries(Vcb
, MftRecord
, IndexRecord
, IndexRoot
->SizeOfEntry
, IndexEntry
, IndexEntryEnd
, FileName
, FirstEntry
, &CurrentEntry
, DirSearch
, OutMFTIndex
);
1389 ExFreePoolWithTag(IndexRecord
, TAG_NTFS
);
1390 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1396 NtfsLookupFileAt(PDEVICE_EXTENSION Vcb
,
1397 PUNICODE_STRING PathName
,
1398 PFILE_RECORD_HEADER
*FileRecord
,
1399 PULONGLONG MFTIndex
,
1400 ULONGLONG CurrentMFTIndex
)
1402 UNICODE_STRING Current
, Remaining
;
1404 ULONG FirstEntry
= 0;
1406 DPRINT("NtfsLookupFileAt(%p, %wZ, %p, %I64x)\n", Vcb
, PathName
, FileRecord
, CurrentMFTIndex
);
1408 FsRtlDissectName(*PathName
, &Current
, &Remaining
);
1410 while (Current
.Length
!= 0)
1412 DPRINT("Current: %wZ\n", &Current
);
1414 Status
= NtfsFindMftRecord(Vcb
, CurrentMFTIndex
, &Current
, &FirstEntry
, FALSE
, &CurrentMFTIndex
);
1415 if (!NT_SUCCESS(Status
))
1420 if (Remaining
.Length
== 0)
1423 FsRtlDissectName(Current
, &Current
, &Remaining
);
1426 *FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
1427 if (*FileRecord
== NULL
)
1429 DPRINT("NtfsLookupFileAt: Can't allocate MFT record\n");
1430 return STATUS_INSUFFICIENT_RESOURCES
;
1433 Status
= ReadFileRecord(Vcb
, CurrentMFTIndex
, *FileRecord
);
1434 if (!NT_SUCCESS(Status
))
1436 DPRINT("NtfsLookupFileAt: Can't read MFT record\n");
1437 ExFreePoolWithTag(*FileRecord
, TAG_NTFS
);
1441 *MFTIndex
= CurrentMFTIndex
;
1443 return STATUS_SUCCESS
;
1447 NtfsLookupFile(PDEVICE_EXTENSION Vcb
,
1448 PUNICODE_STRING PathName
,
1449 PFILE_RECORD_HEADER
*FileRecord
,
1450 PULONGLONG MFTIndex
)
1452 return NtfsLookupFileAt(Vcb
, PathName
, FileRecord
, MFTIndex
, NTFS_FILE_ROOT
);
1456 NtfsFindFileAt(PDEVICE_EXTENSION Vcb
,
1457 PUNICODE_STRING SearchPattern
,
1459 PFILE_RECORD_HEADER
*FileRecord
,
1460 PULONGLONG MFTIndex
,
1461 ULONGLONG CurrentMFTIndex
)
1465 DPRINT("NtfsFindFileAt(%p, %wZ, %u, %p, %p, %I64x)\n", Vcb
, SearchPattern
, *FirstEntry
, FileRecord
, MFTIndex
, CurrentMFTIndex
);
1467 Status
= NtfsFindMftRecord(Vcb
, CurrentMFTIndex
, SearchPattern
, FirstEntry
, TRUE
, &CurrentMFTIndex
);
1468 if (!NT_SUCCESS(Status
))
1470 DPRINT("NtfsFindFileAt: NtfsFindMftRecord() failed with status 0x%08lx\n", Status
);
1474 *FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
1475 if (*FileRecord
== NULL
)
1477 DPRINT("NtfsFindFileAt: Can't allocate MFT record\n");
1478 return STATUS_INSUFFICIENT_RESOURCES
;
1481 Status
= ReadFileRecord(Vcb
, CurrentMFTIndex
, *FileRecord
);
1482 if (!NT_SUCCESS(Status
))
1484 DPRINT("NtfsFindFileAt: Can't read MFT record\n");
1485 ExFreePoolWithTag(*FileRecord
, TAG_NTFS
);
1489 *MFTIndex
= CurrentMFTIndex
;
1491 return STATUS_SUCCESS
;