[NTFS]
[reactos.git] / reactos / drivers / filesystems / ntfs / mft.c
index 7e99ab2..f91e67b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ReactOS kernel
- *  Copyright (C) 2002 ReactOS Team
+ *  Copyright (C) 2002, 2014 ReactOS Team
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  * PROJECT:          ReactOS kernel
  * FILE:             drivers/filesystem/ntfs/mft.c
  * PURPOSE:          NTFS filesystem driver
- * PROGRAMMER:       Eric Kohl
- *                   Updated by Valentin Verkhovsky  2003/09/12
+ * PROGRAMMERS:      Eric Kohl
+ *                   Valentin Verkhovsky
+ *                   Pierre Schweitzer (pierre@reactos.org)
+ *                   HervĂ© Poussineau (hpoussin@reactos.org)
  */
 
 /* INCLUDES *****************************************************************/
@@ -31,8 +33,6 @@
 #define NDEBUG
 #include <debug.h>
 
-UNICODE_STRING IndexOfFileNames = RTL_CONSTANT_STRING(L"$I30");
-
 /* FUNCTIONS ****************************************************************/
 
 PNTFS_ATTR_CONTEXT
@@ -84,10 +84,10 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
                     PNTFS_ATTR_RECORD AttrRecord,
                     PNTFS_ATTR_RECORD AttrRecordEnd,
                     ULONG Type,
