[NTFS]
[reactos.git] / reactos / drivers / filesystems / ntfs / mft.c
index d40d919..3a3f00e 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 *****************************************************************/
 
 /* FUNCTIONS ****************************************************************/
 
-NTSTATUS
-NtfsOpenMft(PDEVICE_EXTENSION Vcb)
+PNTFS_ATTR_CONTEXT
+PrepareAttributeContext(PNTFS_ATTR_RECORD AttrRecord)
 {
-//    PVOID Bitmap;
-    PFILE_RECORD_HEADER MftRecord;
-    PFILE_RECORD_HEADER FileRecord;
-//    PATTRIBUTE Attribute;
-//    PATTRIBUTE AttrData;
-//    PRESIDENT_ATTRIBUTE ResAttr;
-
-    NTSTATUS Status;
-    ULONG BytesPerFileRecord;
-    ULONG n;
-    ULONG i;
+    PNTFS_ATTR_CONTEXT Context;
 
-    DPRINT1("NtfsOpenMft() called\n");
-
-    BytesPerFileRecord = Vcb->NtfsInfo.BytesPerFileRecord;
-
-    MftRecord = ExAllocatePoolWithTag(NonPagedPool,
-                                      BytesPerFileRecord,
-                                      TAG_NTFS);
-    if (MftRecord == NULL)
+    Context = ExAllocatePoolWithTag(NonPagedPool,
+                                    FIELD_OFFSET(NTFS_ATTR_CONTEXT, Record) + AttrRecord->Length,
+                                    TAG_NTFS);
+    RtlCopyMemory(&Context->Record, AttrRecord, AttrRecord->Length);
+    if (AttrRecord->IsNonResident)
     {
-        return STATUS_INSUFFICIENT_RESOURCES;
+        LONGLONG DataRunOffset;
+        ULONGLONG DataRunLength;
+
+        Context->CacheRun = (PUCHAR)&Context->Record + Context->Record.NonResident.MappingPairsOffset;
+        Context->CacheRunOffset = 0;
+        Context->CacheRun = DecodeRun(Context->CacheRun, &DataRunOffset, &DataRunLength);
+        Context->CacheRunLength = DataRunLength;
+        if (DataRunOffset != -1)
+        {
+            /* Normal run. */
+            Context->CacheRunStartLCN =
+            Context->CacheRunLastLCN = DataRunOffset;
+        }
+        else
+        {
+            /* Sparse run. */
+            Context->CacheRunStartLCN = -1;
+            Context->CacheRunLastLCN = 0;
+        }
+        Context->CacheRunCurrentOffset = 0;
     }
 
-    Status = NtfsReadSectors(Vcb->StorageDevice,
-                             Vcb->NtfsInfo.MftStart.u.LowPart * Vcb->NtfsInfo.SectorsPerCluster,
-                             BytesPerFileRecord / Vcb->NtfsInfo.BytesPerSector,
-                             Vcb->NtfsInfo.BytesPerSector,
-                             (PVOID)MftRecord,
-                             FALSE);
-    if (!NT_SUCCESS(Status))
-    {
-        ExFreePool(MftRecord);
-        return Status;
-    }
+    return Context;
+}
 
-    FixupUpdateSequenceArray(MftRecord);
 
-//    Attribute = FindAttribute(MftRecord, AttributeBitmap, 0);
+VOID
+ReleaseAttributeContext(PNTFS_ATTR_CONTEXT Context)
+{
+    ExFreePoolWithTag(Context, TAG_NTFS);
+}
 
-    /* Get number of file records*/
-    n = AttributeDataLength(FindAttribute(MftRecord, AttributeData, 0)) / BytesPerFileRecord;
 
