17e5553d005014bd588c4bc900225180e5b87d7c
[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 BOOLEAN DirSearch)
467 {
468 UNICODE_STRING EntryName;
469
470 EntryName.Buffer = IndexEntry->FileName.Name;
471 EntryName.Length =
472 EntryName.MaximumLength = IndexEntry->FileName.NameLength;
473
474 if (DirSearch)
475 {
476 return FsRtlIsNameInExpression(FileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX), NULL);
477 }
478 else
479 {
480 return (RtlCompareUnicodeString(FileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)) == TRUE);
481 }
482 }
483
484
485 NTSTATUS
486 NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
487 ULONGLONG MFTIndex,
488 PUNICODE_STRING FileName,
489 PULONG FirstEntry,
490 BOOLEAN DirSearch,
491 ULONGLONG *OutMFTIndex,
492 PWSTR OutName)
493 {
494 PFILE_RECORD_HEADER MftRecord;
495 //ULONG Magic;
496 PNTFS_ATTR_CONTEXT IndexRootCtx;
497 PNTFS_ATTR_CONTEXT IndexBitmapCtx;
498 PNTFS_ATTR_CONTEXT IndexAllocationCtx;
499 PINDEX_ROOT_ATTRIBUTE IndexRoot;
500 ULONGLONG BitmapDataSize;
501 ULONGLONG IndexAllocationSize;
502 PCHAR BitmapData;
503 PCHAR IndexRecord;
504 PINDEX_ENTRY_ATTRIBUTE IndexEntry, IndexEntryEnd;
505 ULONG RecordOffset;
506 ULONG IndexBlockSize;
507 NTSTATUS Status;
508 ULONG CurrentEntry = 0;
509
510 MftRecord = ExAllocatePoolWithTag(NonPagedPool,
511 Vcb->NtfsInfo.BytesPerFileRecord,
512 TAG_NTFS);
513 if (MftRecord == NULL)
514 {
515 return STATUS_INSUFFICIENT_RESOURCES;
516 }
517
518 if (NT_SUCCESS(ReadFileRecord(Vcb, MFTIndex, MftRecord)))
519 {
520 //Magic = MftRecord->Magic;
521
522 Status = FindAttribute(Vcb, MftRecord, AttributeIndexRoot, &IndexOfFileNames, &IndexRootCtx);
523 if (!NT_SUCCESS(Status))
524 {
525 ExFreePoolWithTag(MftRecord, TAG_NTFS);
526 return Status;
527 }
528
529 IndexRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerIndexRecord, TAG_NTFS);
530 if (IndexRecord == NULL)
531 {
532 ExFreePoolWithTag(MftRecord, TAG_NTFS);
533 return STATUS_INSUFFICIENT_RESOURCES;
534 }
535
536 ReadAttribute(Vcb, IndexRootCtx, 0, IndexRecord, Vcb->NtfsInfo.BytesPerIndexRecord);
537 IndexRoot = (PINDEX_ROOT_ATTRIBUTE)IndexRecord;
538 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)&IndexRoot->Header + IndexRoot->Header.FirstEntryOffset);
539 /* Index root is always resident. */
540 IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + IndexRootCtx->Record.Resident.ValueLength);
541 ReleaseAttributeContext(IndexRootCtx);
542
543 DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb->NtfsInfo.BytesPerIndexRecord, IndexRoot->SizeOfEntry);
544
545 while (IndexEntry < IndexEntryEnd &&
546 !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
547 {
548 if (CurrentEntry >= *FirstEntry && CompareFileName(FileName, IndexEntry, DirSearch))
549 {
550 *OutMFTIndex = IndexEntry->Data.Directory.IndexedFile;
551 *FirstEntry = CurrentEntry;
552 RtlCopyMemory(OutName, IndexEntry->FileName.Name, IndexEntry->FileName.NameLength);
553 OutName[IndexEntry->FileName.NameLength / sizeof(WCHAR)] = UNICODE_NULL;
554 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
555 ExFreePoolWithTag(MftRecord, TAG_NTFS);
556 return STATUS_SUCCESS;
557 }
558
559 ++CurrentEntry;
560 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
561 }
562
563 if (IndexRoot->Header.Flags & INDEX_ROOT_LARGE)
564 {
565 DPRINT("Large Index!\n");
566
567 IndexBlockSize = IndexRoot->SizeOfEntry;
568
569 Status = FindAttribute(Vcb, MftRecord, AttributeBitmap, &IndexOfFileNames, &IndexBitmapCtx);
570 if (!NT_SUCCESS(Status))
571 {
572 DPRINT("Corrupted filesystem!\n");
573 ExFreePoolWithTag(MftRecord, TAG_NTFS);
574 return Status;
575 }
576 BitmapDataSize = AttributeDataLength(&IndexBitmapCtx->Record);
577 DPRINT("BitmapDataSize: %x\n", (ULONG)BitmapDataSize);
578 if(BitmapDataSize <= 0xFFFFFFFF)
579 BitmapData = ExAllocatePoolWithTag(NonPagedPool, (ULONG)BitmapDataSize, TAG_NTFS);
580 else
581 BitmapData = NULL;
582
583 if (BitmapData == NULL)
584 {
585 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
586 ExFreePoolWithTag(MftRecord, TAG_NTFS);
587 return STATUS_INSUFFICIENT_RESOURCES;
588 }
589 ReadAttribute(Vcb, IndexBitmapCtx, 0, BitmapData, (ULONG)BitmapDataSize);
590 ReleaseAttributeContext(IndexBitmapCtx);
591
592 Status = FindAttribute(Vcb, MftRecord, AttributeIndexAllocation, &IndexOfFileNames, &IndexAllocationCtx);
593 if (!NT_SUCCESS(Status))
594 {
595 DPRINT("Corrupted filesystem!\n");
596 ExFreePoolWithTag(BitmapData, TAG_NTFS);
597 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
598 ExFreePoolWithTag(MftRecord, TAG_NTFS);
599 return Status;
600 }
601 IndexAllocationSize = AttributeDataLength(&IndexAllocationCtx->Record);
602
603 RecordOffset = 0;
604
605 for (;;)
606 {
607 DPRINT("RecordOffset: %x IndexAllocationSize: %x\n", RecordOffset, IndexAllocationSize);
608 for (; RecordOffset < IndexAllocationSize;)
609 {
610 UCHAR Bit = 1 << ((RecordOffset / IndexBlockSize) & 7);
611 ULONG Byte = (RecordOffset / IndexBlockSize) >> 3;
612 if ((BitmapData[Byte] & Bit))
613 break;
614 RecordOffset += IndexBlockSize;
615 }
616
617 if (RecordOffset >= IndexAllocationSize)
618 {
619 break;
620 }
621
622 ReadAttribute(Vcb, IndexAllocationCtx, RecordOffset, IndexRecord, IndexBlockSize);
623
624 if (!FixupUpdateSequenceArray(Vcb, &((PFILE_RECORD_HEADER)IndexRecord)->Ntfs))
625 {
626 break;
627 }
628
629 /* FIXME */
630 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + 0x18 + *(USHORT *)(IndexRecord + 0x18));
631 IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)(IndexRecord + IndexBlockSize);
632
633 while (IndexEntry < IndexEntryEnd &&
634 !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
635 {
636 if (CurrentEntry >= *FirstEntry && CompareFileName(FileName, IndexEntry, DirSearch))
637 {
638 DPRINT("File found\n");
639 *OutMFTIndex = IndexEntry->Data.Directory.IndexedFile;
640 *FirstEntry = CurrentEntry;
641 RtlCopyMemory(OutName, IndexEntry->FileName.Name, IndexEntry->FileName.NameLength);
642 OutName[IndexEntry->FileName.NameLength / sizeof(WCHAR)] = UNICODE_NULL;
643 ExFreePoolWithTag(BitmapData, TAG_NTFS);
644 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
645 ExFreePoolWithTag(MftRecord, TAG_NTFS);
646 ReleaseAttributeContext(IndexAllocationCtx);
647 return STATUS_SUCCESS;
648 }
649
650 ++CurrentEntry;
651 IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
652 }
653
654 RecordOffset += IndexBlockSize;
655 }
656
657 ReleaseAttributeContext(IndexAllocationCtx);
658 ExFreePoolWithTag(BitmapData, TAG_NTFS);
659 }
660
661 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
662 }
663 else
664 {
665 DPRINT("Can't read MFT record\n");
666 }
667 ExFreePoolWithTag(MftRecord, TAG_NTFS);
668
669 return STATUS_OBJECT_PATH_NOT_FOUND;
670 }
671
672 NTSTATUS
673 NtfsLookupFileAt(PDEVICE_EXTENSION Vcb,
674 PUNICODE_STRING PathName,
675 PFILE_RECORD_HEADER *FileRecord,
676 PNTFS_ATTR_CONTEXT *DataContext,
677 PULONGLONG MFTIndex,
678 ULONGLONG CurrentMFTIndex)
679 {
680 UNICODE_STRING Current, Remaining, Found;
681 NTSTATUS Status;
682 WCHAR FoundName[MAX_PATH + 1];
683 ULONG FirstEntry = 0;
684
685 DPRINT1("NtfsLookupFileAt(%p, %wZ, %p, %p, %I64x)\n", Vcb, PathName, FileRecord, DataContext, CurrentMFTIndex);
686
687 FsRtlDissectName(*PathName, &Current, &Remaining);
688
689 while (Current.Length != 0)
690 {
691 DPRINT1("Lookup: %wZ\n", &Current);
692
693 Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, &Current, &FirstEntry, FALSE, &CurrentMFTIndex, FoundName);
694 if (!NT_SUCCESS(Status))
695 {
696 return Status;
697 }
698
699 FsRtlDissectName(*PathName, &Current, &Remaining);
700 }
701
702 *FileRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
703 if (*FileRecord == NULL)
704 {
705 DPRINT("NtfsLookupFileAt: Can't allocate MFT record\n");
706 return STATUS_INSUFFICIENT_RESOURCES;
707 }
708
709 Status = ReadFileRecord(Vcb, CurrentMFTIndex, *FileRecord);
710 if (!NT_SUCCESS(Status))
711 {
712 DPRINT("NtfsLookupFileAt: Can't read MFT record\n");
713 ExFreePoolWithTag(FileRecord, TAG_NTFS);
714 return Status;
715 }
716
717 RtlInitUnicodeString(&Found, FoundName);
718
719 Status = FindAttribute(Vcb, *FileRecord, AttributeData, &Found, DataContext);
720 if (!NT_SUCCESS(Status))
721 {
722 DPRINT("NtfsLookupFileAt: Can't find data attribute\n");
723 ExFreePoolWithTag(FileRecord, TAG_NTFS);
724 return Status;
725 }
726
727 *MFTIndex = CurrentMFTIndex;
728
729 return STATUS_SUCCESS;
730 }
731
732 NTSTATUS
733 NtfsLookupFile(PDEVICE_EXTENSION Vcb,
734 PUNICODE_STRING PathName,
735 PFILE_RECORD_HEADER *FileRecord,
736 PNTFS_ATTR_CONTEXT *DataContext,
737 PULONGLONG MFTIndex)
738 {
739 return NtfsLookupFileAt(Vcb, PathName, FileRecord, DataContext, MFTIndex, NTFS_FILE_ROOT);
740 }
741
742 NTSTATUS
743 NtfsFindFileAt(PDEVICE_EXTENSION Vcb,
744 PUNICODE_STRING SearchPattern,
745 PULONG FirstEntry,
746 PFILE_RECORD_HEADER *FileRecord,
747 PNTFS_ATTR_CONTEXT *DataContext,
748 PULONGLONG MFTIndex,
749 ULONGLONG CurrentMFTIndex)
750 {
751 UNICODE_STRING Found;
752 NTSTATUS Status;
753 WCHAR FoundName[MAX_PATH + 1];
754
755 DPRINT1("NtfsFindFileAt(%p, %wZ, %p, %p, %p, %p, %I64x)\n", Vcb, SearchPattern, FirstEntry, FileRecord, DataContext, MFTIndex, CurrentMFTIndex);
756
757 Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, SearchPattern, FirstEntry, TRUE, &CurrentMFTIndex, FoundName);
758 if (!NT_SUCCESS(Status))
759 {
760 return Status;
761 }
762
763 *FileRecord = ExAllocatePoolWithTag(NonPagedPool, Vcb->NtfsInfo.BytesPerFileRecord, TAG_NTFS);
764 if (*FileRecord == NULL)
765 {
766 DPRINT("NtfsFindFileAt: Can't allocate MFT record\n");
767 return STATUS_INSUFFICIENT_RESOURCES;
768 }
769
770 Status = ReadFileRecord(Vcb, CurrentMFTIndex, *FileRecord);
771 if (!NT_SUCCESS(Status))
772 {
773 DPRINT("NtfsFindFileAt: Can't read MFT record\n");
774 ExFreePoolWithTag(FileRecord, TAG_NTFS);
775 return Status;
776 }
777
778 RtlInitUnicodeString(&Found, FoundName);
779
780 Status = FindAttribute(Vcb, *FileRecord, AttributeData, &Found, DataContext);
781 if (!NT_SUCCESS(Status))
782 {
783 DPRINT("NtfsFindFileAt: Can't find data attribute\n");
784 ExFreePoolWithTag(FileRecord, TAG_NTFS);
785 return Status;
786 }
787
788 *MFTIndex = CurrentMFTIndex;
789
790 return STATUS_SUCCESS;
791 }
792
793 /* EOF */