-                    const WCHAR *Name,
+                    PCWSTR Name,
                     ULONG NameLength)
 {
-    DPRINT("FindAttributeHelper(%p, %p, %p, 0x%x, %s, %u)\n", Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
+    DPRINT("FindAttributeHelper(%p, %p, %p, 0x%x, %S, %u)\n", Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
 
     while (AttrRecord < AttrRecordEnd)
     {
@@ -105,9 +105,6 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
             PNTFS_ATTR_RECORD ListAttrRecord;
             PNTFS_ATTR_RECORD ListAttrRecordEnd;
 
-            // Do not handle non-resident yet
-            ASSERT(!(AttrRecord->IsNonResident & 1));
-
             ListContext = PrepareAttributeContext(AttrRecord);
 
             ListSize = AttributeDataLength(&ListContext->Record);
@@ -135,7 +132,7 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
 
                 if (Context != NULL)
                 {
-                    DPRINT("Found context = %p\n", Context);
+                    if (AttrRecord->IsNonResident) DPRINT("Found context = %p\n", Context);
                     return Context;
                 }
             }
@@ -143,13 +140,12 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
 
         if (AttrRecord->Type == Type)
         {
-            DPRINT("%d, %d\n", AttrRecord->NameLength, NameLength);
             if (AttrRecord->NameLength == NameLength)
             {
                 PWCHAR AttrName;
 
                 AttrName = (PWCHAR)((PCHAR)AttrRecord + AttrRecord->NameOffset);
-                DPRINT("%s, %s\n", AttrName, Name);
+                DPRINT("%.*S, %.*S\n", AttrRecord->NameLength, AttrName, NameLength, Name);
                 if (RtlCompareMemory(AttrName, Name, NameLength << 1) == (NameLength << 1))
                 {
                     /* Found it, fill up the context and return. */
@@ -176,18 +172,19 @@ NTSTATUS
 FindAttribute(PDEVICE_EXTENSION Vcb,
               PFILE_RECORD_HEADER MftRecord,
               ULONG Type,
-              PUNICODE_STRING Name,
+              PCWSTR Name,
+              ULONG NameLength,
               PNTFS_ATTR_CONTEXT * AttrCtx)
 {
     PNTFS_ATTR_RECORD AttrRecord;
     PNTFS_ATTR_RECORD AttrRecordEnd;
 
-    DPRINT("NtfsFindAttribute(%p, %p, %u, %s)\n", Vcb, MftRecord, Type, Name);
+    DPRINT("FindAttribute(%p, %p, 0x%x, %S, %u, %p)\n", Vcb, MftRecord, Type, Name, NameLength, AttrCtx);
 
     AttrRecord = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + MftRecord->AttributeOffset);
     AttrRecordEnd = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + Vcb->NtfsInfo.BytesPerFileRecord);
 
-    *AttrCtx = FindAttributeHelper(Vcb, AttrRecord, AttrRecordEnd, Type, Name->Buffer, Name->Length);
+    *AttrCtx = FindAttributeHelper(Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
     if (*AttrCtx == NULL)
     {
         return STATUS_OBJECT_NAME_NOT_FOUND;
@@ -310,6 +307,7 @@ ReadAttribute(PDEVICE_EXTENSION Vcb,
     Status = NtfsReadDisk(Vcb->StorageDevice,
                           DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster + Offset - CurrentOffset,
                           ReadLength,
+                          Vcb->NtfsInfo.BytesPerSector,
                           (PVOID)Buffer,
                           FALSE);
     if (NT_SUCCESS(Status))
@@ -344,6 +342,7 @@ ReadAttribute(PDEVICE_EXTENSION Vcb,
                 Status = NtfsReadDisk(Vcb->StorageDevice,
                                       DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster,
                                       ReadLength,
+                                      Vcb->NtfsInfo.BytesPerSector,
                                       (PVOID)Buffer,
                                       FALSE);
                 if (!NT_SUCCESS(Status))
@@ -399,10 +398,12 @@ ReadFileRecord(PDEVICE_EXTENSION Vcb,
 {
     ULONGLONG BytesRead;
 
+    DPRINT("ReadFileRecord(%p, %I64x, %p)\n", Vcb, index, file);
+
     BytesRead = ReadAttribute(Vcb, Vcb->MFTContext, index * Vcb->NtfsInfo.BytesPerFileRecord, (PCHAR)file, Vcb->NtfsInfo.BytesPerFileRecord);
     if (BytesRead != Vcb->NtfsInfo.BytesPerFileRecord)
     {
-        DPRINT1("ReadFileRecord failed: %u read, %u expected\n", BytesRead, Vcb->NtfsInfo.BytesPerFileRecord);
+        DPRINT1("ReadFileRecord failed: %I64u read, %u expected\n", BytesRead, Vcb->NtfsInfo.BytesPerFileRecord);
         return STATUS_PARTIAL_COPY;
     }
 
@@ -462,20 +463,33 @@ ReadLCN(PDEVICE_EXTENSION Vcb,
 
 BOOLEAN
 CompareFileName(PUNICODE_STRING FileName,
-                PINDEX_ENTRY_ATTRIBUTE IndexEntry)
+                PINDEX_ENTRY_ATTRIBUTE IndexEntry,
+                BOOLEAN DirSearch)
 {
     UNICODE_STRING EntryName;
 
     EntryName.Buffer = IndexEntry->FileName.Name;
     EntryName.Length = 
-    EntryName.MaximumLength = IndexEntry->FileName.NameLength;
+    EntryName.MaximumLength = IndexEntry->FileName.NameLength * sizeof(WCHAR);
 
-    return (RtlCompareUnicodeString(FileName, &EntryName, !!(IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)) == TRUE);
+    if (DirSearch)
+    {
+        return FsRtlIsNameInExpression(FileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX), NULL);
+    }
+    else
+    {
+        return (RtlCompareUnicodeString(FileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)) == 0);
+    }
 }
 
 
 NTSTATUS
-NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING FileName, ULONGLONG *OutMFTIndex)
+NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
+                  ULONGLONG MFTIndex,
+                  PUNICODE_STRING FileName,
+                  PULONG FirstEntry,
+                  BOOLEAN DirSearch,
+                  ULONGLONG *OutMFTIndex)
 {
     PFILE_RECORD_HEADER MftRecord;
     //ULONG Magic;
@@ -483,6 +497,7 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
     PNTFS_ATTR_CONTEXT IndexBitmapCtx;
     PNTFS_ATTR_CONTEXT IndexAllocationCtx;
     PINDEX_ROOT_ATTRIBUTE IndexRoot;
+    PINDEX_BUFFER IndexBuffer;
     ULONGLONG BitmapDataSize;
     ULONGLONG IndexAllocationSize;
     PCHAR BitmapData;
@@ -491,6 +506,9 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
     ULONG RecordOffset;
     ULONG IndexBlockSize;
     NTSTATUS Status;
+    ULONG CurrentEntry = 0;
+
+    DPRINT("NtfsFindMftRecord(%p, %I64d, %wZ, %p, %u, %p)\n", Vcb, MFTIndex, FileName, FirstEntry, DirSearch, OutMFTIndex);
 
     MftRecord = ExAllocatePoolWithTag(NonPagedPool,
                                       Vcb->NtfsInfo.BytesPerFileRecord,
@@ -500,11 +518,11 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
-    if (ReadFileRecord(Vcb, MFTIndex, MftRecord))
+    if (NT_SUCCESS(ReadFileRecord(Vcb, MFTIndex, MftRecord)))
     {
         //Magic = MftRecord->Magic;
 
-        Status = FindAttribute(Vcb, MftRecord, AttributeIndexRoot, &IndexOfFileNames, &IndexRootCtx);
+        Status = FindAttribute(Vcb, MftRecord, AttributeIndexRoot, L"$I30", 4, &IndexRootCtx);
         if (!NT_SUCCESS(Status))
         {
             ExFreePoolWithTag(MftRecord, TAG_NTFS);
@@ -530,14 +548,19 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
         while (IndexEntry < IndexEntryEnd &&
                !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
         {
-            if (CompareFileName(FileName, IndexEntry))
+            if ((IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK) > 0x10 &&
+                CurrentEntry >= *FirstEntry &&
+                CompareFileName(FileName, IndexEntry, DirSearch))
             {
-                *OutMFTIndex = IndexEntry->Data.Directory.IndexedFile;
+                *OutMFTIndex = (IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK);
+                *FirstEntry = CurrentEntry;
                 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
                 ExFreePoolWithTag(MftRecord, TAG_NTFS);
                 return STATUS_SUCCESS;
             }
-        IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
+
+            ++CurrentEntry;
+            IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
         }
 
         if (IndexRoot->Header.Flags & INDEX_ROOT_LARGE)
@@ -546,10 +569,10 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
 
             IndexBlockSize = IndexRoot->SizeOfEntry;
 
-            Status = FindAttribute(Vcb, MftRecord, AttributeBitmap, &IndexOfFileNames, &IndexBitmapCtx);
+            Status = FindAttribute(Vcb, MftRecord, AttributeBitmap, L"$I30", 4, &IndexBitmapCtx);
             if (!NT_SUCCESS(Status))
             {
-                DPRINT("Corrupted filesystem!\n");
+                DPRINT1("Corrupted filesystem!\n");
                 ExFreePoolWithTag(MftRecord, TAG_NTFS);
                 return Status;
             }
@@ -569,7 +592,7 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
             ReadAttribute(Vcb, IndexBitmapCtx, 0, BitmapData, (ULONG)BitmapDataSize);
             ReleaseAttributeContext(IndexBitmapCtx);
 
-            Status = FindAttribute(Vcb, MftRecord, AttributeIndexAllocation, &IndexOfFileNames, &IndexAllocationCtx);
+            Status = FindAttribute(Vcb, MftRecord, AttributeIndexAllocation, L"$I30", 4, &IndexAllocationCtx);
             if (!NT_SUCCESS(Status))
             {
                 DPRINT("Corrupted filesystem!\n");
@@ -601,28 +624,37 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
 
                 ReadAttribute(Vcb, IndexAllocationCtx, RecordOffset, IndexRecord, IndexBlockSize);
 
-                if (!FixupUpdateSequenceArray(Vcb, &((PFILE_RECORD_HEADER)IndexRecord)->Ntfs))
+                if (!NT_SUCCESS(FixupUpdateSequenceArray(Vcb, &((PFILE_RECORD_HEADER)IndexRecord)->Ntfs)))
                 {
                     break;
                 }
 
-                /* FIXME */
-                IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + 0x18 + *(USHORT *)(IndexRecord + 0x18));
-                IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + IndexBlockSize);
+                IndexBuffer = (PINDEX_BUFFER)IndexRecord;
+                ASSERT(IndexBuffer->Ntfs.Type == 'XDNI');
+                ASSERT(IndexBuffer->Header.AllocatedSize + 0x18 == IndexBlockSize);
+                IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((ULONG_PTR)&IndexBuffer->Header + IndexBuffer->Header.FirstEntryOffset);
+                IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)((ULONG_PTR)&IndexBuffer->Header + IndexBuffer->Header.TotalSizeOfEntries);
+                ASSERT(IndexEntryEnd <= (PINDEX_ENTRY_ATTRIBUTE)((ULONG_PTR)IndexBuffer + IndexBlockSize));
 
                 while (IndexEntry < IndexEntryEnd &&
                        !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
                 {
-                    if (CompareFileName(FileName, IndexEntry))
+                    if ((IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK) > 0x10 &&
+                        CurrentEntry >= *FirstEntry &&
+                        CompareFileName(FileName, IndexEntry, DirSearch))
                     {
                         DPRINT("File found\n");
-                        *OutMFTIndex = IndexEntry->Data.Directory.IndexedFile;
+                        *OutMFTIndex = (IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK);
+                        *FirstEntry = CurrentEntry;
                         ExFreePoolWithTag(BitmapData, TAG_NTFS);
                         ExFreePoolWithTag(IndexRecord, TAG_NTFS);
                         ExFreePoolWithTag(MftRecord, TAG_NTFS);
                         ReleaseAttributeContext(IndexAllocationCtx);
                         return STATUS_SUCCESS;
                     }
+
+                    ++CurrentEntry;
+                    ASSERT(IndexEntry->Length >= sizeof(INDEX_ENTRY_ATTRIBUTE));
                     IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
                 }
 
@@ -645,54 +677,135 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING Fil
 }
 
 NTSTATUS
-NtfsLookupFile(PDEVICE_EXTENSION Vcb,
-               PUNICODE_STRING PathName,
-               PFILE_RECORD_HEADER *FileRecord,
-               PNTFS_ATTR_CONTEXT *DataContext)
+NtfsLookupFileAt(PDEVICE_EXTENSION Vcb,
+                 PUNICODE_STRING PathName,
+                 PFILE_RECORD_HEADER *FileRecord,
+                 PNTFS_ATTR_CONTEXT *DataContext,
+                 PULONGLONG MFTIndex,
+                 ULONGLONG CurrentMFTIndex)
 {
-    ULONGLONG CurrentMFTIndex;
     UNICODE_STRING Current, Remaining;
     NTSTATUS Status;
+    ULONG FirstEntry = 0;
 
-    DPRINT1("NtfsLookupFile(%p, %wZ, %p)\n", Vcb, PathName, FileRecord);
+    DPRINT("NtfsLookupFileAt(%p, %wZ, %p, %p, %I64x)\n", Vcb, PathName, FileRecord, DataContext, CurrentMFTIndex);
 
-    CurrentMFTIndex = NTFS_FILE_ROOT;
     FsRtlDissectName(*PathName, &Current, &Remaining);
 
     while (Current.Length != 0)
     {
-        DPRINT1("Lookup: %wZ\n", &Current);
+        DPRINT("Current: %wZ\n", &Current);
 
-        Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, &Current, &CurrentMFTIndex);
+        Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, &Current, &FirstEntry, FALSE, &CurrentMFTIndex);
         if (!NT_SUCCESS(Status))
         {
             return Status;
         }
 
-        FsRtlDissectName(*PathName, &Current, &Remaining);
+        if (Remaining.Length == 0)
+            break;
+
+        FsRtlDissectName(Current, &Current, &Remaining);
     }
 
     *FileRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
     if (*FileRecord == NULL)
     {
-        DPRINT("NtfsLookupFile: Can't allocate MFT record\n");
+        DPRINT("NtfsLookupFileAt: Can't allocate MFT record\n");
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
     Status = ReadFileRecord(Vcb, CurrentMFTIndex, *FileRecord);
     if (!NT_SUCCESS(Status))
     {
-        DPRINT("NtfsLookupFile: Can't read MFT record\n");
+        DPRINT("NtfsLookupFileAt: Can't read MFT record\n");
+        ExFreePoolWithTag(*FileRecord, TAG_NTFS);
+        return Status;
+    }
+
+    if (!((*FileRecord)->Flags & FRH_DIRECTORY))
+    {
+        Status = FindAttribute(Vcb, *FileRecord, AttributeData, L"", 0, DataContext);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT("NtfsLookupFileAt: Can't find data attribute\n");
+            ExFreePoolWithTag(*FileRecord, TAG_NTFS);
+            return Status;
+        }
+    }
+    else
+    {
+        *DataContext = NULL;
+    }
+
+    *MFTIndex = CurrentMFTIndex;
+
+    return STATUS_SUCCESS;
+}
+
+NTSTATUS
+NtfsLookupFile(PDEVICE_EXTENSION Vcb,
+               PUNICODE_STRING PathName,
+               PFILE_RECORD_HEADER *FileRecord,
+               PNTFS_ATTR_CONTEXT *DataContext,
+               PULONGLONG MFTIndex)
+{
+    return NtfsLookupFileAt(Vcb, PathName, FileRecord, DataContext, MFTIndex, NTFS_FILE_ROOT);
+}
+
+NTSTATUS
+NtfsFindFileAt(PDEVICE_EXTENSION Vcb,
+               PUNICODE_STRING SearchPattern,
+               PULONG FirstEntry,
+               PFILE_RECORD_HEADER *FileRecord,
+               PNTFS_ATTR_CONTEXT *DataContext,
+               PULONGLONG MFTIndex,
+               ULONGLONG CurrentMFTIndex)
+{
+    NTSTATUS Status;
+
+    DPRINT("NtfsFindFileAt(%p, %wZ, %p, %p, %p, %p, %I64x)\n", Vcb, SearchPattern, FirstEntry, FileRecord, DataContext, MFTIndex, CurrentMFTIndex);
+
+    Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, SearchPattern, FirstEntry, TRUE, &CurrentMFTIndex);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT("NtfsFindFileAt: NtfsFindMftRecord() failed with status 0x%08lx\n", Status);
         return Status;
     }
 
-    Status = FindAttribute(Vcb, *FileRecord, AttributeData, PathName, DataContext);
+    *FileRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
+    if (*FileRecord == NULL)
+    {
+        DPRINT("NtfsFindFileAt: Can't allocate MFT record\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    Status = ReadFileRecord(Vcb, CurrentMFTIndex, *FileRecord);
     if (!NT_SUCCESS(Status))
     {
-        DPRINT("NtfsLookupFile: Can't find data attribute\n");
+        DPRINT("NtfsFindFileAt: Can't read MFT record\n");
+        ExFreePoolWithTag(*FileRecord, TAG_NTFS);
         return Status;
     }
 
+    if (!((*FileRecord)->Flags & FRH_DIRECTORY))
+    {
+        Status = FindAttribute(Vcb, *FileRecord, AttributeData, L"", 0, DataContext);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT("NtfsFindFileAt: Can't find data attribute\n");
+            ExFreePoolWithTag(*FileRecord, TAG_NTFS);
+            return Status;
+        }
+    }
+    else
+    {
+        *DataContext = NULL;
+    }
+
+    *MFTIndex = CurrentMFTIndex;
+
     return STATUS_SUCCESS;
 }
+
 /* EOF */