-    FileRecord = ExAllocatePoolWithTag(NonPagedPool,
-                                       BytesPerFileRecord,
-                                       TAG_NTFS);
-    if (FileRecord == NULL)
-    {
-        ExFreePool(MftRecord);
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
+PNTFS_ATTR_CONTEXT
+FindAttributeHelper(PDEVICE_EXTENSION Vcb,
+                    PNTFS_ATTR_RECORD AttrRecord,
+                    PNTFS_ATTR_RECORD AttrRecordEnd,
+                    ULONG Type,
+                    PCWSTR Name,
+                    ULONG NameLength)
+{
+    DPRINT("FindAttributeHelper(%p, %p, %p, 0x%x, %S, %u)\n", Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
 
-    /* Enumerate MFT Records */
-    DPRINT("Enumerate MFT records\n");
-    for (i = 0; i < n; i++)
+    while (AttrRecord < AttrRecordEnd)
     {
-        ReadFileRecord(Vcb,
-                       i,
-                       FileRecord,
-                       MftRecord);
+        DPRINT("AttrRecord->Type = 0x%x\n", AttrRecord->Type);
 
-        if (FileRecord->Ntfs.Type == NRH_FILE_TYPE &&
-            (FileRecord->Flags & FRH_IN_USE))
+        if (AttrRecord->Type == AttributeEnd)
+            break;
+
+        if (AttrRecord->Type == AttributeAttributeList)
         {
-            DPRINT("\nFile  %lu\n\n", i);
+            PNTFS_ATTR_CONTEXT Context;
+            PNTFS_ATTR_CONTEXT ListContext;
+            PVOID ListBuffer;
+            ULONGLONG ListSize;
+            PNTFS_ATTR_RECORD ListAttrRecord;
+            PNTFS_ATTR_RECORD ListAttrRecordEnd;
+
+            ListContext = PrepareAttributeContext(AttrRecord);
+
+            ListSize = AttributeDataLength(&ListContext->Record);
+            if(ListSize <= 0xFFFFFFFF)
+                ListBuffer = ExAllocatePoolWithTag(NonPagedPool, (ULONG)ListSize, TAG_NTFS);
+            else
+                ListBuffer = NULL;
+
+            if(!ListBuffer)
+            {
+                DPRINT("Failed to allocate memory: %x\n", (ULONG)ListSize);
+                continue;
+            }
+
+            ListAttrRecord = (PNTFS_ATTR_RECORD)ListBuffer;
+            ListAttrRecordEnd = (PNTFS_ATTR_RECORD)((PCHAR)ListBuffer + ListSize);
+
+            if (ReadAttribute(Vcb, ListContext, 0, ListBuffer, (ULONG)ListSize) == ListSize)
+            {
+                Context = FindAttributeHelper(Vcb, ListAttrRecord, ListAttrRecordEnd,
+                                              Type, Name, NameLength);
+
+                ReleaseAttributeContext(ListContext);
+                ExFreePoolWithTag(ListBuffer, TAG_NTFS);
+
+                if (Context != NULL)
+                {
+                    if (AttrRecord->IsNonResident) DPRINT("Found context = %p\n", Context);
+                    return Context;
+                }
+            }
+        }
 
-            /* Enumerate attributtes */
-            NtfsDumpFileAttributes (FileRecord);
-            DbgPrint("\n\n");
+        if (AttrRecord->Type == Type)
+        {
+            if (AttrRecord->NameLength == NameLength)
+            {
+                PWCHAR AttrName;
+
+                AttrName = (PWCHAR)((PCHAR)AttrRecord + AttrRecord->NameOffset);
+                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. */
+                    DPRINT("Found context\n");
+                    return PrepareAttributeContext(AttrRecord);
+                }
+            }
         }
-    }
 
-    ExFreePool(FileRecord);
-    ExFreePool(MftRecord);
+        if (AttrRecord->Length == 0)
+        {
+            DPRINT("Null length attribute record\n");
+            return NULL;
+        }
+        AttrRecord = (PNTFS_ATTR_RECORD)((PCHAR)AttrRecord + AttrRecord->Length);
+    }
 
-    return Status;
+    DPRINT("Ended\n");
+    return NULL;
 }
 
 
-PATTRIBUTE
-FindAttribute(PFILE_RECORD_HEADER FileRecord,
-              ATTRIBUTE_TYPE Type,
-              PWSTR name)
+NTSTATUS
+FindAttribute(PDEVICE_EXTENSION Vcb,
+              PFILE_RECORD_HEADER MftRecord,
+              ULONG Type,
+              PCWSTR Name,
+              ULONG NameLength,
+              PNTFS_ATTR_CONTEXT * AttrCtx)
 {
-    PATTRIBUTE Attribute;
+    PNTFS_ATTR_RECORD AttrRecord;
+    PNTFS_ATTR_RECORD AttrRecordEnd;
 
-    UNREFERENCED_PARAMETER(name);
+    DPRINT("FindAttribute(%p, %p, 0x%x, %S, %u, %p)\n", Vcb, MftRecord, Type, Name, NameLength, AttrCtx);
 
-    Attribute = (PATTRIBUTE)((ULONG_PTR)FileRecord + FileRecord->AttributeOffset);
-    while (Attribute < (PATTRIBUTE)((ULONG_PTR)FileRecord + FileRecord->BytesInUse) &&
-           Attribute->AttributeType != (ATTRIBUTE_TYPE)-1)
-    {
-        if (Attribute->AttributeType == Type)
-        {
-            return Attribute;
-        }
+    AttrRecord = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + MftRecord->AttributeOffset);
+    AttrRecordEnd = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + Vcb->NtfsInfo.BytesPerFileRecord);
 
