[NTFS]
[reactos.git] / reactos / drivers / filesystems / ntfs / mft.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/mft.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
24 * Updated by Valentin Verkhovsky 2003/09/12
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "ntfs.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 UNICODE_STRING IndexOfFileNames = RTL_CONSTANT_STRING(L"$I30");
35
36 /* FUNCTIONS ****************************************************************/
37
38 PNTFS_ATTR_CONTEXT
39 PrepareAttributeContext(PNTFS_ATTR_RECORD AttrRecord)
40 {
41 PNTFS_ATTR_CONTEXT Context;
42
43 Context = ExAllocatePoolWithTag(NonPagedPool,
44 FIELD_OFFSET(NTFS_ATTR_CONTEXT, Record) + AttrRecord->Length,
45 TAG_NTFS);
46 RtlCopyMemory(&Context->Record, AttrRecord, AttrRecord->Length);
47 if (AttrRecord->IsNonResident)
48 {
49 LONGLONG DataRunOffset;
50 ULONGLONG DataRunLength;
51
52 Context->CacheRun = (PUCHAR)&Context->Record + Context->Record.NonResident.MappingPairsOffset;
53 Context->CacheRunOffset = 0;
54 Context->CacheRun = DecodeRun(Context->CacheRun, &DataRunOffset, &DataRunLength);
55 Context->CacheRunLength = DataRunLength;
56 if (DataRunOffset != -1)
57 {
58 /* Normal run. */
59 Context->CacheRunStartLCN =
60 Context->CacheRunLastLCN = DataRunOffset;
61 }
62 else
63 {
64 /* Sparse run. */
65 Context->CacheRunStartLCN = -1;
66 Context->CacheRunLastLCN = 0;
67 }
68 Context->CacheRunCurrentOffset = 0;
69 }
70
71 return Context;
72 }
73
74
75 VOID
76 ReleaseAttributeContext(PNTFS_ATTR_CONTEXT Context)
77 {
78 ExFreePoolWithTag(Context, TAG_NTFS);
79 }
80
81
82 PNTFS_ATTR_CONTEXT
83 FindAttributeHelper(PDEVICE_EXTENSION Vcb,
84 PNTFS_ATTR_RECORD AttrRecord,
85 PNTFS_ATTR_RECORD AttrRecordEnd,
86 ULONG Type,
87 const WCHAR *Name,
88 ULONG NameLength)
89 {
90 DPRINT("FindAttributeHelper(%p, %p, %p, 0x%x, %s, %u)\n", Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
91
92 while (AttrRecord < AttrRecordEnd)
93 {
94 DPRINT("AttrRecord->Type = 0x%x\n", AttrRecord->Type);
95
96 if (AttrRecord->Type == AttributeEnd)
97 break;
98
99 if (AttrRecord->Type == AttributeAttributeList)
100 {
101 PNTFS_ATTR_CONTEXT Context;
102 PNTFS_ATTR_CONTEXT ListContext;
103 PVOID ListBuffer;
104 ULONGLONG ListSize;
105 PNTFS_ATTR_RECORD ListAttrRecord;
106 PNTFS_ATTR_RECORD ListAttrRecordEnd;
107
108 // Do not handle non-resident yet
109 ASSERT(!(AttrRecord->IsNonResident & 1));
110
111 ListContext = PrepareAttributeContext(AttrRecord);
112
113 ListSize = AttributeDataLength(&ListContext->Record);
114 if(ListSize <= 0xFFFFFFFF)
115 ListBuffer = ExAllocatePoolWithTag(NonPagedPool, (ULONG)ListSize, TAG_NTFS);
116 else
117 ListBuffer = NULL;
118
119 if(!ListBuffer)
120 {
121 DPRINT("Failed to allocate memory: %x\n", (ULONG)ListSize);
122 continue;
123 }
124
125 ListAttrRecord = (PNTFS_ATTR_RECORD)ListBuffer;
126 ListAttrRecordEnd = (PNTFS_ATTR_RECORD)((PCHAR)ListBuffer + ListSize);
127
128 if (ReadAttribute(Vcb, ListContext, 0, ListBuffer, (ULONG)ListSize) == ListSize)
129 {
130 Context = FindAttributeHelper(Vcb, ListAttrRecord, ListAttrRecordEnd,
131 Type, Name, NameLength);
132
133 ReleaseAttributeContext(ListContext);
134 ExFreePoolWithTag(ListBuffer, TAG_NTFS);
135
136 if (Context != NULL)
137 {
138 DPRINT("Found context = %p\n", Context);
139 return Context;
140 }
141 }
142 }
143
144 if (AttrRecord->Type == Type)
145 {
146 DPRINT("%d, %d\n", AttrRecord->NameLength, NameLength);
147 if (AttrRecord->NameLength == NameLength)
148 {
149 PWCHAR AttrName;
150
151 AttrName = (PWCHAR)((PCHAR)AttrRecord + AttrRecord->NameOffset);
152 DPRINT("%s, %s\n", AttrName, Name);
153 if (RtlCompareMemory(AttrName, Name, NameLength << 1) == (NameLength << 1))
154 {
155 /* Found it, fill up the context and return. */
156 DPRINT("Found context\n");
157 return PrepareAttributeContext(AttrRecord);
158 }
159 }
160 }
161
162 if (AttrRecord->Length == 0)
163 {
164 DPRINT("Null length attribute record\n");
165 return NULL;
166 }
167 AttrRecord = (PNTFS_ATTR_RECORD)((PCHAR)AttrRecord + AttrRecord->Length);
168 }
169
170 DPRINT("Ended\n");
171 return NULL;
172 }
173
174
175 NTSTATUS
176 FindAttribute(PDEVICE_EXTENSION Vcb,
177 PFILE_RECORD_HEADER MftRecord,
178 ULONG Type,
179 PUNICODE_STRING Name,
180 PNTFS_ATTR_CONTEXT * AttrCtx)
181 {
182 PNTFS_ATTR_RECORD AttrRecord;
183 PNTFS_ATTR_RECORD AttrRecordEnd;
184
185 DPRINT("NtfsFindAttribute(%p, %p, %u, %s)\n", Vcb, MftRecord, Type, Name);
186
187 AttrRecord = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + MftRecord->AttributeOffset);
188 AttrRecordEnd = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + Vcb->NtfsInfo.BytesPerFileRecord);
189
190 *AttrCtx = FindAttributeHelper(Vcb, AttrRecord, AttrRecordEnd, Type, Name->Buffer, Name->Length);
191 if (*AttrCtx == NULL)
192 {
193 return STATUS_OBJECT_NAME_NOT_FOUND;
194 }
195
196 return STATUS_SUCCESS;
197 }
198
199
200 ULONG
201 AttributeAllocatedLength(PNTFS_ATTR_RECORD AttrRecord)
202 {
203 if (AttrRecord->IsNonResident)
204 return AttrRecord->NonResident.AllocatedSize;
205 else
206 return AttrRecord->Resident.ValueLength;
207 }
208
209
210 ULONGLONG
211 AttributeDataLength(PNTFS_ATTR_RECORD AttrRecord)
212 {
213 if (AttrRecord->IsNonResident)
214 return AttrRecord->NonResident.DataSize;
215 else
216 return AttrRecord->Resident.ValueLength;
217 }
218
219
220 ULONG
221 ReadAttribute(PDEVICE_EXTENSION Vcb,
222 PNTFS_ATTR_CONTEXT Context,
223 ULONGLONG Offset,
224 PCHAR Buffer,
225 ULONG Length)
226 {
227 ULONGLONG LastLCN;
228 PUCHAR DataRun;
229 LONGLONG DataRunOffset;
230 ULONGLONG DataRunLength;
231 LONGLONG DataRunStartLCN;
232 ULONGLONG CurrentOffset;
233 ULONG ReadLength;
234 ULONG AlreadyRead;
235 NTSTATUS Status;
236
237 if (!Context->Record.IsNonResident)
238 {
239 if (Offset > Context->Record.Resident.ValueLength)
240 return 0;
241 if (Offset + Length > Context->Record.Resident.ValueLength)
242 Length = (ULONG)(Context->Record.Resident.ValueLength - Offset);
243 RtlCopyMemory(Buffer, (PCHAR)&Context->Record + Context->Record.Resident.ValueOffset + Offset, Length);
244 return Length;
245 }
246
247 /*
248 * Non-resident attribute
249 */
250
251 /*
252 * I. Find the corresponding start data run.
253 */
254
255 AlreadyRead = 0;
256
257 // FIXME: Cache seems to be non-working. Disable it for now
258 //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
259 if (0)
260 {
261 DataRun = Context->CacheRun;
262 LastLCN = Context->CacheRunLastLCN;
263 DataRunStartLCN = Context->CacheRunStartLCN;
264 DataRunLength = Context->CacheRunLength;
265 CurrentOffset = Context->CacheRunCurrentOffset;
266 }
267 else
268 {
269 LastLCN = 0;
270 DataRun = (PUCHAR)&Context->Record + Context->Record.NonResident.MappingPairsOffset;
271 CurrentOffset = 0;
272
273 while (1)
274 {
275 DataRun = DecodeRun(DataRun, &DataRunOffset, &DataRunLength);
276 if (DataRunOffset != -1)
277 {
278 /* Normal data run. */
279 DataRunStartLCN = LastLCN + DataRunOffset;
280 LastLCN = DataRunStartLCN;
281 }
282 else
283 {
284 /* Sparse data run. */
285 DataRunStartLCN = -1;
286 }
287
288 if (Offset >= CurrentOffset &&
289 Offset < CurrentOffset + (DataRunLength * Vcb->NtfsInfo.BytesPerCluster))
290 {
291 break;
292 }
293
294 if (*DataRun == 0)
295 {
296 return AlreadyRead;
297 }
298
299 CurrentOffset += DataRunLength * Vcb->NtfsInfo.BytesPerCluster;
300 }
301 }
302
303 /*
304 * II. Go through the run list and read the data
305 */
306
307 ReadLength = (ULONG)min(DataRunLength * Vcb->NtfsInfo.BytesPerCluster - (Offset - CurrentOffset), Length);
308 if (DataRunStartLCN == -1)
309 RtlZeroMemory(Buffer, ReadLength);
310 Status = NtfsReadDisk(Vcb->StorageDevice,
311 DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster + Offset - CurrentOffset,
312 ReadLength,
313 (PVOID)Buffer,
314 FALSE);
315 if (NT_SUCCESS(Status))
316 {
317 Length -= ReadLength;
318 Buffer += ReadLength;
319 AlreadyRead += ReadLength;
320
321 if (ReadLength == DataRunLength * Vcb->NtfsInfo.BytesPerCluster - (Offset - CurrentOffset))
322 {
323 CurrentOffset += DataRunLength * Vcb->NtfsInfo.BytesPerCluster;
324 DataRun = DecodeRun(DataRun, &DataRunOffset, &DataRunLength);
325 if (DataRunLength != (ULONGLONG)-1)
326 {
327 DataRunStartLCN = LastLCN + DataRunOffset;
328 LastLCN = DataRunStartLCN;
329 }
330 else
331 DataRunStartLCN = -1;
332
333 if (*DataRun == 0)
334 return AlreadyRead;
335 }
336
337 while (Length > 0)
338 {
339 ReadLength = (ULONG)min(DataRunLength * Vcb->NtfsInfo.BytesPerCluster, Length);
340 if (DataRunStartLCN == -1)
341 RtlZeroMemory(Buffer, ReadLength);
342 else
343 {
344 Status = NtfsReadDisk(Vcb->StorageDevice,
345 DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster,
346 ReadLength,
347 (PVOID)Buffer,
348 FALSE);
349 if (!NT_SUCCESS(Status))
350 break;
351 }
352
353 Length -= ReadLength;
354 Buffer += ReadLength;
355 AlreadyRead += ReadLength;
356
357 /* We finished this request, but there still data in this data run. */
358 if (Length == 0 && ReadLength != DataRunLength * Vcb->NtfsInfo.BytesPerCluster)
359 break;
360
361 /*
362 * Go to next run in the list.
363 */
364
365 if (*DataRun == 0)
366 break;
367 CurrentOffset += DataRunLength * Vcb->NtfsInfo.BytesPerCluster;
368 DataRun = DecodeRun(DataRun, &DataRunOffset, &DataRunLength);
369 if (DataRunOffset != -1)
370 {
371 /* Normal data run. */
372 DataRunStartLCN = LastLCN + DataRunOffset;
373 LastLCN = DataRunStartLCN;
374 }
375 else
376 {
377 /* Sparse data run. */
378 DataRunStartLCN = -1;
379 }
380 } /* while */
381
382 } /* if Disk */
383
384 Context->CacheRun = DataRun;
385 Context->CacheRunOffset = Offset + AlreadyRead;
386 Context->CacheRunStartLCN = DataRunStartLCN;
387 Context->CacheRunLength = DataRunLength;
388 Context->CacheRunLastLCN = LastLCN;
389 Context->CacheRunCurrentOffset = CurrentOffset;
390
391 return AlreadyRead;
392 }
393
394
395 NTSTATUS
396 ReadFileRecord(PDEVICE_EXTENSION Vcb,
397 ULONGLONG index,
398 PFILE_RECORD_HEADER file)
399 {
400 ULONGLONG BytesRead;
401
402 BytesRead = ReadAttribute(Vcb, Vcb->MFTContext, index * Vcb->NtfsInfo.BytesPerFileRecord, (PCHAR)file, Vcb->NtfsInfo.BytesPerFileRecord);
403 if (BytesRead != Vcb->NtfsInfo.BytesPerFileRecord)
404 {
405 DPRINT1("ReadFileRecord failed: %u read, %u expected\n", BytesRead, Vcb->NtfsInfo.BytesPerFileRecord);
406 return STATUS_PARTIAL_COPY;
407 }
408
409 /* Apply update sequence array fixups. */
410 return FixupUpdateSequenceArray(Vcb, &file->Ntfs);
411 }
412
413
414 NTSTATUS
415 FixupUpdateSequenceArray(PDEVICE_EXTENSION Vcb,
416 PNTFS_RECORD_HEADER Record)
417 {
418 USHORT *USA;
419 USHORT USANumber;
420 USHORT USACount;
421 USHORT *Block;
422
423 USA = (USHORT*)((PCHAR)Record + Record->UsaOffset);
424 USANumber = *(USA++);
425 USACount = Record->UsaCount - 1; /* Exclude the USA Number. */
426 Block = (USHORT*)((PCHAR)Record + Vcb->NtfsInfo.BytesPerSector - 2);
427
428 while (USACount)
429 {
430 if (*Block != USANumber)
431 {
432 DPRINT1("Mismatch with USA: %u read, %u expected\n" , *Block, USANumber);
433 return STATUS_UNSUCCESSFUL;
434 }
435 *Block = *(USA++);
436 Block = (USHORT*)((PCHAR)Block + Vcb->NtfsInfo.BytesPerSector);
437 USACount--;
438 }
439
440 return STATUS_SUCCESS;
441 }
442
443
444 NTSTATUS
445 ReadLCN(PDEVICE_EXTENSION Vcb,
446 ULONGLONG lcn,
447 ULONG count,
448 PVOID buffer)
449 {
450 LARGE_INTEGER DiskSector;
451
452 DiskSector.QuadPart = lcn;
453
454 return NtfsReadSectors(Vcb->StorageDevice,
455 DiskSector.u.LowPart * Vcb->NtfsInfo.SectorsPerCluster,
456 count * Vcb->NtfsInfo.SectorsPerCluster,
457 Vcb->NtfsInfo.BytesPerSector,
458 buffer,
459 FALSE);
460 }
461
462
463 BOOLEAN
464 CompareFileName(PUNICODE_STRING FileName,
465 PINDEX_ENTRY_ATTRIBUTE IndexEntry)
466 {
467 UNICODE_STRING EntryName;
468
469 EntryName.Buffer = IndexEntry->FileName.Name;
470 EntryName.Length =
471 EntryName.MaximumLength = IndexEntry->FileName.NameLength;
472
473 return (RtlCompareUnicodeString(FileName, &EntryName, !!(IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)) == TRUE);
474 }
475
476
477 NTSTATUS
478 NtfsFindMftRecord(PDEVICE_EXTENSION Vcb, ULONGLONG MFTIndex, PUNICODE_STRING FileName, ULONGLONG *OutMFTIndex)
479 {
480 PFILE_RECORD_HEADER MftRecord;
481 //ULONG Magic;
482 PNTFS_ATTR_CONTEXT IndexRootCtx;
483 PNTFS_ATTR_CONTEXT IndexBitmapCtx;
484 PNTFS_ATTR_CONTEXT IndexAllocationCtx;
485 PINDEX_ROOT_ATTRIBUTE IndexRoot;
486 ULONGLONG BitmapDataSize;
487 ULONGLONG IndexAllocationSize;
488 PCHAR BitmapData;
489 PCHAR IndexRecord;
490 PINDEX_ENTRY_ATTRIBUTE IndexEntry, IndexEntryEnd;
491 ULONG RecordOffset;
492 ULONG IndexBlockSize;
493 NTSTATUS Status;
494
495 MftRecord = ExAllocatePoolWithTag(NonPagedPool,
496 Vcb->NtfsInfo.BytesPerFileRecord,
497 TAG_NTFS);
498 if (MftRecord == NULL)
499 {
500 return STATUS_INSUFFICIENT_RESOURCES;
501 }
502
503 if (ReadFileRecord(Vcb, MFTIndex, MftRecord))
504 {
505 //Magic = MftRecord->Magic;
506
507 Status = FindAttribute(Vcb, MftRecord, AttributeIndexRoot, &IndexOfFileNames, &IndexRootCtx);
508 if (!NT_SUCCESS(Status))
509 {
510 ExFreePoolWithTag(MftRecord, TAG_NTFS);
511 return Status;
512 }
513
514 IndexRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerIndexRecord, TAG_NTFS);
515 if (IndexRecord == NULL)
516 {
517 ExFreePoolWithTag(MftRecord, TAG_NTFS);
518 return STATUS_INSUFFICIENT_RESOURCES;
519 }
520
521 ReadAttribute(Vcb, IndexRootCtx, 0, IndexRecord, Vcb->NtfsInfo.BytesPerIndexRecord);
522 IndexRoot = (PINDEX_ROOT_ATTRIBUTE)IndexRecord;
523 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)&IndexRoot->Header + IndexRoot->Header.FirstEntryOffset);
524 /* Index root is always resident. */
525 IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + IndexRootCtx->Record.Resident.ValueLength);
526 ReleaseAttributeContext(IndexRootCtx);
527
528 DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb->NtfsInfo.BytesPerIndexRecord, IndexRoot->SizeOfEntry);
529
530 while (IndexEntry < IndexEntryEnd &&
531 !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
532 {
533 if (CompareFileName(FileName, IndexEntry))
534 {
535 *OutMFTIndex = IndexEntry->Data.Directory.IndexedFile;
536 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
537 ExFreePoolWithTag(MftRecord, TAG_NTFS);
538 return STATUS_SUCCESS;
539 }
540 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
541 }
542
543 if (IndexRoot->Header.Flags & INDEX_ROOT_LARGE)
544 {
545 DPRINT("Large Index!\n");
546
547 IndexBlockSize = IndexRoot->SizeOfEntry;
548
549 Status = FindAttribute(Vcb, MftRecord, AttributeBitmap, &IndexOfFileNames, &IndexBitmapCtx);
550 if (!NT_SUCCESS(Status))
551 {
552 DPRINT("Corrupted filesystem!\n");
553 ExFreePoolWithTag(MftRecord, TAG_NTFS);
554 return Status;
555 }
556 BitmapDataSize = AttributeDataLength(&IndexBitmapCtx->Record);
557 DPRINT("BitmapDataSize: %x\n", (ULONG)BitmapDataSize);
558 if(BitmapDataSize <= 0xFFFFFFFF)
559 BitmapData = ExAllocatePoolWithTag(NonPagedPool, (ULONG)BitmapDataSize, TAG_NTFS);
560 else
561 BitmapData = NULL;
562
563 if (BitmapData == NULL)
564 {
565 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
566 ExFreePoolWithTag(MftRecord, TAG_NTFS);
567 return STATUS_INSUFFICIENT_RESOURCES;
568 }
569 ReadAttribute(Vcb, IndexBitmapCtx, 0, BitmapData, (ULONG)BitmapDataSize);
570 ReleaseAttributeContext(IndexBitmapCtx);
571
572 Status = FindAttribute(Vcb, MftRecord, AttributeIndexAllocation, &IndexOfFileNames, &IndexAllocationCtx);
573 if (!NT_SUCCESS(Status))
574 {
575 DPRINT("Corrupted filesystem!\n");
576 ExFreePoolWithTag(BitmapData, TAG_NTFS);
577 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
578 ExFreePoolWithTag(MftRecord, TAG_NTFS);
579 return Status;
580 }
581 IndexAllocationSize = AttributeDataLength(&IndexAllocationCtx->Record);
582
583 RecordOffset = 0;
584
585 for (;;)
586 {
587 DPRINT("RecordOffset: %x IndexAllocationSize: %x\n", RecordOffset, IndexAllocationSize);
588 for (; RecordOffset < IndexAllocationSize;)
589 {
590 UCHAR Bit = 1 << ((RecordOffset / IndexBlockSize) & 7);
591 ULONG Byte = (RecordOffset / IndexBlockSize) >> 3;
592 if ((BitmapData[Byte] & Bit))
593 break;
594 RecordOffset += IndexBlockSize;
595 }
596
597 if (RecordOffset >= IndexAllocationSize)
598 {
599 break;
600 }
601
602 ReadAttribute(Vcb, IndexAllocationCtx, RecordOffset, IndexRecord, IndexBlockSize);
603
604 if (!FixupUpdateSequenceArray(Vcb, &((PFILE_RECORD_HEADER)IndexRecord)->Ntfs))
605 {
606 break;
607 }
608
609 /* FIXME */
610 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + 0x18 + *(USHORT *)(IndexRecord + 0x18));
611 IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + IndexBlockSize);
612
613 while (IndexEntry < IndexEntryEnd &&
614 !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
615 {
616 if (CompareFileName(FileName, IndexEntry))
617 {
618 DPRINT("File found\n");
619 *OutMFTIndex = IndexEntry->Data.Directory.IndexedFile;
620 ExFreePoolWithTag(BitmapData, TAG_NTFS);
621 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
622 ExFreePoolWithTag(MftRecord, TAG_NTFS);
623 ReleaseAttributeContext(IndexAllocationCtx);
624 return STATUS_SUCCESS;
625 }
626 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
627 }
628
629 RecordOffset += IndexBlockSize;
630 }
631
632 ReleaseAttributeContext(IndexAllocationCtx);
633 ExFreePoolWithTag(BitmapData, TAG_NTFS);
634 }
635
636 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
637 }
638 else
639 {
640 DPRINT("Can't read MFT record\n");
641 }
642 ExFreePoolWithTag(MftRecord, TAG_NTFS);
643
644 return STATUS_OBJECT_PATH_NOT_FOUND;
645 }
646
647 NTSTATUS
648 NtfsLookupFileAt(PDEVICE_EXTENSION Vcb,
649 PUNICODE_STRING PathName,
650 PFILE_RECORD_HEADER *FileRecord,
651 PNTFS_ATTR_CONTEXT *DataContext,
652 ULONGLONG CurrentMFTIndex)
653 {
654 UNICODE_STRING Current, Remaining;
655 NTSTATUS Status;
656
657 DPRINT1("NtfsLookupFileAt(%p, %wZ, %p, %p, %I64x)\n", Vcb, PathName, FileRecord, DataContext, CurrentMFTIndex);
658
659 FsRtlDissectName(*PathName, &Current, &Remaining);
660
661 while (Current.Length != 0)
662 {
663 DPRINT1("Lookup: %wZ\n", &Current);
664
665 Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, &Current, &CurrentMFTIndex);
666 if (!NT_SUCCESS(Status))
667 {
668 return Status;
669 }
670
671 FsRtlDissectName(*PathName, &Current, &Remaining);
672 }
673
674 *FileRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
675 if (*FileRecord == NULL)
676 {
677 DPRINT("NtfsLookupFile: Can't allocate MFT record\n");
678 return STATUS_INSUFFICIENT_RESOURCES;
679 }
680
681 Status = ReadFileRecord(Vcb, CurrentMFTIndex, *FileRecord);
682 if (!NT_SUCCESS(Status))
683 {
684 DPRINT("NtfsLookupFile: Can't read MFT record\n");
685 ExFreePoolWithTag(FileRecord, TAG_NTFS);
686 return Status;
687 }
688
689 Status = FindAttribute(Vcb, *FileRecord, AttributeData, PathName, DataContext);
690 if (!NT_SUCCESS(Status))
691 {
692 DPRINT("NtfsLookupFile: Can't find data attribute\n");
693 ExFreePoolWithTag(FileRecord, TAG_NTFS);
694 return Status;
695 }
696
697 return STATUS_SUCCESS;
698 }
699
700 NTSTATUS
701 NtfsLookupFile(PDEVICE_EXTENSION Vcb,
702 PUNICODE_STRING PathName,
703 PFILE_RECORD_HEADER *FileRecord,
704 PNTFS_ATTR_CONTEXT *DataContext)
705 {
706 return NtfsLookupFileAt(Vcb, PathName, FileRecord, DataContext, NTFS_FILE_ROOT);
707 }
708 /* EOF */