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
);
139 *Offset
= Context
.Offset
;
141 FindCloseAttribute(&Context
);
142 return STATUS_SUCCESS
;
146 Status
= FindNextAttribute(&Context
, &Attribute
);
149 FindCloseAttribute(&Context
);
150 return STATUS_OBJECT_NAME_NOT_FOUND
;
155 AttributeAllocatedLength(PNTFS_ATTR_RECORD AttrRecord
)
157 if (AttrRecord
->IsNonResident
)
158 return AttrRecord
->NonResident
.AllocatedSize
;
160 return AttrRecord
->Resident
.ValueLength
;
165 AttributeDataLength(PNTFS_ATTR_RECORD AttrRecord
)
167 if (AttrRecord
->IsNonResident
)
168 return AttrRecord
->NonResident
.DataSize
;
170 return AttrRecord
->Resident
.ValueLength
;
175 SetAttributeDataLength(PFILE_OBJECT FileObject
,
177 PNTFS_ATTR_CONTEXT AttrContext
,
179 PFILE_RECORD_HEADER FileRecord
,
180 PDEVICE_EXTENSION DeviceExt
,
181 PLARGE_INTEGER DataSize
)
183 if (AttrContext
->Record
.IsNonResident
)
185 // do we need to increase the allocation size?
186 if (AttrContext
->Record
.NonResident
.AllocatedSize
< DataSize
->QuadPart
)
188 DPRINT1("FixMe: Increasing allocation size is unimplemented!\n");
189 return STATUS_NOT_IMPLEMENTED
;
192 // TODO: is the file compressed, encrypted, or sparse?
194 // NOTE: we need to have acquired the main resource exclusively, as well as(?) the PagingIoResource
196 // TODO: update the allocated size on-disk
197 DPRINT("Allocated Size: %I64u\n", AttrContext
->Record
.NonResident
.AllocatedSize
);
199 AttrContext
->Record
.NonResident
.DataSize
= DataSize
->QuadPart
;
200 AttrContext
->Record
.NonResident
.InitializedSize
= DataSize
->QuadPart
;
202 Fcb
->RFCB
.FileSize
= *DataSize
;
203 Fcb
->RFCB
.ValidDataLength
= *DataSize
;
205 DPRINT("Data Size: %I64u\n", Fcb
->RFCB
.FileSize
.QuadPart
);
207 //NtfsDumpFileAttributes(Fcb->Vcb, FileRecord);
209 // copy the attribute back into the FileRecord
210 RtlCopyMemory((PCHAR
)FileRecord
+ AttrOffset
, &AttrContext
->Record
, AttrContext
->Record
.Length
);
212 //NtfsDumpFileAttributes(Fcb->Vcb, FileRecord);
214 // write the updated file record back to disk
215 UpdateFileRecord(Fcb
->Vcb
, Fcb
->MFTIndex
, FileRecord
);
217 CcSetFileSizes(FileObject
, (PCC_FILE_SIZES
)&Fcb
->RFCB
.AllocationSize
);
221 // we can't yet handle resident attributes
222 DPRINT1("FixMe: Can't handle increasing length of resident attribute\n");
223 return STATUS_NOT_IMPLEMENTED
;
226 return STATUS_SUCCESS
;
230 ReadAttribute(PDEVICE_EXTENSION Vcb
,
231 PNTFS_ATTR_CONTEXT Context
,
238 LONGLONG DataRunOffset
;
239 ULONGLONG DataRunLength
;
240 LONGLONG DataRunStartLCN
;
241 ULONGLONG CurrentOffset
;
246 if (!Context
->Record
.IsNonResident
)
248 if (Offset
> Context
->Record
.Resident
.ValueLength
)
250 if (Offset
+ Length
> Context
->Record
.Resident
.ValueLength
)
251 Length
= (ULONG
)(Context
->Record
.Resident
.ValueLength
- Offset
);
252 RtlCopyMemory(Buffer
, (PCHAR
)&Context
->Record
+ Context
->Record
.Resident
.ValueOffset
+ Offset
, Length
);
257 * Non-resident attribute
261 * I. Find the corresponding start data run.
266 // FIXME: Cache seems to be non-working. Disable it for now
267 //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
270 DataRun
= Context
->CacheRun
;
271 LastLCN
= Context
->CacheRunLastLCN
;
272 DataRunStartLCN
= Context
->CacheRunStartLCN
;
273 DataRunLength
= Context
->CacheRunLength
;
274 CurrentOffset
= Context
->CacheRunCurrentOffset
;
279 DataRun
= (PUCHAR
)&Context
->Record
+ Context
->Record
.NonResident
.MappingPairsOffset
;
284 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
285 if (DataRunOffset
!= -1)
287 /* Normal data run. */
288 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
289 LastLCN
= DataRunStartLCN
;
293 /* Sparse data run. */
294 DataRunStartLCN
= -1;
297 if (Offset
>= CurrentOffset
&&
298 Offset
< CurrentOffset
+ (DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
))
308 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
313 * II. Go through the run list and read the data
316 ReadLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
), Length
);
317 if (DataRunStartLCN
== -1)
319 RtlZeroMemory(Buffer
, ReadLength
);
320 Status
= STATUS_SUCCESS
;
324 Status
= NtfsReadDisk(Vcb
->StorageDevice
,
325 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
+ Offset
- CurrentOffset
,
327 Vcb
->NtfsInfo
.BytesPerSector
,
331 if (NT_SUCCESS(Status
))
333 Length
-= ReadLength
;
334 Buffer
+= ReadLength
;
335 AlreadyRead
+= ReadLength
;
337 if (ReadLength
== DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
))
339 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
340 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
341 if (DataRunOffset
!= (ULONGLONG
)-1)
343 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
344 LastLCN
= DataRunStartLCN
;
347 DataRunStartLCN
= -1;
352 ReadLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
, Length
);
353 if (DataRunStartLCN
== -1)
354 RtlZeroMemory(Buffer
, ReadLength
);
357 Status
= NtfsReadDisk(Vcb
->StorageDevice
,
358 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
,
360 Vcb
->NtfsInfo
.BytesPerSector
,
363 if (!NT_SUCCESS(Status
))
367 Length
-= ReadLength
;
368 Buffer
+= ReadLength
;
369 AlreadyRead
+= ReadLength
;
371 /* We finished this request, but there still data in this data run. */
372 if (Length
== 0 && ReadLength
!= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
)
376 * Go to next run in the list.
381 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
382 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
383 if (DataRunOffset
!= -1)
385 /* Normal data run. */
386 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
387 LastLCN
= DataRunStartLCN
;
391 /* Sparse data run. */
392 DataRunStartLCN
= -1;
398 Context
->CacheRun
= DataRun
;
399 Context
->CacheRunOffset
= Offset
+ AlreadyRead
;
400 Context
->CacheRunStartLCN
= DataRunStartLCN
;
401 Context
->CacheRunLength
= DataRunLength
;
402 Context
->CacheRunLastLCN
= LastLCN
;
403 Context
->CacheRunCurrentOffset
= CurrentOffset
;
410 * @name WriteAttribute
413 * Writes an NTFS attribute to the disk. It presently borrows a lot of code from ReadAttribute(),
414 * and it still needs more documentation / cleaning up.
417 * Volume Control Block indicating which volume to write the attribute to
420 * Pointer to an NTFS_ATTR_CONTEXT that has information about the attribute
423 * Offset, in bytes, from the beginning of the attribute indicating where to start
427 * The data that's being written to the device
430 * How much data will be written, in bytes
432 * @param RealLengthWritten
433 * Pointer to a ULONG which will receive how much data was written, in bytes
436 * STATUS_SUCCESS if successful, an error code otherwise. STATUS_NOT_IMPLEMENTED if
437 * writing to a sparse file.
439 * @remarks Note that in this context the word "attribute" isn't referring read-only, hidden,
440 * etc. - the file's data is actually stored in an attribute in NTFS parlance.
445 WriteAttribute(PDEVICE_EXTENSION Vcb
,
446 PNTFS_ATTR_CONTEXT Context
,
450 PULONG RealLengthWritten
)
454 LONGLONG DataRunOffset
;
455 ULONGLONG DataRunLength
;
456 LONGLONG DataRunStartLCN
;
457 ULONGLONG CurrentOffset
;
460 PUCHAR SourceBuffer
= Buffer
;
461 LONGLONG StartingOffset
;
463 DPRINT("WriteAttribute(%p, %p, %I64U, %p, %lu)\n", Vcb
, Context
, Offset
, Buffer
, Length
);
465 // is this a resident attribute?
466 if (!Context
->Record
.IsNonResident
)
468 DPRINT1("FIXME: Writing to resident NTFS records (small files) is not supported at this time.\n");
469 // (TODO: This should be really easy to implement)
471 /* LeftOver code from ReadAttribute(), may be helpful:
472 if (Offset > Context->Record.Resident.ValueLength)
474 if (Offset + Length > Context->Record.Resident.ValueLength)
475 Length = (ULONG)(Context->Record.Resident.ValueLength - Offset);
476 RtlCopyMemory(Buffer, (PCHAR)&Context->Record + Context->Record.Resident.ValueOffset + Offset, Length);
479 return STATUS_NOT_IMPLEMENTED
; // until we implement it
482 // This is a non-resident attribute.
484 // I. Find the corresponding start data run.
486 *RealLengthWritten
= 0;
488 // FIXME: Cache seems to be non-working. Disable it for now
489 //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
492 DataRun = Context->CacheRun;
493 LastLCN = Context->CacheRunLastLCN;
494 DataRunStartLCN = Context->CacheRunStartLCN;
495 DataRunLength = Context->CacheRunLength;
496 CurrentOffset = Context->CacheRunCurrentOffset;
501 DataRun
= (PUCHAR
)&Context
->Record
+ Context
->Record
.NonResident
.MappingPairsOffset
;
506 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
507 if (DataRunOffset
!= -1)
510 // DPRINT1("Writing to normal data run, LastLCN %I64u DataRunOffset %I64d\n", LastLCN, DataRunOffset);
511 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
512 LastLCN
= DataRunStartLCN
;
516 // Sparse data run. We can't support writing to sparse files yet
517 // (it may require increasing the allocation size).
518 DataRunStartLCN
= -1;
519 DPRINT1("FIXME: Writing to sparse files is not supported yet!\n");
520 return STATUS_NOT_IMPLEMENTED
;
523 // Have we reached the data run we're trying to write to?
524 if (Offset
>= CurrentOffset
&&
525 Offset
< CurrentOffset
+ (DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
))
532 // We reached the last assigned cluster
533 // TODO: assign new clusters to the end of the file.
534 // (Presently, this code will never be reached, the write should have already failed by now)
535 return STATUS_END_OF_FILE
;
538 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
542 // II. Go through the run list and write the data
544 /* REVIEWME -- As adapted from NtfsReadAttribute():
545 We seem to be making a special case for the first applicable data run, but I'm not sure why.
546 Does it have something to do with (not) caching? Is this strategy equally applicable to writing? */
548 WriteLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
), Length
);
550 StartingOffset
= DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
+ Offset
- CurrentOffset
;
552 // Write the data to the disk
553 Status
= NtfsWriteDisk(Vcb
->StorageDevice
,
556 Vcb
->NtfsInfo
.BytesPerSector
,
557 (PVOID
)SourceBuffer
);
559 // Did the write fail?
560 if (!NT_SUCCESS(Status
))
562 Context
->CacheRun
= DataRun
;
563 Context
->CacheRunOffset
= Offset
;
564 Context
->CacheRunStartLCN
= DataRunStartLCN
;
565 Context
->CacheRunLength
= DataRunLength
;
566 Context
->CacheRunLastLCN
= LastLCN
;
567 Context
->CacheRunCurrentOffset
= CurrentOffset
;
572 Length
-= WriteLength
;
573 SourceBuffer
+= WriteLength
;
574 *RealLengthWritten
+= WriteLength
;
576 // Did we write to the end of the data run?
577 if (WriteLength
== DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
))
579 // Advance to the next data run
580 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
581 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
583 if (DataRunOffset
!= (ULONGLONG
)-1)
585 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
586 LastLCN
= DataRunStartLCN
;
589 DataRunStartLCN
= -1;
592 // Do we have more data to write?
595 // Make sure we don't write past the end of the current data run
596 WriteLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
, Length
);
598 // Are we dealing with a sparse data run?
599 if (DataRunStartLCN
== -1)
601 DPRINT1("FIXME: Don't know how to write to sparse files yet! (DataRunStartLCN == -1)\n");
602 return STATUS_NOT_IMPLEMENTED
;
606 // write the data to the disk
607 Status
= NtfsWriteDisk(Vcb
->StorageDevice
,
608 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
,
610 Vcb
->NtfsInfo
.BytesPerSector
,
611 (PVOID
)SourceBuffer
);
612 if (!NT_SUCCESS(Status
))
616 Length
-= WriteLength
;
617 SourceBuffer
+= WriteLength
;
618 *RealLengthWritten
+= WriteLength
;
620 // We finished this request, but there's still data in this data run.
621 if (Length
== 0 && WriteLength
!= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
)
624 // Go to next run in the list.
628 // that was the last run
631 // Failed sanity check.
632 DPRINT1("Encountered EOF before expected!\n");
633 return STATUS_END_OF_FILE
;
639 // Advance to the next data run
640 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
641 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
642 if (DataRunOffset
!= -1)
645 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
646 LastLCN
= DataRunStartLCN
;
651 DataRunStartLCN
= -1;
653 } // end while (Length > 0) [more data to write]
655 Context
->CacheRun
= DataRun
;
656 Context
->CacheRunOffset
= Offset
+ *RealLengthWritten
;
657 Context
->CacheRunStartLCN
= DataRunStartLCN
;
658 Context
->CacheRunLength
= DataRunLength
;
659 Context
->CacheRunLastLCN
= LastLCN
;
660 Context
->CacheRunCurrentOffset
= CurrentOffset
;
666 ReadFileRecord(PDEVICE_EXTENSION Vcb
,
668 PFILE_RECORD_HEADER file
)
672 DPRINT("ReadFileRecord(%p, %I64x, %p)\n", Vcb
, index
, file
);
674 BytesRead
= ReadAttribute(Vcb
, Vcb
->MFTContext
, index
* Vcb
->NtfsInfo
.BytesPerFileRecord
, (PCHAR
)file
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
675 if (BytesRead
!= Vcb
->NtfsInfo
.BytesPerFileRecord
)
677 DPRINT1("ReadFileRecord failed: %I64u read, %u expected\n", BytesRead
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
678 return STATUS_PARTIAL_COPY
;
681 /* Apply update sequence array fixups. */
682 return FixupUpdateSequenceArray(Vcb
, &file
->Ntfs
);
688 * Writes a file record to the master file table, at a given index.
691 UpdateFileRecord(PDEVICE_EXTENSION Vcb
,
693 PFILE_RECORD_HEADER file
)
696 NTSTATUS Status
= STATUS_SUCCESS
;
698 DPRINT("UpdateFileRecord(%p, %I64x, %p)\n", Vcb
, index
, file
);
700 // Add the fixup array to prepare the data for writing to disk
701 AddFixupArray(Vcb
, &file
->Ntfs
);
703 // write the file record to the master file table
704 Status
= WriteAttribute(Vcb
, Vcb
->MFTContext
, index
* Vcb
->NtfsInfo
.BytesPerFileRecord
, (const PUCHAR
)file
, Vcb
->NtfsInfo
.BytesPerFileRecord
, &BytesWritten
);
706 // TODO: Update MFT mirror
708 if (!NT_SUCCESS(Status
))
710 DPRINT1("UpdateFileRecord failed: %I64u written, %u expected\n", BytesWritten
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
718 FixupUpdateSequenceArray(PDEVICE_EXTENSION Vcb
,
719 PNTFS_RECORD_HEADER Record
)
726 USA
= (USHORT
*)((PCHAR
)Record
+ Record
->UsaOffset
);
727 USANumber
= *(USA
++);
728 USACount
= Record
->UsaCount
- 1; /* Exclude the USA Number. */
729 Block
= (USHORT
*)((PCHAR
)Record
+ Vcb
->NtfsInfo
.BytesPerSector
- 2);
731 DPRINT("FixupUpdateSequenceArray(%p, %p)\nUSANumber: %u\tUSACount: %u\n", Vcb
, Record
, USANumber
, USACount
);
735 if (*Block
!= USANumber
)
737 DPRINT1("Mismatch with USA: %u read, %u expected\n" , *Block
, USANumber
);
738 return STATUS_UNSUCCESSFUL
;
741 Block
= (USHORT
*)((PCHAR
)Block
+ Vcb
->NtfsInfo
.BytesPerSector
);
745 return STATUS_SUCCESS
;
749 AddFixupArray(PDEVICE_EXTENSION Vcb
,
750 PNTFS_RECORD_HEADER Record
)
752 USHORT
*pShortToFixUp
;
753 unsigned int ArrayEntryCount
= Record
->UsaCount
- 1;
754 unsigned int Offset
= Vcb
->NtfsInfo
.BytesPerSector
- 2;
757 PFIXUP_ARRAY fixupArray
= (PFIXUP_ARRAY
)((UCHAR
*)Record
+ Record
->UsaOffset
);
759 DPRINT("AddFixupArray(%p, %p)\n fixupArray->USN: %u, ArrayEntryCount: %u\n", Vcb
, Record
, fixupArray
->USN
, ArrayEntryCount
);
763 for (i
= 0; i
< ArrayEntryCount
; i
++)
765 DPRINT("USN: %u\tOffset: %u\n", fixupArray
->USN
, Offset
);
767 pShortToFixUp
= (USHORT
*)((PCHAR
)Record
+ Offset
);
768 fixupArray
->Array
[i
] = *pShortToFixUp
;
769 *pShortToFixUp
= fixupArray
->USN
;
770 Offset
+= Vcb
->NtfsInfo
.BytesPerSector
;
773 return STATUS_SUCCESS
;
777 ReadLCN(PDEVICE_EXTENSION Vcb
,
782 LARGE_INTEGER DiskSector
;
784 DiskSector
.QuadPart
= lcn
;
786 return NtfsReadSectors(Vcb
->StorageDevice
,
787 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
788 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
789 Vcb
->NtfsInfo
.BytesPerSector
,
796 CompareFileName(PUNICODE_STRING FileName
,
797 PINDEX_ENTRY_ATTRIBUTE IndexEntry
,
800 BOOLEAN Ret
, Alloc
= FALSE
;
801 UNICODE_STRING EntryName
;
803 EntryName
.Buffer
= IndexEntry
->FileName
.Name
;
805 EntryName
.MaximumLength
= IndexEntry
->FileName
.NameLength
* sizeof(WCHAR
);
809 UNICODE_STRING IntFileName
;
810 if (IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_POSIX
)
812 NT_VERIFY(NT_SUCCESS(RtlUpcaseUnicodeString(&IntFileName
, FileName
, TRUE
)));
817 IntFileName
= *FileName
;
820 Ret
= FsRtlIsNameInExpression(&IntFileName
, &EntryName
, (IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_POSIX
), NULL
);
824 RtlFreeUnicodeString(&IntFileName
);
831 return (RtlCompareUnicodeString(FileName
, &EntryName
, (IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_POSIX
)) == 0);
838 DumpIndexEntry(PINDEX_ENTRY_ATTRIBUTE IndexEntry
)
840 DPRINT1("Entry: %p\n", IndexEntry
);
841 DPRINT1("\tData.Directory.IndexedFile: %I64x\n", IndexEntry
->Data
.Directory
.IndexedFile
);
842 DPRINT1("\tLength: %u\n", IndexEntry
->Length
);
843 DPRINT1("\tKeyLength: %u\n", IndexEntry
->KeyLength
);
844 DPRINT1("\tFlags: %x\n", IndexEntry
->Flags
);
845 DPRINT1("\tReserved: %x\n", IndexEntry
->Reserved
);
846 DPRINT1("\t\tDirectoryFileReferenceNumber: %I64x\n", IndexEntry
->FileName
.DirectoryFileReferenceNumber
);
847 DPRINT1("\t\tCreationTime: %I64u\n", IndexEntry
->FileName
.CreationTime
);
848 DPRINT1("\t\tChangeTime: %I64u\n", IndexEntry
->FileName
.ChangeTime
);
849 DPRINT1("\t\tLastWriteTime: %I64u\n", IndexEntry
->FileName
.LastWriteTime
);
850 DPRINT1("\t\tLastAccessTime: %I64u\n", IndexEntry
->FileName
.LastAccessTime
);
851 DPRINT1("\t\tAllocatedSize: %I64u\n", IndexEntry
->FileName
.AllocatedSize
);
852 DPRINT1("\t\tDataSize: %I64u\n", IndexEntry
->FileName
.DataSize
);
853 DPRINT1("\t\tFileAttributes: %x\n", IndexEntry
->FileName
.FileAttributes
);
854 DPRINT1("\t\tNameLength: %u\n", IndexEntry
->FileName
.NameLength
);
855 DPRINT1("\t\tNameType: %x\n", IndexEntry
->FileName
.NameType
);
856 DPRINT1("\t\tName: %.*S\n", IndexEntry
->FileName
.NameLength
, IndexEntry
->FileName
.Name
);
861 BrowseIndexEntries(PDEVICE_EXTENSION Vcb
,
862 PFILE_RECORD_HEADER MftRecord
,
864 ULONG IndexBlockSize
,
865 PINDEX_ENTRY_ATTRIBUTE FirstEntry
,
866 PINDEX_ENTRY_ATTRIBUTE LastEntry
,
867 PUNICODE_STRING FileName
,
871 ULONGLONG
*OutMFTIndex
)
875 PINDEX_ENTRY_ATTRIBUTE IndexEntry
;
876 PNTFS_ATTR_CONTEXT IndexAllocationCtx
;
877 ULONGLONG IndexAllocationSize
;
878 PINDEX_BUFFER IndexBuffer
;
880 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
);
882 IndexEntry
= FirstEntry
;
883 while (IndexEntry
< LastEntry
&&
884 !(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_END
))
886 if ((IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
) > 0x10 &&
887 *CurrentEntry
>= *StartEntry
&&
888 IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_DOS
&&
889 CompareFileName(FileName
, IndexEntry
, DirSearch
))
891 *StartEntry
= *CurrentEntry
;
892 *OutMFTIndex
= (IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
);
893 return STATUS_SUCCESS
;
896 (*CurrentEntry
) += 1;
897 ASSERT(IndexEntry
->Length
>= sizeof(INDEX_ENTRY_ATTRIBUTE
));
898 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)IndexEntry
+ IndexEntry
->Length
);
901 /* If we're already browsing a subnode */
902 if (IndexRecord
== NULL
)
904 return STATUS_OBJECT_PATH_NOT_FOUND
;
907 /* If there's no subnode */
908 if (!(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_NODE
))
910 return STATUS_OBJECT_PATH_NOT_FOUND
;
913 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexAllocation
, L
"$I30", 4, &IndexAllocationCtx
, NULL
);
914 if (!NT_SUCCESS(Status
))
916 DPRINT("Corrupted filesystem!\n");
920 IndexAllocationSize
= AttributeDataLength(&IndexAllocationCtx
->Record
);
921 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
922 for (RecordOffset
= 0; RecordOffset
< IndexAllocationSize
; RecordOffset
+= IndexBlockSize
)
924 ReadAttribute(Vcb
, IndexAllocationCtx
, RecordOffset
, IndexRecord
, IndexBlockSize
);
925 Status
= FixupUpdateSequenceArray(Vcb
, &((PFILE_RECORD_HEADER
)IndexRecord
)->Ntfs
);
926 if (!NT_SUCCESS(Status
))
931 IndexBuffer
= (PINDEX_BUFFER
)IndexRecord
;
932 ASSERT(IndexBuffer
->Ntfs
.Type
== NRH_INDX_TYPE
);
933 ASSERT(IndexBuffer
->Header
.AllocatedSize
+ FIELD_OFFSET(INDEX_BUFFER
, Header
) == IndexBlockSize
);
934 FirstEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.FirstEntryOffset
);
935 LastEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.TotalSizeOfEntries
);
936 ASSERT(LastEntry
<= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)IndexBuffer
+ IndexBlockSize
));
938 Status
= BrowseIndexEntries(NULL
, NULL
, NULL
, 0, FirstEntry
, LastEntry
, FileName
, StartEntry
, CurrentEntry
, DirSearch
, OutMFTIndex
);
939 if (NT_SUCCESS(Status
))
945 ReleaseAttributeContext(IndexAllocationCtx
);
950 NtfsFindMftRecord(PDEVICE_EXTENSION Vcb
,
952 PUNICODE_STRING FileName
,
955 ULONGLONG
*OutMFTIndex
)
957 PFILE_RECORD_HEADER MftRecord
;
958 PNTFS_ATTR_CONTEXT IndexRootCtx
;
959 PINDEX_ROOT_ATTRIBUTE IndexRoot
;
961 PINDEX_ENTRY_ATTRIBUTE IndexEntry
, IndexEntryEnd
;
963 ULONG CurrentEntry
= 0;
965 DPRINT("NtfsFindMftRecord(%p, %I64d, %wZ, %u, %u, %p)\n", Vcb
, MFTIndex
, FileName
, *FirstEntry
, DirSearch
, OutMFTIndex
);
967 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
968 Vcb
->NtfsInfo
.BytesPerFileRecord
,
970 if (MftRecord
== NULL
)
972 return STATUS_INSUFFICIENT_RESOURCES
;
975 Status
= ReadFileRecord(Vcb
, MFTIndex
, MftRecord
);
976 if (!NT_SUCCESS(Status
))
978 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
982 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
983 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexRoot
, L
"$I30", 4, &IndexRootCtx
, NULL
);
984 if (!NT_SUCCESS(Status
))
986 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
990 IndexRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerIndexRecord
, TAG_NTFS
);
991 if (IndexRecord
== NULL
)
993 ReleaseAttributeContext(IndexRootCtx
);
994 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
995 return STATUS_INSUFFICIENT_RESOURCES
;
998 ReadAttribute(Vcb
, IndexRootCtx
, 0, IndexRecord
, Vcb
->NtfsInfo
.BytesPerIndexRecord
);
999 IndexRoot
= (PINDEX_ROOT_ATTRIBUTE
)IndexRecord
;
1000 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)&IndexRoot
->Header
+ IndexRoot
->Header
.FirstEntryOffset
);
1001 /* Index root is always resident. */
1002 IndexEntryEnd
= (PINDEX_ENTRY_ATTRIBUTE
)(IndexRecord
+ IndexRoot
->Header
.TotalSizeOfEntries
);
1003 ReleaseAttributeContext(IndexRootCtx
);
1005 DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb
->NtfsInfo
.BytesPerIndexRecord
, IndexRoot
->SizeOfEntry
);
1007 Status
= BrowseIndexEntries(Vcb
, MftRecord
, IndexRecord
, IndexRoot
->SizeOfEntry
, IndexEntry
, IndexEntryEnd
, FileName
, FirstEntry
, &CurrentEntry
, DirSearch
, OutMFTIndex
);
1009 ExFreePoolWithTag(IndexRecord
, TAG_NTFS
);
1010 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1016 NtfsLookupFileAt(PDEVICE_EXTENSION Vcb
,
1017 PUNICODE_STRING PathName
,
1018 PFILE_RECORD_HEADER
*FileRecord
,
1019 PULONGLONG MFTIndex
,
1020 ULONGLONG CurrentMFTIndex
)
1022 UNICODE_STRING Current
, Remaining
;
1024 ULONG FirstEntry
= 0;
1026 DPRINT("NtfsLookupFileAt(%p, %wZ, %p, %I64x)\n", Vcb
, PathName
, FileRecord
, CurrentMFTIndex
);
1028 FsRtlDissectName(*PathName
, &Current
, &Remaining
);
1030 while (Current
.Length
!= 0)
1032 DPRINT("Current: %wZ\n", &Current
);
1034 Status
= NtfsFindMftRecord(Vcb
, CurrentMFTIndex
, &Current
, &FirstEntry
, FALSE
, &CurrentMFTIndex
);
1035 if (!NT_SUCCESS(Status
))
1040 if (Remaining
.Length
== 0)
1043 FsRtlDissectName(Current
, &Current
, &Remaining
);
1046 *FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
1047 if (*FileRecord
== NULL
)
1049 DPRINT("NtfsLookupFileAt: Can't allocate MFT record\n");
1050 return STATUS_INSUFFICIENT_RESOURCES
;
1053 Status
= ReadFileRecord(Vcb
, CurrentMFTIndex
, *FileRecord
);
1054 if (!NT_SUCCESS(Status
))
1056 DPRINT("NtfsLookupFileAt: Can't read MFT record\n");
1057 ExFreePoolWithTag(*FileRecord
, TAG_NTFS
);
1061 *MFTIndex
= CurrentMFTIndex
;
1063 return STATUS_SUCCESS
;
1067 NtfsLookupFile(PDEVICE_EXTENSION Vcb
,
1068 PUNICODE_STRING PathName
,
1069 PFILE_RECORD_HEADER
*FileRecord
,
1070 PULONGLONG MFTIndex
)
1072 return NtfsLookupFileAt(Vcb
, PathName
, FileRecord
, MFTIndex
, NTFS_FILE_ROOT
);
1076 NtfsFindFileAt(PDEVICE_EXTENSION Vcb
,
1077 PUNICODE_STRING SearchPattern
,
1079 PFILE_RECORD_HEADER
*FileRecord
,
1080 PULONGLONG MFTIndex
,
1081 ULONGLONG CurrentMFTIndex
)
1085 DPRINT("NtfsFindFileAt(%p, %wZ, %u, %p, %p, %I64x)\n", Vcb
, SearchPattern
, *FirstEntry
, FileRecord
, MFTIndex
, CurrentMFTIndex
);
1087 Status
= NtfsFindMftRecord(Vcb
, CurrentMFTIndex
, SearchPattern
, FirstEntry
, TRUE
, &CurrentMFTIndex
);
1088 if (!NT_SUCCESS(Status
))
1090 DPRINT("NtfsFindFileAt: NtfsFindMftRecord() failed with status 0x%08lx\n", Status
);
1094 *FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
1095 if (*FileRecord
== NULL
)
1097 DPRINT("NtfsFindFileAt: Can't allocate MFT record\n");
1098 return STATUS_INSUFFICIENT_RESOURCES
;
1101 Status
= ReadFileRecord(Vcb
, CurrentMFTIndex
, *FileRecord
);
1102 if (!NT_SUCCESS(Status
))
1104 DPRINT("NtfsFindFileAt: Can't read MFT record\n");
1105 ExFreePoolWithTag(*FileRecord
, TAG_NTFS
);
1109 *MFTIndex
= CurrentMFTIndex
;
1111 return STATUS_SUCCESS
;