-        Attribute = (PATTRIBUTE)((ULONG_PTR)Attribute + Attribute->Length);
+    *AttrCtx = FindAttributeHelper(Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
+    if (*AttrCtx == NULL)
+    {
+        return STATUS_OBJECT_NAME_NOT_FOUND;
     }
 
-    return NULL;
+    return STATUS_SUCCESS;
 }
 
 
 ULONG
-AttributeAllocatedLength(PATTRIBUTE Attribute)
+AttributeAllocatedLength(PNTFS_ATTR_RECORD AttrRecord)
 {
-    if (Attribute->Nonresident)
-    {
-        return ((PNONRESIDENT_ATTRIBUTE)Attribute)->AllocatedSize;
-    }
+    if (AttrRecord->IsNonResident)
+        return AttrRecord->NonResident.AllocatedSize;
+    else
+        return AttrRecord->Resident.ValueLength;
+}
+
 
-    return ((PRESIDENT_ATTRIBUTE)Attribute)->ValueLength;
+ULONGLONG
+AttributeDataLength(PNTFS_ATTR_RECORD AttrRecord)
+{
+    if (AttrRecord->IsNonResident)
+        return AttrRecord->NonResident.DataSize;
+    else
+        return AttrRecord->Resident.ValueLength;
 }
 
 
 ULONG
-AttributeDataLength(PATTRIBUTE Attribute)
+ReadAttribute(PDEVICE_EXTENSION Vcb,
+              PNTFS_ATTR_CONTEXT Context,
+              ULONGLONG Offset,
+              PCHAR Buffer,
+              ULONG Length)
 {
-    if (Attribute->Nonresident)
+    ULONGLONG LastLCN;
+    PUCHAR DataRun;
+    LONGLONG DataRunOffset;
+    ULONGLONG DataRunLength;
+    LONGLONG DataRunStartLCN;
+    ULONGLONG CurrentOffset;
+    ULONG ReadLength;
+    ULONG AlreadyRead;
+    NTSTATUS Status;
+
+    if (!Context->Record.IsNonResident)
     {
-        return ((PNONRESIDENT_ATTRIBUTE)Attribute)->DataSize;
+        if (Offset > Context->Record.Resident.ValueLength)
+            return 0;
+        if (Offset + Length > Context->Record.Resident.ValueLength)
+            Length = (ULONG)(Context->Record.Resident.ValueLength - Offset);
+        RtlCopyMemory(Buffer, (PCHAR)&Context->Record + Context->Record.Resident.ValueOffset + Offset, Length);
+        return Length;
     }
 
-    return ((PRESIDENT_ATTRIBUTE)Attribute)->ValueLength;
-}
-
+    /*
+     * Non-resident attribute
+     */
 
-VOID
-ReadAttribute(PATTRIBUTE attr,
-              PVOID buffer,
-              PDEVICE_EXTENSION Vcb,
-              PDEVICE_OBJECT DeviceObject)
-{
-    PNONRESIDENT_ATTRIBUTE NresAttr = (PNONRESIDENT_ATTRIBUTE)attr;
+    /*
+     * I. Find the corresponding start data run.
+     */
 
-    UNREFERENCED_PARAMETER(DeviceObject);
+    AlreadyRead = 0;
 
-    if (attr->Nonresident == FALSE)
+    // FIXME: Cache seems to be non-working. Disable it for now
+    //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
+    if (0)
     {
-        memcpy(buffer,
-               (PVOID)((ULONG_PTR)attr + ((PRESIDENT_ATTRIBUTE)attr)->ValueOffset),
-               ((PRESIDENT_ATTRIBUTE)attr)->ValueLength);
+        DataRun = Context->CacheRun;
+        LastLCN = Context->CacheRunLastLCN;
+        DataRunStartLCN = Context->CacheRunStartLCN;
+        DataRunLength = Context->CacheRunLength;
+        CurrentOffset = Context->CacheRunCurrentOffset;
     }
+    else
+    {
+        LastLCN = 0;
+        DataRun = (PUCHAR)&Context->Record + Context->Record.NonResident.MappingPairsOffset;
+        CurrentOffset = 0;
+
+        while (1)
+        {
+            DataRun = DecodeRun(DataRun, &DataRunOffset, &DataRunLength);
+            if (DataRunOffset != -1)
+            {
+                /* Normal data run. */
+                DataRunStartLCN = LastLCN + DataRunOffset;
+                LastLCN = DataRunStartLCN;
+            }
+            else
+            {
+                /* Sparse data run. */
+                DataRunStartLCN = -1;
+            }
+
+            if (Offset >= CurrentOffset &&
+                Offset < CurrentOffset + (DataRunLength * Vcb->NtfsInfo.BytesPerCluster))
+            {
+                break;
+            }
+
+            if (*DataRun == 0)
+            {
+                return AlreadyRead;
+            }
+
+            CurrentOffset += DataRunLength * Vcb->NtfsInfo.BytesPerCluster;
+        }
+    }
+
+    /*
+     * II. Go through the run list and read the data
+     */
+
+    ReadLength = (ULONG)min(DataRunLength * Vcb->NtfsInfo.BytesPerCluster - (Offset - CurrentOffset), Length);
+    if (DataRunStartLCN == -1)
+        RtlZeroMemory(Buffer, ReadLength);
+    Status = NtfsReadDisk(Vcb->StorageDevice,
+                          DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster + Offset - CurrentOffset,
+                          ReadLength,
+                          Vcb->NtfsInfo.BytesPerSector,
+                          (PVOID)Buffer,
+                          FALSE);
+    if (NT_SUCCESS(Status))
+    {
+        Length -= ReadLength;
+        Buffer += ReadLength;
+        AlreadyRead += ReadLength;
+
+        if (ReadLength == DataRunLength * Vcb->NtfsInfo.BytesPerCluster - (Offset - CurrentOffset))
+        {
+            CurrentOffset += DataRunLength * Vcb->NtfsInfo.BytesPerCluster;
+            DataRun = DecodeRun(DataRun, &DataRunOffset, &DataRunLength);
+            if (DataRunLength != (ULONGLONG)-1)
+            {
+                DataRunStartLCN = LastLCN + DataRunOffset;
+                LastLCN = DataRunStartLCN;
+            }
+            else
+                DataRunStartLCN = -1;
+
+            if (*DataRun == 0)
+                return AlreadyRead;
+        }
 
-    ReadExternalAttribute(Vcb,
-                          NresAttr,
-                          0,
-                          (ULONG)(NresAttr->LastVcn) + 1,
-                          buffer);
+        while (Length > 0)
+        {
+            ReadLength = (ULONG)min(DataRunLength * Vcb->NtfsInfo.BytesPerCluster, Length);
+            if (DataRunStartLCN == -1)
+                RtlZeroMemory(Buffer, ReadLength);
+            else
+            {
+                Status = NtfsReadDisk(Vcb->StorageDevice,
+                                      DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster,
+                                      ReadLength,
+                                      Vcb->NtfsInfo.BytesPerSector,
+                                      (PVOID)Buffer,
+                                      FALSE);
+                if (!NT_SUCCESS(Status))
+                    break;
+            }
+
+            Length -= ReadLength;
+            Buffer += ReadLength;
+            AlreadyRead += ReadLength;
+
+            /* We finished this request, but there still data in this data run. */
+            if (Length == 0 && ReadLength != DataRunLength * Vcb->NtfsInfo.BytesPerCluster)
+                break;
+
+            /*
+             * Go to next run in the list.
+             */
+
+            if (*DataRun == 0)
+                break;
+            CurrentOffset += DataRunLength * Vcb->NtfsInfo.BytesPerCluster;
+            DataRun = DecodeRun(DataRun, &DataRunOffset, &DataRunLength);
+            if (DataRunOffset != -1)
+            {
+                /* Normal data run. */
+                DataRunStartLCN = LastLCN + DataRunOffset;
+                LastLCN = DataRunStartLCN;
+            }
+            else
+            {
+                /* Sparse data run. */
+                DataRunStartLCN = -1;
+            }
+        } /* while */
+
+    } /* if Disk */
+
+    Context->CacheRun = DataRun;
+    Context->CacheRunOffset = Offset + AlreadyRead;
+    Context->CacheRunStartLCN = DataRunStartLCN;
+    Context->CacheRunLength = DataRunLength;
+    Context->CacheRunLastLCN = LastLCN;
+    Context->CacheRunCurrentOffset = CurrentOffset;
+
+    return AlreadyRead;
 }
 
 
 NTSTATUS
 ReadFileRecord(PDEVICE_EXTENSION Vcb,
-               ULONG index,
-               PFILE_RECORD_HEADER file,
-               PFILE_RECORD_HEADER Mft)
+               ULONGLONG index,
+               PFILE_RECORD_HEADER file)
 {
-    PVOID p;
-    ULONG BytesPerFileRecord = Vcb->NtfsInfo.BytesPerFileRecord;
-    ULONG clusters = max(BytesPerFileRecord / Vcb->NtfsInfo.BytesPerCluster, 1);
-    ULONGLONG vcn = index * BytesPerFileRecord / Vcb->NtfsInfo.BytesPerCluster;
-    LONG m = (Vcb->NtfsInfo.BytesPerCluster / BytesPerFileRecord) - 1;
-    ULONG n = m > 0 ? (index & m) : 0;
+    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: %I64u read, %u expected\n", BytesRead, Vcb->NtfsInfo.BytesPerFileRecord);
+        return STATUS_PARTIAL_COPY;
+    }
 
-    p = ExAllocatePoolWithTag(NonPagedPool,
-                              clusters * Vcb->NtfsInfo.BytesPerCluster,
-                              TAG_NTFS);
+    /* Apply update sequence array fixups. */
+    return FixupUpdateSequenceArray(Vcb, &file->Ntfs);
+}
 
-    ReadVCN (Vcb, Mft, AttributeData, vcn, clusters, p);
 
-    memcpy(file,
-           (PVOID)((ULONG_PTR)p + n * BytesPerFileRecord),
-           BytesPerFileRecord);
+NTSTATUS 
+FixupUpdateSequenceArray(PDEVICE_EXTENSION Vcb,
+                         PNTFS_RECORD_HEADER Record)
+{
+    USHORT *USA;
+    USHORT USANumber;
+    USHORT USACount;
+    USHORT *Block;
 
-    ExFreePool(p);
+    USA = (USHORT*)((PCHAR)Record + Record->UsaOffset);
+    USANumber = *(USA++);
+    USACount = Record->UsaCount - 1; /* Exclude the USA Number. */
+    Block = (USHORT*)((PCHAR)Record + Vcb->NtfsInfo.BytesPerSector - 2);
 
-    FixupUpdateSequenceArray(file);
+    while (USACount)
+    {
+        if (*Block != USANumber)
+        {
+            DPRINT1("Mismatch with USA: %u read, %u expected\n" , *Block, USANumber);
+            return STATUS_UNSUCCESSFUL;
+        }
+        *Block = *(USA++);
+        Block = (USHORT*)((PCHAR)Block + Vcb->NtfsInfo.BytesPerSector);
+        USACount--;
+    }
 
     return STATUS_SUCCESS;
 }
 
 
-VOID
-ReadExternalAttribute(PDEVICE_EXTENSION Vcb,
-                      PNONRESIDENT_ATTRIBUTE NresAttr,
-                      ULONGLONG vcn,
-                      ULONG count,
-                      PVOID buffer)
+NTSTATUS
+ReadLCN(PDEVICE_EXTENSION Vcb,
+        ULONGLONG lcn,
+        ULONG count,
+        PVOID buffer)
 {
-    ULONGLONG lcn;
-    ULONGLONG runcount;
-    ULONG readcount;
-    ULONG left;
-    ULONG n;
+    LARGE_INTEGER DiskSector;
 
-    PUCHAR bytes = (PUCHAR)buffer;
+    DiskSector.QuadPart = lcn;
 
-    for (left = count; left > 0; left -= readcount)
-    {
-        FindRun(NresAttr, vcn, &lcn, &runcount);
+    return NtfsReadSectors(Vcb->StorageDevice,
+                           DiskSector.u.LowPart * Vcb->NtfsInfo.SectorsPerCluster,
+                           count * Vcb->NtfsInfo.SectorsPerCluster,
+                           Vcb->NtfsInfo.BytesPerSector,
+                           buffer,
+                           FALSE);
+}
 
-//        readcount = (ULONG)(__min(runcount, left));
-        readcount = (ULONG)min(runcount, left);
 
+BOOLEAN
+CompareFileName(PUNICODE_STRING FileName,
+                PINDEX_ENTRY_ATTRIBUTE IndexEntry,
+                BOOLEAN DirSearch)
+{
+    BOOLEAN Ret, Alloc = FALSE;
+    UNICODE_STRING EntryName;
 
-        n = readcount * Vcb->NtfsInfo.BytesPerCluster;
+    EntryName.Buffer = IndexEntry->FileName.Name;
+    EntryName.Length = 
+    EntryName.MaximumLength = IndexEntry->FileName.NameLength * sizeof(WCHAR);
 
-        if (lcn == 0)
-            memset(bytes, 0, n);
+    if (DirSearch)
+    {
+        UNICODE_STRING IntFileName;
+        if (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)
+        {
+            ASSERT(NT_SUCCESS(RtlUpcaseUnicodeString(&IntFileName, FileName, TRUE)));
+            Alloc = TRUE;
+        }
         else
-            ReadLCN(Vcb, lcn, readcount, bytes);
+        {
+            IntFileName = *FileName;
+        }
+
+        Ret = FsRtlIsNameInExpression(&IntFileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX), NULL);
+
+        if (Alloc)
+        {
+            RtlFreeUnicodeString(&IntFileName);
+        }
 
-        vcn += readcount;
-        bytes += n;
+        return Ret;
+    }
+    else
+    {
+        return (RtlCompareUnicodeString(FileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)) == 0);
     }
 }
 
 
-VOID
-ReadVCN(PDEVICE_EXTENSION Vcb,
-        PFILE_RECORD_HEADER file,
-        ATTRIBUTE_TYPE type,
-        ULONGLONG vcn,
-        ULONG count,
-        PVOID buffer)
+NTSTATUS
+NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
+                  ULONGLONG MFTIndex,
+                  PUNICODE_STRING FileName,
+                  PULONG FirstEntry,
+                  BOOLEAN DirSearch,
+                  ULONGLONG *OutMFTIndex)
 {
-    PNONRESIDENT_ATTRIBUTE NresAttr;
-    PATTRIBUTE attr;
+    PFILE_RECORD_HEADER MftRecord;
+    PNTFS_ATTR_CONTEXT IndexRootCtx;
+    PNTFS_ATTR_CONTEXT IndexBitmapCtx;
+    PNTFS_ATTR_CONTEXT IndexAllocationCtx;
+    PINDEX_ROOT_ATTRIBUTE IndexRoot;
+    PINDEX_BUFFER IndexBuffer;
+    ULONGLONG BitmapDataSize;
+    ULONGLONG IndexAllocationSize;
+    PCHAR BitmapData;
+    PCHAR IndexRecord;
+    PINDEX_ENTRY_ATTRIBUTE IndexEntry, IndexEntryEnd;
+    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,
+                                      TAG_NTFS);
+    if (MftRecord == NULL)
+    {
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    if (NT_SUCCESS(ReadFileRecord(Vcb, MFTIndex, MftRecord)))
+    {
+        ASSERT(MftRecord->Ntfs.Type == NRH_FILE_TYPE);
+
+        Status = FindAttribute(Vcb, MftRecord, AttributeIndexRoot, L"$I30", 4, &IndexRootCtx);
+        if (!NT_SUCCESS(Status))
+        {
+            ExFreePoolWithTag(MftRecord, TAG_NTFS);
+            return Status;
+        }
+
+        IndexRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerIndexRecord, TAG_NTFS);
+        if (IndexRecord == NULL)
+        {
+            ExFreePoolWithTag(MftRecord, TAG_NTFS);
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+
+        ReadAttribute(Vcb, IndexRootCtx, 0, IndexRecord, Vcb->NtfsInfo.BytesPerIndexRecord);
+        IndexRoot = (PINDEX_ROOT_ATTRIBUTE)IndexRecord;
+        IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)&IndexRoot->Header + IndexRoot->Header.FirstEntryOffset);
+        /* Index root is always resident. */
+        IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + IndexRootCtx->Record.Resident.ValueLength);
+        ReleaseAttributeContext(IndexRootCtx);
 
-    attr = FindAttribute(file, type, 0);
+        DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb->NtfsInfo.BytesPerIndexRecord, IndexRoot->SizeOfEntry);
 
-    NresAttr = (PNONRESIDENT_ATTRIBUTE) attr;
+        while (IndexEntry < IndexEntryEnd &&
+               !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
+        {
+            if ((IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK) > 0x10 &&
+                CurrentEntry >= *FirstEntry &&
+                CompareFileName(FileName, IndexEntry, DirSearch))
+            {
+                *OutMFTIndex = (IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK);
+                *FirstEntry = CurrentEntry;
+                ExFreePoolWithTag(IndexRecord, TAG_NTFS);
+                ExFreePoolWithTag(MftRecord, TAG_NTFS);
+                return STATUS_SUCCESS;
+            }
+
+            ++CurrentEntry;
+            IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
+        }
 
-    if (NresAttr == 0 || (vcn < NresAttr->StartVcn ||vcn > NresAttr->LastVcn))
+        if (IndexRoot->Header.Flags & INDEX_ROOT_LARGE)
+        {
+            DPRINT("Large Index!\n");
+
+            IndexBlockSize = IndexRoot->SizeOfEntry;
+
+            Status = FindAttribute(Vcb, MftRecord, AttributeBitmap, L"$I30", 4, &IndexBitmapCtx);
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("Corrupted filesystem!\n");
+                ExFreePoolWithTag(MftRecord, TAG_NTFS);
+                return Status;
+            }
+            BitmapDataSize = AttributeDataLength(&IndexBitmapCtx->Record);
+            DPRINT("BitmapDataSize: %x\n", (ULONG)BitmapDataSize);
+            if(BitmapDataSize <= 0xFFFFFFFF)
+                BitmapData = ExAllocatePoolWithTag(NonPagedPool, (ULONG)BitmapDataSize, TAG_NTFS);
+            else
+                BitmapData = NULL;
+
+            if (BitmapData == NULL)
+            {
+                ExFreePoolWithTag(IndexRecord, TAG_NTFS);
+                ExFreePoolWithTag(MftRecord, TAG_NTFS);
+                return STATUS_INSUFFICIENT_RESOURCES;
+            }
+            ReadAttribute(Vcb, IndexBitmapCtx, 0, BitmapData, (ULONG)BitmapDataSize);
+            ReleaseAttributeContext(IndexBitmapCtx);
+
+            Status = FindAttribute(Vcb, MftRecord, AttributeIndexAllocation, L"$I30", 4, &IndexAllocationCtx);
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT("Corrupted filesystem!\n");
+                ExFreePoolWithTag(BitmapData, TAG_NTFS);
+                ExFreePoolWithTag(IndexRecord, TAG_NTFS);
+                ExFreePoolWithTag(MftRecord, TAG_NTFS);
+                return Status;
+            }
+            IndexAllocationSize = AttributeDataLength(&IndexAllocationCtx->Record);
+
+            RecordOffset = 0;
+
+            for (;;)
+            {
+                DPRINT("RecordOffset: %x IndexAllocationSize: %x\n", RecordOffset, IndexAllocationSize);
+                for (; RecordOffset < IndexAllocationSize;)
+                {
+                    UCHAR Bit = 1 << ((RecordOffset / IndexBlockSize) & 7);
+                    ULONG Byte = (RecordOffset / IndexBlockSize) >> 3;
+                    if ((BitmapData[Byte] & Bit))
+                        break;
+                    RecordOffset += IndexBlockSize;
+                }
+
+                if (RecordOffset >= IndexAllocationSize)
+                {
+                    break;
+                }
+
+                ReadAttribute(Vcb, IndexAllocationCtx, RecordOffset, IndexRecord, IndexBlockSize);
+
+                if (!NT_SUCCESS(FixupUpdateSequenceArray(Vcb, &((PFILE_RECORD_HEADER)IndexRecord)->Ntfs)))
+                {
+                    break;
+                }
+
+                IndexBuffer = (PINDEX_BUFFER)IndexRecord;
+                ASSERT(IndexBuffer->Ntfs.Type == NRH_INDX_TYPE);
+                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 ((IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK) > 0x10 &&
+                        CurrentEntry >= *FirstEntry &&
+                        CompareFileName(FileName, IndexEntry, DirSearch))
+                    {
+                        DPRINT("File found\n");
+                        *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);
+                }
+
+                RecordOffset += IndexBlockSize;
+            }
+
+            ReleaseAttributeContext(IndexAllocationCtx);
+            ExFreePoolWithTag(BitmapData, TAG_NTFS);
+        }
+
+        ExFreePoolWithTag(IndexRecord, TAG_NTFS);
+    }
+    else
     {
-//      PATTRIBUTE attrList = FindAttribute(file,AttributeAttributeList,0);
-        DbgPrint("Exeption \n");
-//      KeDebugCheck(0);
+        DPRINT("Can't read MFT record\n");
     }
+    ExFreePoolWithTag(MftRecord, TAG_NTFS);
 
-    ReadExternalAttribute(Vcb, NresAttr, vcn, count, buffer);
+    return STATUS_OBJECT_PATH_NOT_FOUND;
 }
 
-
-#if 0
-BOOL bitset(PUCHAR bitmap, ULONG i)
+NTSTATUS
+NtfsLookupFileAt(PDEVICE_EXTENSION Vcb,
+                 PUNICODE_STRING PathName,
+                 PFILE_RECORD_HEADER *FileRecord,
+                 PNTFS_ATTR_CONTEXT *DataContext,
+                 PULONGLONG MFTIndex,
+                 ULONGLONG CurrentMFTIndex)
 {
-    return (bitmap[i>>3] & (1 << (i & 7))) !=0;
-}
-#endif
+    UNICODE_STRING Current, Remaining;
+    NTSTATUS Status;
+    ULONG FirstEntry = 0;
 
+    DPRINT("NtfsLookupFileAt(%p, %wZ, %p, %p, %I64x)\n", Vcb, PathName, FileRecord, DataContext, CurrentMFTIndex);
 
-VOID
-FixupUpdateSequenceArray(PFILE_RECORD_HEADER file)
-{
-    PUSHORT usa = (PUSHORT)((ULONG_PTR)file + file->Ntfs.UsaOffset);
-    PUSHORT sector = (PUSHORT)file;
-    ULONG i;
+    FsRtlDissectName(*PathName, &Current, &Remaining);
+
+    while (Current.Length != 0)
+    {
+        DPRINT("Current: %wZ\n", &Current);
+
+        Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, &Current, &FirstEntry, FALSE, &CurrentMFTIndex);
+        if (!NT_SUCCESS(Status))
+        {
+            return Status;
+        }
+
+        if (Remaining.Length == 0)
+            break;
+
+        FsRtlDissectName(Current, &Current, &Remaining);
+    }
+
+    *FileRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
+    if (*FileRecord == NULL)
+    {
+        DPRINT("NtfsLookupFileAt: Can't allocate MFT record\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    Status = ReadFileRecord(Vcb, CurrentMFTIndex, *FileRecord);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT("NtfsLookupFileAt: Can't read MFT record\n");
+        ExFreePoolWithTag(*FileRecord, TAG_NTFS);
+        return Status;
+    }
 
-    for (i = 1; i < file->Ntfs.UsaCount; i++)
+    if (!((*FileRecord)->Flags & FRH_DIRECTORY))
     {
-        sector[255] = usa[i];
-        sector += 256;
+        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
-ReadLCN(PDEVICE_EXTENSION Vcb,
-        ULONGLONG lcn,
-        ULONG count,
-        PVOID buffer)
+NtfsFindFileAt(PDEVICE_EXTENSION Vcb,
+               PUNICODE_STRING SearchPattern,
+               PULONG FirstEntry,
+               PFILE_RECORD_HEADER *FileRecord,
+               PNTFS_ATTR_CONTEXT *DataContext,
+               PULONGLONG MFTIndex,
+               ULONGLONG CurrentMFTIndex)
 {
-    LARGE_INTEGER DiskSector;
+    NTSTATUS Status;
 
-    DiskSector.QuadPart = lcn;
+    DPRINT("NtfsFindFileAt(%p, %wZ, %p, %p, %p, %p, %I64x)\n", Vcb, SearchPattern, FirstEntry, FileRecord, DataContext, MFTIndex, CurrentMFTIndex);
 
-    return NtfsReadSectors(Vcb->StorageDevice,
-                           DiskSector.u.LowPart * Vcb->NtfsInfo.SectorsPerCluster,
-                           count * Vcb->NtfsInfo.SectorsPerCluster,
-                           Vcb->NtfsInfo.BytesPerSector,
-                           buffer,
-                           FALSE);
+    Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, SearchPattern, FirstEntry, TRUE, &CurrentMFTIndex);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT("NtfsFindFileAt: NtfsFindMftRecord() failed with status 0x%08lx\n", Status);
+        return Status;
+    }
+
+    *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("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 */