3 * Copyright (C) 2002, 2014 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/mft.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
25 * Pierre Schweitzer (pierre@reactos.org)
26 * Hervé Poussineau (hpoussin@reactos.org)
30 /* INCLUDES *****************************************************************/
33 #include <ntintsafe.h>
38 /* FUNCTIONS ****************************************************************/
41 PrepareAttributeContext(PNTFS_ATTR_RECORD AttrRecord
)
43 PNTFS_ATTR_CONTEXT Context
;
45 Context
= ExAllocatePoolWithTag(NonPagedPool
,
46 sizeof(NTFS_ATTR_CONTEXT
),
50 DPRINT1("Error: Unable to allocate memory for context!\n");
54 // Allocate memory for a copy of the attribute
55 Context
->pRecord
= ExAllocatePoolWithTag(NonPagedPool
, AttrRecord
->Length
, TAG_NTFS
);
58 DPRINT1("Error: Unable to allocate memory for attribute record!\n");
59 ExFreePoolWithTag(Context
, TAG_NTFS
);
64 RtlCopyMemory(Context
->pRecord
, AttrRecord
, AttrRecord
->Length
);
66 if (AttrRecord
->IsNonResident
)
68 LONGLONG DataRunOffset
;
69 ULONGLONG DataRunLength
;
70 ULONGLONG NextVBN
= 0;
71 PUCHAR DataRun
= (PUCHAR
)((ULONG_PTR
)Context
->pRecord
+ Context
->pRecord
->NonResident
.MappingPairsOffset
);
73 Context
->CacheRun
= DataRun
;
74 Context
->CacheRunOffset
= 0;
75 Context
->CacheRun
= DecodeRun(Context
->CacheRun
, &DataRunOffset
, &DataRunLength
);
76 Context
->CacheRunLength
= DataRunLength
;
77 if (DataRunOffset
!= -1)
80 Context
->CacheRunStartLCN
=
81 Context
->CacheRunLastLCN
= DataRunOffset
;
86 Context
->CacheRunStartLCN
= -1;
87 Context
->CacheRunLastLCN
= 0;
89 Context
->CacheRunCurrentOffset
= 0;
91 // Convert the data runs to a map control block
92 if (!NT_SUCCESS(ConvertDataRunsToLargeMCB(DataRun
, &Context
->DataRunsMCB
, &NextVBN
)))
94 DPRINT1("Unable to convert data runs to MCB!\n");
95 ExFreePoolWithTag(Context
->pRecord
, TAG_NTFS
);
96 ExFreePoolWithTag(Context
, TAG_NTFS
);
106 ReleaseAttributeContext(PNTFS_ATTR_CONTEXT Context
)
108 if (Context
->pRecord
->IsNonResident
)
110 FsRtlUninitializeLargeMcb(&Context
->DataRunsMCB
);
114 ExFreePoolWithTag(Context
->pRecord
, TAG_NTFS
);
116 ExFreePoolWithTag(Context
, TAG_NTFS
);
121 * @name FindAttribute
124 * Searches a file record for an attribute matching the given type and name.
127 * Optional pointer to a ULONG that will receive the offset of the found attribute
128 * from the beginning of the record. Can be set to NULL.
131 FindAttribute(PDEVICE_EXTENSION Vcb
,
132 PFILE_RECORD_HEADER MftRecord
,
136 PNTFS_ATTR_CONTEXT
* AttrCtx
,
141 FIND_ATTR_CONTXT Context
;
142 PNTFS_ATTR_RECORD Attribute
;
144 DPRINT("FindAttribute(%p, %p, 0x%x, %S, %lu, %p, %p)\n", Vcb
, MftRecord
, Type
, Name
, NameLength
, AttrCtx
, Offset
);
147 Status
= FindFirstAttribute(&Context
, Vcb
, MftRecord
, FALSE
, &Attribute
);
148 while (NT_SUCCESS(Status
))
150 if (Attribute
->Type
== Type
&& Attribute
->NameLength
== NameLength
)
156 AttrName
= (PWCHAR
)((PCHAR
)Attribute
+ Attribute
->NameOffset
);
157 DPRINT("%.*S, %.*S\n", Attribute
->NameLength
, AttrName
, NameLength
, Name
);
158 if (RtlCompareMemory(AttrName
, Name
, NameLength
* sizeof(WCHAR
)) == (NameLength
* sizeof(WCHAR
)))
170 /* Found it, fill up the context and return. */
171 DPRINT("Found context\n");
172 *AttrCtx
= PrepareAttributeContext(Attribute
);
174 (*AttrCtx
)->FileMFTIndex
= MftRecord
->MFTRecordNumber
;
177 *Offset
= Context
.Offset
;
179 FindCloseAttribute(&Context
);
180 return STATUS_SUCCESS
;
184 Status
= FindNextAttribute(&Context
, &Attribute
);
187 FindCloseAttribute(&Context
);
188 return STATUS_OBJECT_NAME_NOT_FOUND
;
193 AttributeAllocatedLength(PNTFS_ATTR_RECORD AttrRecord
)
195 if (AttrRecord
->IsNonResident
)
196 return AttrRecord
->NonResident
.AllocatedSize
;
198 return ALIGN_UP_BY(AttrRecord
->Resident
.ValueLength
, ATTR_RECORD_ALIGNMENT
);
203 AttributeDataLength(PNTFS_ATTR_RECORD AttrRecord
)
205 if (AttrRecord
->IsNonResident
)
206 return AttrRecord
->NonResident
.DataSize
;
208 return AttrRecord
->Resident
.ValueLength
;
212 * @name IncreaseMftSize
215 * Increases the size of the master file table on a volume, increasing the space available for file records.
218 * Pointer to the VCB (DEVICE_EXTENSION) of the target volume.
222 * Boolean indicating if the function is allowed to wait for exclusive access to the master file table.
223 * This will only be relevant if the MFT doesn't have any free file records and needs to be enlarged.
226 * STATUS_SUCCESS on success.
227 * STATUS_INSUFFICIENT_RESOURCES if an allocation fails.
228 * STATUS_INVALID_PARAMETER if there was an error reading the Mft's bitmap.
229 * STATUS_CANT_WAIT if CanWait was FALSE and the function could not get immediate, exclusive access to the MFT.
232 * Increases the size of the Master File Table by 64 records. Bitmap entries for the new records are cleared,
233 * and the bitmap is also enlarged if needed. Mimicking Windows' behavior when enlarging the mft is still TODO.
234 * This function will wait for exlusive access to the volume fcb.
237 IncreaseMftSize(PDEVICE_EXTENSION Vcb
, BOOLEAN CanWait
)
239 PNTFS_ATTR_CONTEXT BitmapContext
;
240 LARGE_INTEGER BitmapSize
;
241 LARGE_INTEGER DataSize
;
242 LONGLONG BitmapSizeDifference
;
243 ULONG NewRecords
= ATTR_RECORD_ALIGNMENT
* 8; // Allocate one new record for every bit of every byte we'll be adding to the bitmap
244 ULONG DataSizeDifference
= Vcb
->NtfsInfo
.BytesPerFileRecord
* NewRecords
;
247 ULONGLONG BitmapBytes
;
248 ULONGLONG NewBitmapSize
;
249 ULONGLONG FirstNewMftIndex
;
252 PFILE_RECORD_HEADER BlankFileRecord
;
256 DPRINT1("IncreaseMftSize(%p, %s)\n", Vcb
, CanWait
? "TRUE" : "FALSE");
258 // We need exclusive access to the mft while we change its size
259 if (!ExAcquireResourceExclusiveLite(&(Vcb
->DirResource
), CanWait
))
261 return STATUS_CANT_WAIT
;
264 // Create a blank file record that will be used later
265 BlankFileRecord
= NtfsCreateEmptyFileRecord(Vcb
);
266 if (!BlankFileRecord
)
268 DPRINT1("Error: Unable to create empty file record!\n");
269 return STATUS_INSUFFICIENT_RESOURCES
;
272 // Clear the flags (file record is not in use)
273 BlankFileRecord
->Flags
= 0;
275 // Find the bitmap attribute of master file table
276 Status
= FindAttribute(Vcb
, Vcb
->MasterFileTable
, AttributeBitmap
, L
"", 0, &BitmapContext
, NULL
);
277 if (!NT_SUCCESS(Status
))
279 DPRINT1("ERROR: Couldn't find $BITMAP attribute of Mft!\n");
280 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
281 ExReleaseResourceLite(&(Vcb
->DirResource
));
285 // Get size of Bitmap Attribute
286 BitmapSize
.QuadPart
= AttributeDataLength(BitmapContext
->pRecord
);
288 // Calculate the new mft size
289 DataSize
.QuadPart
= AttributeDataLength(Vcb
->MFTContext
->pRecord
) + DataSizeDifference
;
291 // Find the index of the first Mft entry that will be created
292 FirstNewMftIndex
= AttributeDataLength(Vcb
->MFTContext
->pRecord
) / Vcb
->NtfsInfo
.BytesPerFileRecord
;
294 // Determine how many bytes will make up the bitmap
295 BitmapBytes
= DataSize
.QuadPart
/ Vcb
->NtfsInfo
.BytesPerFileRecord
/ 8;
296 if ((DataSize
.QuadPart
/ Vcb
->NtfsInfo
.BytesPerFileRecord
) % 8 != 0)
299 // Windows will always keep the number of bytes in a bitmap as a multiple of 8, so no bytes are wasted on slack
300 BitmapBytes
= ALIGN_UP_BY(BitmapBytes
, ATTR_RECORD_ALIGNMENT
);
302 // Determine how much we need to adjust the bitmap size (it's possible we don't)
303 BitmapSizeDifference
= BitmapBytes
- BitmapSize
.QuadPart
;
304 NewBitmapSize
= max(BitmapSize
.QuadPart
+ BitmapSizeDifference
, BitmapSize
.QuadPart
);
306 // Allocate memory for the bitmap
307 BitmapBuffer
= ExAllocatePoolWithTag(NonPagedPool
, NewBitmapSize
, TAG_NTFS
);
310 DPRINT1("ERROR: Unable to allocate memory for bitmap attribute!\n");
311 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
312 ExReleaseResourceLite(&(Vcb
->DirResource
));
313 ReleaseAttributeContext(BitmapContext
);
314 return STATUS_INSUFFICIENT_RESOURCES
;
317 // Zero the bytes we'll be adding
318 RtlZeroMemory(BitmapBuffer
, NewBitmapSize
);
320 // Read the bitmap attribute
321 BytesRead
= ReadAttribute(Vcb
,
326 if (BytesRead
!= BitmapSize
.LowPart
)
328 DPRINT1("ERROR: Bytes read != Bitmap size!\n");
329 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
330 ExReleaseResourceLite(&(Vcb
->DirResource
));
331 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
332 ReleaseAttributeContext(BitmapContext
);
333 return STATUS_INVALID_PARAMETER
;
336 // Increase the mft size
337 Status
= SetNonResidentAttributeDataLength(Vcb
, Vcb
->MFTContext
, Vcb
->MftDataOffset
, Vcb
->MasterFileTable
, &DataSize
);
338 if (!NT_SUCCESS(Status
))
340 DPRINT1("ERROR: Failed to set size of $MFT data attribute!\n");
341 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
342 ExReleaseResourceLite(&(Vcb
->DirResource
));
343 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
344 ReleaseAttributeContext(BitmapContext
);
348 // We'll need to find the bitmap again, because its offset will have changed after resizing the data attribute
349 ReleaseAttributeContext(BitmapContext
);
350 Status
= FindAttribute(Vcb
, Vcb
->MasterFileTable
, AttributeBitmap
, L
"", 0, &BitmapContext
, &BitmapOffset
);
351 if (!NT_SUCCESS(Status
))
353 DPRINT1("ERROR: Couldn't find $BITMAP attribute of Mft!\n");
354 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
355 ExReleaseResourceLite(&(Vcb
->DirResource
));
359 // If the bitmap grew
360 if (BitmapSizeDifference
> 0)
362 // Set the new bitmap size
363 BitmapSize
.QuadPart
= NewBitmapSize
;
364 if (BitmapContext
->pRecord
->IsNonResident
)
365 Status
= SetNonResidentAttributeDataLength(Vcb
, BitmapContext
, BitmapOffset
, Vcb
->MasterFileTable
, &BitmapSize
);
367 Status
= SetResidentAttributeDataLength(Vcb
, BitmapContext
, BitmapOffset
, Vcb
->MasterFileTable
, &BitmapSize
);
369 if (!NT_SUCCESS(Status
))
371 DPRINT1("ERROR: Failed to set size of bitmap attribute!\n");
372 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
373 ExReleaseResourceLite(&(Vcb
->DirResource
));
374 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
375 ReleaseAttributeContext(BitmapContext
);
380 NtfsDumpFileAttributes(Vcb
, Vcb
->MasterFileTable
);
382 // Update the file record with the new attribute sizes
383 Status
= UpdateFileRecord(Vcb
, Vcb
->VolumeFcb
->MFTIndex
, Vcb
->MasterFileTable
);
384 if (!NT_SUCCESS(Status
))
386 DPRINT1("ERROR: Failed to update $MFT file record!\n");
387 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
388 ExReleaseResourceLite(&(Vcb
->DirResource
));
389 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
390 ReleaseAttributeContext(BitmapContext
);
394 // Write out the new bitmap
395 Status
= WriteAttribute(Vcb
, BitmapContext
, 0, BitmapBuffer
, NewBitmapSize
, &LengthWritten
, Vcb
->MasterFileTable
);
396 if (!NT_SUCCESS(Status
))
398 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
399 ExReleaseResourceLite(&(Vcb
->DirResource
));
400 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
401 ReleaseAttributeContext(BitmapContext
);
402 DPRINT1("ERROR: Couldn't write to bitmap attribute of $MFT!\n");
406 // Create blank records for the new file record entries.
407 for (i
= 0; i
< NewRecords
; i
++)
409 Status
= UpdateFileRecord(Vcb
, FirstNewMftIndex
+ i
, BlankFileRecord
);
410 if (!NT_SUCCESS(Status
))
412 DPRINT1("ERROR: Failed to write blank file record!\n");
413 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
414 ExReleaseResourceLite(&(Vcb
->DirResource
));
415 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
416 ReleaseAttributeContext(BitmapContext
);
421 // Update the mft mirror
422 Status
= UpdateMftMirror(Vcb
);
425 ExFreePoolWithTag(BlankFileRecord
, TAG_NTFS
);
426 ExReleaseResourceLite(&(Vcb
->DirResource
));
427 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
428 ReleaseAttributeContext(BitmapContext
);
434 * @name MoveAttributes
437 * Moves a block of attributes to a new location in the file Record. The attribute at FirstAttributeToMove
438 * and every attribute after that will be moved to MoveTo.
441 * Pointer to the DEVICE_EXTENSION (VCB) of the target volume.
443 * @param FirstAttributeToMove
444 * Pointer to the first NTFS_ATTR_RECORD that needs to be moved. This pointer must reside within a file record.
446 * @param FirstAttributeOffset
447 * Offset of FirstAttributeToMove relative to the beginning of the file record.
450 * ULONG_PTR with the memory location that will be the new location of the first attribute being moved.
453 * The new location of the final attribute (i.e. AttributeEnd marker).
456 MoveAttributes(PDEVICE_EXTENSION DeviceExt
,
457 PNTFS_ATTR_RECORD FirstAttributeToMove
,
458 ULONG FirstAttributeOffset
,
461 // Get the size of all attributes after this one
462 ULONG MemBlockSize
= 0;
463 PNTFS_ATTR_RECORD CurrentAttribute
= FirstAttributeToMove
;
464 ULONG CurrentOffset
= FirstAttributeOffset
;
465 PNTFS_ATTR_RECORD FinalAttribute
;
467 while (CurrentAttribute
->Type
!= AttributeEnd
&& CurrentOffset
< DeviceExt
->NtfsInfo
.BytesPerFileRecord
)
469 CurrentOffset
+= CurrentAttribute
->Length
;
470 MemBlockSize
+= CurrentAttribute
->Length
;
471 CurrentAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)CurrentAttribute
+ CurrentAttribute
->Length
);
474 FinalAttribute
= (PNTFS_ATTR_RECORD
)(MoveTo
+ MemBlockSize
);
475 MemBlockSize
+= sizeof(ULONG
) * 2; // Add the AttributeEnd and file record end
477 ASSERT(MemBlockSize
% ATTR_RECORD_ALIGNMENT
== 0);
479 // Move the attributes after this one
480 RtlMoveMemory((PCHAR
)MoveTo
, FirstAttributeToMove
, MemBlockSize
);
482 return FinalAttribute
;
486 InternalSetResidentAttributeLength(PDEVICE_EXTENSION DeviceExt
,
487 PNTFS_ATTR_CONTEXT AttrContext
,
488 PFILE_RECORD_HEADER FileRecord
,
492 PNTFS_ATTR_RECORD Destination
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
493 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Destination
+ Destination
->Length
);
494 PNTFS_ATTR_RECORD FinalAttribute
;
495 ULONG OldAttributeLength
= Destination
->Length
;
496 ULONG NextAttributeOffset
;
498 DPRINT1("InternalSetResidentAttributeLength( %p, %p, %p, %lu, %lu )\n", DeviceExt
, AttrContext
, FileRecord
, AttrOffset
, DataSize
);
500 ASSERT(!AttrContext
->pRecord
->IsNonResident
);
502 // Update ValueLength Field
503 Destination
->Resident
.ValueLength
= DataSize
;
505 // Calculate the record length and end marker offset
506 Destination
->Length
= ALIGN_UP_BY(DataSize
+ AttrContext
->pRecord
->Resident
.ValueOffset
, ATTR_RECORD_ALIGNMENT
);
507 NextAttributeOffset
= AttrOffset
+ Destination
->Length
;
509 // Ensure NextAttributeOffset is aligned to an 8-byte boundary
510 ASSERT(NextAttributeOffset
% ATTR_RECORD_ALIGNMENT
== 0);
512 // Will the new attribute be larger than the old one?
513 if (Destination
->Length
> OldAttributeLength
)
515 // Free the old copy of the attribute in the context, as it will be the wrong length
516 ExFreePoolWithTag(AttrContext
->pRecord
, TAG_NTFS
);
518 // Create a new copy of the attribute record for the context
519 AttrContext
->pRecord
= ExAllocatePoolWithTag(NonPagedPool
, Destination
->Length
, TAG_NTFS
);
520 if (!AttrContext
->pRecord
)
522 DPRINT1("Unable to allocate memory for attribute!\n");
523 return STATUS_INSUFFICIENT_RESOURCES
;
525 RtlZeroMemory((PVOID
)((ULONG_PTR
)AttrContext
->pRecord
+ OldAttributeLength
), Destination
->Length
- OldAttributeLength
);
526 RtlCopyMemory(AttrContext
->pRecord
, Destination
, OldAttributeLength
);
529 // Are there attributes after this one that need to be moved?
530 if (NextAttribute
->Type
!= AttributeEnd
)
532 // Move the attributes after this one
533 FinalAttribute
= MoveAttributes(DeviceExt
, NextAttribute
, NextAttributeOffset
, (ULONG_PTR
)Destination
+ Destination
->Length
);
537 // advance to the final "attribute," adjust for the changed length of the attribute we're resizing
538 FinalAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)NextAttribute
- OldAttributeLength
+ Destination
->Length
);
541 // Update pRecord's length
542 AttrContext
->pRecord
->Length
= Destination
->Length
;
543 AttrContext
->pRecord
->Resident
.ValueLength
= DataSize
;
545 // set the file record end
546 SetFileRecordEnd(FileRecord
, FinalAttribute
, FILE_RECORD_END
);
548 //NtfsDumpFileRecord(DeviceExt, FileRecord);
550 return STATUS_SUCCESS
;
554 * @parameter FileRecord
555 * Pointer to a file record. Must be a full record at least
556 * Fcb->Vcb->NtfsInfo.BytesPerFileRecord bytes large, not just the header.
559 SetAttributeDataLength(PFILE_OBJECT FileObject
,
561 PNTFS_ATTR_CONTEXT AttrContext
,
563 PFILE_RECORD_HEADER FileRecord
,
564 PLARGE_INTEGER DataSize
)
566 NTSTATUS Status
= STATUS_SUCCESS
;
568 DPRINT1("SetAttributeDataLength(%p, %p, %p, %lu, %p, %I64u)\n",
576 // are we truncating the file?
577 if (DataSize
->QuadPart
< AttributeDataLength(AttrContext
->pRecord
))
579 if (!MmCanFileBeTruncated(FileObject
->SectionObjectPointer
, DataSize
))
581 DPRINT1("Can't truncate a memory-mapped file!\n");
582 return STATUS_USER_MAPPED_FILE
;
586 if (AttrContext
->pRecord
->IsNonResident
)
588 Status
= SetNonResidentAttributeDataLength(Fcb
->Vcb
,
596 // resident attribute
597 Status
= SetResidentAttributeDataLength(Fcb
->Vcb
,
604 if (!NT_SUCCESS(Status
))
606 DPRINT1("ERROR: Failed to set size of attribute!\n");
610 //NtfsDumpFileAttributes(Fcb->Vcb, FileRecord);
612 // write the updated file record back to disk
613 Status
= UpdateFileRecord(Fcb
->Vcb
, Fcb
->MFTIndex
, FileRecord
);
615 if (NT_SUCCESS(Status
))
617 if (AttrContext
->pRecord
->IsNonResident
)
618 Fcb
->RFCB
.AllocationSize
.QuadPart
= AttrContext
->pRecord
->NonResident
.AllocatedSize
;
620 Fcb
->RFCB
.AllocationSize
= *DataSize
;
621 Fcb
->RFCB
.FileSize
= *DataSize
;
622 Fcb
->RFCB
.ValidDataLength
= *DataSize
;
623 CcSetFileSizes(FileObject
, (PCC_FILE_SIZES
)&Fcb
->RFCB
.AllocationSize
);
626 return STATUS_SUCCESS
;
630 * @name SetFileRecordEnd
633 * This small function sets a new endpoint for the file record. It set's the final
634 * AttrEnd->Type to AttributeEnd and recalculates the bytes used by the file record.
637 * Pointer to the file record whose endpoint (length) will be set.
640 * Pointer to section of memory that will receive the AttributeEnd marker. This must point
641 * to memory allocated for the FileRecord. Must be aligned to an 8-byte boundary (relative to FileRecord).
644 * This value will be written after AttributeEnd but isn't critical at all. When Windows resizes
645 * a file record, it preserves the final ULONG that previously ended the record, even though this
646 * value is (to my knowledge) never used. We emulate this behavior.
650 SetFileRecordEnd(PFILE_RECORD_HEADER FileRecord
,
651 PNTFS_ATTR_RECORD AttrEnd
,
654 // Ensure AttrEnd is aligned on an 8-byte boundary, relative to FileRecord
655 ASSERT(((ULONG_PTR
)AttrEnd
- (ULONG_PTR
)FileRecord
) % ATTR_RECORD_ALIGNMENT
== 0);
657 // mark the end of attributes
658 AttrEnd
->Type
= AttributeEnd
;
660 // Restore the "file-record-end marker." The value is never checked but this behavior is consistent with Win2k3.
661 AttrEnd
->Length
= EndMarker
;
663 // recalculate bytes in use
664 FileRecord
->BytesInUse
= (ULONG_PTR
)AttrEnd
- (ULONG_PTR
)FileRecord
+ sizeof(ULONG
) * 2;
668 * @name SetNonResidentAttributeDataLength
671 * Called by SetAttributeDataLength() to set the size of a non-resident attribute. Doesn't update the file record.
674 * Pointer to a DEVICE_EXTENSION describing the target disk.
677 * PNTFS_ATTR_CONTEXT describing the location of the attribute whose size is being set.
680 * Offset, from the beginning of the record, of the attribute being sized.
683 * Pointer to a file record containing the attribute to be resized. Must be a complete file record,
684 * not just the header.
687 * Pointer to a LARGE_INTEGER describing the new size of the attribute's data.
690 * STATUS_SUCCESS on success;
691 * STATUS_INSUFFICIENT_RESOURCES if an allocation fails.
692 * STATUS_INVALID_PARAMETER if we can't find the last cluster in the data run.
695 * Called by SetAttributeDataLength() and IncreaseMftSize(). Use SetAttributeDataLength() unless you have a good
696 * reason to use this. Doesn't update the file record on disk. Doesn't inform the cache controller of changes with
697 * any associated files. Synchronization is the callers responsibility.
700 SetNonResidentAttributeDataLength(PDEVICE_EXTENSION Vcb
,
701 PNTFS_ATTR_CONTEXT AttrContext
,
703 PFILE_RECORD_HEADER FileRecord
,
704 PLARGE_INTEGER DataSize
)
706 NTSTATUS Status
= STATUS_SUCCESS
;
707 ULONG BytesPerCluster
= Vcb
->NtfsInfo
.BytesPerCluster
;
708 ULONGLONG AllocationSize
= ROUND_UP(DataSize
->QuadPart
, BytesPerCluster
);
709 PNTFS_ATTR_RECORD DestinationAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
710 ULONG ExistingClusters
= AttrContext
->pRecord
->NonResident
.AllocatedSize
/ BytesPerCluster
;
712 ASSERT(AttrContext
->pRecord
->IsNonResident
);
714 // do we need to increase the allocation size?
715 if (AttrContext
->pRecord
->NonResident
.AllocatedSize
< AllocationSize
)
717 ULONG ClustersNeeded
= (AllocationSize
/ BytesPerCluster
) - ExistingClusters
;
718 LARGE_INTEGER LastClusterInDataRun
;
719 ULONG NextAssignedCluster
;
720 ULONG AssignedClusters
;
722 if (ExistingClusters
== 0)
724 LastClusterInDataRun
.QuadPart
= 0;
728 if (!FsRtlLookupLargeMcbEntry(&AttrContext
->DataRunsMCB
,
729 (LONGLONG
)AttrContext
->pRecord
->NonResident
.HighestVCN
,
730 (PLONGLONG
)&LastClusterInDataRun
.QuadPart
,
736 DPRINT1("Error looking up final large MCB entry!\n");
738 // Most likely, HighestVCN went above the largest mapping
739 DPRINT1("Highest VCN of record: %I64u\n", AttrContext
->pRecord
->NonResident
.HighestVCN
);
740 return STATUS_INVALID_PARAMETER
;
744 DPRINT("LastClusterInDataRun: %I64u\n", LastClusterInDataRun
.QuadPart
);
745 DPRINT("Highest VCN of record: %I64u\n", AttrContext
->pRecord
->NonResident
.HighestVCN
);
747 while (ClustersNeeded
> 0)
749 Status
= NtfsAllocateClusters(Vcb
,
750 LastClusterInDataRun
.LowPart
+ 1,
752 &NextAssignedCluster
,
755 if (!NT_SUCCESS(Status
))
757 DPRINT1("Error: Unable to allocate requested clusters!\n");
761 // now we need to add the clusters we allocated to the data run
762 Status
= AddRun(Vcb
, AttrContext
, AttrOffset
, FileRecord
, NextAssignedCluster
, AssignedClusters
);
763 if (!NT_SUCCESS(Status
))
765 DPRINT1("Error: Unable to add data run!\n");
769 ClustersNeeded
-= AssignedClusters
;
770 LastClusterInDataRun
.LowPart
= NextAssignedCluster
+ AssignedClusters
- 1;
773 else if (AttrContext
->pRecord
->NonResident
.AllocatedSize
> AllocationSize
)
775 // shrink allocation size
776 ULONG ClustersToFree
= ExistingClusters
- (AllocationSize
/ BytesPerCluster
);
777 Status
= FreeClusters(Vcb
, AttrContext
, AttrOffset
, FileRecord
, ClustersToFree
);
780 // TODO: is the file compressed, encrypted, or sparse?
782 AttrContext
->pRecord
->NonResident
.AllocatedSize
= AllocationSize
;
783 AttrContext
->pRecord
->NonResident
.DataSize
= DataSize
->QuadPart
;
784 AttrContext
->pRecord
->NonResident
.InitializedSize
= DataSize
->QuadPart
;
786 DestinationAttribute
->NonResident
.AllocatedSize
= AllocationSize
;
787 DestinationAttribute
->NonResident
.DataSize
= DataSize
->QuadPart
;
788 DestinationAttribute
->NonResident
.InitializedSize
= DataSize
->QuadPart
;
790 // HighestVCN seems to be set incorrectly somewhere. Apply a hack-fix to reset it.
791 // HACKHACK FIXME: Fix for sparse files; this math won't work in that case.
792 AttrContext
->pRecord
->NonResident
.HighestVCN
= ((ULONGLONG
)AllocationSize
/ Vcb
->NtfsInfo
.BytesPerCluster
) - 1;
793 DestinationAttribute
->NonResident
.HighestVCN
= AttrContext
->pRecord
->NonResident
.HighestVCN
;
795 DPRINT("Allocated Size: %I64u\n", DestinationAttribute
->NonResident
.AllocatedSize
);
801 * @name SetResidentAttributeDataLength
804 * Called by SetAttributeDataLength() to set the size of a non-resident attribute. Doesn't update the file record.
807 * Pointer to a DEVICE_EXTENSION describing the target disk.
810 * PNTFS_ATTR_CONTEXT describing the location of the attribute whose size is being set.
813 * Offset, from the beginning of the record, of the attribute being sized.
816 * Pointer to a file record containing the attribute to be resized. Must be a complete file record,
817 * not just the header.
820 * Pointer to a LARGE_INTEGER describing the new size of the attribute's data.
823 * STATUS_SUCCESS on success;
824 * STATUS_INSUFFICIENT_RESOURCES if an allocation fails.
825 * STATUS_INVALID_PARAMETER if AttrContext describes a non-resident attribute.
826 * STATUS_NOT_IMPLEMENTED if requested to decrease the size of an attribute that isn't the
827 * last attribute listed in the file record.
830 * Called by SetAttributeDataLength() and IncreaseMftSize(). Use SetAttributeDataLength() unless you have a good
831 * reason to use this. Doesn't update the file record on disk. Doesn't inform the cache controller of changes with
832 * any associated files. Synchronization is the callers responsibility.
835 SetResidentAttributeDataLength(PDEVICE_EXTENSION Vcb
,
836 PNTFS_ATTR_CONTEXT AttrContext
,
838 PFILE_RECORD_HEADER FileRecord
,
839 PLARGE_INTEGER DataSize
)
843 // find the next attribute
844 ULONG NextAttributeOffset
= AttrOffset
+ AttrContext
->pRecord
->Length
;
845 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((PCHAR
)FileRecord
+ NextAttributeOffset
);
847 ASSERT(!AttrContext
->pRecord
->IsNonResident
);
849 //NtfsDumpFileAttributes(Vcb, FileRecord);
851 // Do we need to increase the data length?
852 if (DataSize
->QuadPart
> AttrContext
->pRecord
->Resident
.ValueLength
)
854 // There's usually padding at the end of a record. Do we need to extend past it?
855 ULONG MaxValueLength
= AttrContext
->pRecord
->Length
- AttrContext
->pRecord
->Resident
.ValueOffset
;
856 if (MaxValueLength
< DataSize
->LowPart
)
858 // If this is the last attribute, we could move the end marker to the very end of the file record
859 MaxValueLength
+= Vcb
->NtfsInfo
.BytesPerFileRecord
- NextAttributeOffset
- (sizeof(ULONG
) * 2);
861 if (MaxValueLength
< DataSize
->LowPart
|| NextAttribute
->Type
!= AttributeEnd
)
863 // convert attribute to non-resident
864 PNTFS_ATTR_RECORD Destination
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
865 PNTFS_ATTR_RECORD NewRecord
;
866 LARGE_INTEGER AttribDataSize
;
868 ULONG NewRecordLength
;
869 ULONG EndAttributeOffset
;
872 DPRINT1("Converting attribute to non-resident.\n");
874 AttribDataSize
.QuadPart
= AttrContext
->pRecord
->Resident
.ValueLength
;
876 // Is there existing data we need to back-up?
877 if (AttribDataSize
.QuadPart
> 0)
879 AttribData
= ExAllocatePoolWithTag(NonPagedPool
, AttribDataSize
.QuadPart
, TAG_NTFS
);
880 if (AttribData
== NULL
)
882 DPRINT1("ERROR: Couldn't allocate memory for attribute data. Can't migrate to non-resident!\n");
883 return STATUS_INSUFFICIENT_RESOURCES
;
886 // read data to temp buffer
887 Status
= ReadAttribute(Vcb
, AttrContext
, 0, AttribData
, AttribDataSize
.QuadPart
);
888 if (!NT_SUCCESS(Status
))
890 DPRINT1("ERROR: Unable to read attribute before migrating!\n");
891 ExFreePoolWithTag(AttribData
, TAG_NTFS
);
896 // Start by turning this attribute into a 0-length, non-resident attribute, then enlarge it.
898 // The size of a 0-length, non-resident attribute will be 0x41 + the size of the attribute name, aligned to an 8-byte boundary
899 NewRecordLength
= ALIGN_UP_BY(0x41 + (AttrContext
->pRecord
->NameLength
* sizeof(WCHAR
)), ATTR_RECORD_ALIGNMENT
);
901 // Create a new attribute record that will store the 0-length, non-resident attribute
902 NewRecord
= ExAllocatePoolWithTag(NonPagedPool
, NewRecordLength
, TAG_NTFS
);
904 // Zero out the NonResident structure
905 RtlZeroMemory(NewRecord
, NewRecordLength
);
907 // Copy the data that's common to both non-resident and resident attributes
908 RtlCopyMemory(NewRecord
, AttrContext
->pRecord
, FIELD_OFFSET(NTFS_ATTR_RECORD
, Resident
.ValueLength
));
911 if (AttrContext
->pRecord
->NameLength
!= 0)
914 // An attribute name will be located at offset 0x18 for a resident attribute, 0x40 for non-resident
915 RtlCopyMemory((PCHAR
)((ULONG_PTR
)NewRecord
+ 0x40),
916 (PCHAR
)((ULONG_PTR
)AttrContext
->pRecord
+ 0x18),
917 AttrContext
->pRecord
->NameLength
* sizeof(WCHAR
));
920 // update the mapping pairs offset, which will be 0x40 (size of a non-resident header) + length in bytes of the name
921 NewRecord
->NonResident
.MappingPairsOffset
= 0x40 + (AttrContext
->pRecord
->NameLength
* sizeof(WCHAR
));
923 // update the end of the file record
924 // calculate position of end markers (1 byte for empty data run)
925 EndAttributeOffset
= AttrOffset
+ NewRecord
->NonResident
.MappingPairsOffset
+ 1;
926 EndAttributeOffset
= ALIGN_UP_BY(EndAttributeOffset
, ATTR_RECORD_ALIGNMENT
);
929 NewRecord
->Length
= EndAttributeOffset
- AttrOffset
;
931 ASSERT(NewRecord
->Length
== NewRecordLength
);
933 // Copy the new attribute record into the file record
934 RtlCopyMemory(Destination
, NewRecord
, NewRecord
->Length
);
936 // Update the file record end
937 SetFileRecordEnd(FileRecord
,
938 (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ EndAttributeOffset
),
941 // Initialize the MCB, potentially catch an exception
944 FsRtlInitializeLargeMcb(&AttrContext
->DataRunsMCB
, NonPagedPool
);
946 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
948 DPRINT1("Unable to create LargeMcb!\n");
949 if (AttribDataSize
.QuadPart
> 0)
950 ExFreePoolWithTag(AttribData
, TAG_NTFS
);
951 _SEH2_YIELD(return _SEH2_GetExceptionCode());
954 // Mark the attribute as non-resident (we wait until after we know the LargeMcb was initialized)
955 NewRecord
->IsNonResident
= Destination
->IsNonResident
= 1;
957 // Update file record on disk
958 Status
= UpdateFileRecord(Vcb
, AttrContext
->FileMFTIndex
, FileRecord
);
959 if (!NT_SUCCESS(Status
))
961 DPRINT1("ERROR: Couldn't update file record to continue migration!\n");
962 if (AttribDataSize
.QuadPart
> 0)
963 ExFreePoolWithTag(AttribData
, TAG_NTFS
);
967 // Now we need to free the old copy of the attribute record in the context and replace it with the new one
968 ExFreePoolWithTag(AttrContext
->pRecord
, TAG_NTFS
);
969 AttrContext
->pRecord
= NewRecord
;
971 // Now we can treat the attribute as non-resident and enlarge it normally
972 Status
= SetNonResidentAttributeDataLength(Vcb
, AttrContext
, AttrOffset
, FileRecord
, DataSize
);
973 if (!NT_SUCCESS(Status
))
975 DPRINT1("ERROR: Unable to migrate resident attribute!\n");
976 if (AttribDataSize
.QuadPart
> 0)
977 ExFreePoolWithTag(AttribData
, TAG_NTFS
);
981 // restore the back-up attribute, if we made one
982 if (AttribDataSize
.QuadPart
> 0)
984 Status
= WriteAttribute(Vcb
, AttrContext
, 0, AttribData
, AttribDataSize
.QuadPart
, &LengthWritten
, FileRecord
);
985 if (!NT_SUCCESS(Status
))
987 DPRINT1("ERROR: Unable to write attribute data to non-resident clusters during migration!\n");
988 // TODO: Reverse migration so no data is lost
989 ExFreePoolWithTag(AttribData
, TAG_NTFS
);
993 ExFreePoolWithTag(AttribData
, TAG_NTFS
);
999 // set the new length of the resident attribute (if we didn't migrate it)
1000 if (!AttrContext
->pRecord
->IsNonResident
)
1001 return InternalSetResidentAttributeLength(Vcb
, AttrContext
, FileRecord
, AttrOffset
, DataSize
->LowPart
);
1003 return STATUS_SUCCESS
;
1007 ReadAttribute(PDEVICE_EXTENSION Vcb
,
1008 PNTFS_ATTR_CONTEXT Context
,
1015 LONGLONG DataRunOffset
;
1016 ULONGLONG DataRunLength
;
1017 LONGLONG DataRunStartLCN
;
1018 ULONGLONG CurrentOffset
;
1026 if (!Context
->pRecord
->IsNonResident
)
1028 // We need to truncate Offset to a ULONG for pointer arithmetic
1029 // The check below should ensure that Offset is well within the range of 32 bits
1030 ULONG LittleOffset
= (ULONG
)Offset
;
1032 // Ensure that offset isn't beyond the end of the attribute
1033 if (Offset
> Context
->pRecord
->Resident
.ValueLength
)
1035 if (Offset
+ Length
> Context
->pRecord
->Resident
.ValueLength
)
1036 Length
= (ULONG
)(Context
->pRecord
->Resident
.ValueLength
- Offset
);
1038 RtlCopyMemory(Buffer
, (PVOID
)((ULONG_PTR
)Context
->pRecord
+ Context
->pRecord
->Resident
.ValueOffset
+ LittleOffset
), Length
);
1043 * Non-resident attribute
1047 * I. Find the corresponding start data run.
1052 // FIXME: Cache seems to be non-working. Disable it for now
1053 //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
1056 DataRun
= Context
->CacheRun
;
1057 LastLCN
= Context
->CacheRunLastLCN
;
1058 DataRunStartLCN
= Context
->CacheRunStartLCN
;
1059 DataRunLength
= Context
->CacheRunLength
;
1060 CurrentOffset
= Context
->CacheRunCurrentOffset
;
1065 ULONG UsedBufferSize
;
1066 TempBuffer
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
1071 // This will be rewritten in the next iteration to just use the DataRuns MCB directly
1072 ConvertLargeMCBToDataRuns(&Context
->DataRunsMCB
,
1074 Vcb
->NtfsInfo
.BytesPerFileRecord
,
1077 DataRun
= TempBuffer
;
1081 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1082 if (DataRunOffset
!= -1)
1084 /* Normal data run. */
1085 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1086 LastLCN
= DataRunStartLCN
;
1090 /* Sparse data run. */
1091 DataRunStartLCN
= -1;
1094 if (Offset
>= CurrentOffset
&&
1095 Offset
< CurrentOffset
+ (DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
))
1105 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
1110 * II. Go through the run list and read the data
1113 ReadLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
), Length
);
1114 if (DataRunStartLCN
== -1)
1116 RtlZeroMemory(Buffer
, ReadLength
);
1117 Status
= STATUS_SUCCESS
;
1121 Status
= NtfsReadDisk(Vcb
->StorageDevice
,
1122 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
+ Offset
- CurrentOffset
,
1124 Vcb
->NtfsInfo
.BytesPerSector
,
1128 if (NT_SUCCESS(Status
))
1130 Length
-= ReadLength
;
1131 Buffer
+= ReadLength
;
1132 AlreadyRead
+= ReadLength
;
1134 if (ReadLength
== DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
))
1136 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
1137 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1138 if (DataRunOffset
!= (ULONGLONG
)-1)
1140 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1141 LastLCN
= DataRunStartLCN
;
1144 DataRunStartLCN
= -1;
1149 ReadLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
, Length
);
1150 if (DataRunStartLCN
== -1)
1151 RtlZeroMemory(Buffer
, ReadLength
);
1154 Status
= NtfsReadDisk(Vcb
->StorageDevice
,
1155 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
,
1157 Vcb
->NtfsInfo
.BytesPerSector
,
1160 if (!NT_SUCCESS(Status
))
1164 Length
-= ReadLength
;
1165 Buffer
+= ReadLength
;
1166 AlreadyRead
+= ReadLength
;
1168 /* We finished this request, but there still data in this data run. */
1169 if (Length
== 0 && ReadLength
!= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
)
1173 * Go to next run in the list.
1178 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
1179 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1180 if (DataRunOffset
!= -1)
1182 /* Normal data run. */
1183 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1184 LastLCN
= DataRunStartLCN
;
1188 /* Sparse data run. */
1189 DataRunStartLCN
= -1;
1196 if (Context
->pRecord
->IsNonResident
)
1197 ExFreePoolWithTag(TempBuffer
, TAG_NTFS
);
1199 Context
->CacheRun
= DataRun
;
1200 Context
->CacheRunOffset
= Offset
+ AlreadyRead
;
1201 Context
->CacheRunStartLCN
= DataRunStartLCN
;
1202 Context
->CacheRunLength
= DataRunLength
;
1203 Context
->CacheRunLastLCN
= LastLCN
;
1204 Context
->CacheRunCurrentOffset
= CurrentOffset
;
1211 * @name WriteAttribute
1214 * Writes an NTFS attribute to the disk. It presently borrows a lot of code from ReadAttribute(),
1215 * and it still needs more documentation / cleaning up.
1218 * Volume Control Block indicating which volume to write the attribute to
1221 * Pointer to an NTFS_ATTR_CONTEXT that has information about the attribute
1224 * Offset, in bytes, from the beginning of the attribute indicating where to start
1228 * The data that's being written to the device
1231 * How much data will be written, in bytes
1233 * @param RealLengthWritten
1234 * Pointer to a ULONG which will receive how much data was written, in bytes
1237 * Optional pointer to a FILE_RECORD_HEADER that contains a copy of the file record
1238 * being written to. Can be NULL, in which case the file record will be read from disk.
1239 * If not-null, WriteAttribute() will skip reading from disk, and FileRecord
1240 * will be updated with the newly-written attribute before the function returns.
1243 * STATUS_SUCCESS if successful, an error code otherwise. STATUS_NOT_IMPLEMENTED if
1244 * writing to a sparse file.
1246 * @remarks Note that in this context the word "attribute" isn't referring read-only, hidden,
1247 * etc. - the file's data is actually stored in an attribute in NTFS parlance.
1252 WriteAttribute(PDEVICE_EXTENSION Vcb
,
1253 PNTFS_ATTR_CONTEXT Context
,
1255 const PUCHAR Buffer
,
1257 PULONG RealLengthWritten
,
1258 PFILE_RECORD_HEADER FileRecord
)
1262 LONGLONG DataRunOffset
;
1263 ULONGLONG DataRunLength
;
1264 LONGLONG DataRunStartLCN
;
1265 ULONGLONG CurrentOffset
;
1268 PUCHAR SourceBuffer
= Buffer
;
1269 LONGLONG StartingOffset
;
1270 BOOLEAN FileRecordAllocated
= FALSE
;
1276 DPRINT("WriteAttribute(%p, %p, %I64u, %p, %lu, %p, %p)\n", Vcb
, Context
, Offset
, Buffer
, Length
, RealLengthWritten
, FileRecord
);
1278 *RealLengthWritten
= 0;
1280 // is this a resident attribute?
1281 if (!Context
->pRecord
->IsNonResident
)
1283 ULONG AttributeOffset
;
1284 PNTFS_ATTR_CONTEXT FoundContext
;
1285 PNTFS_ATTR_RECORD Destination
;
1287 // Ensure requested data is within the bounds of the attribute
1288 ASSERT(Offset
+ Length
<= Context
->pRecord
->Resident
.ValueLength
);
1290 if (Offset
+ Length
> Context
->pRecord
->Resident
.ValueLength
)
1292 DPRINT1("DRIVER ERROR: Attribute is too small!\n");
1293 return STATUS_INVALID_PARAMETER
;
1296 // Do we need to read the file record?
1297 if (FileRecord
== NULL
)
1299 FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
1302 DPRINT1("Error: Couldn't allocate file record!\n");
1303 return STATUS_NO_MEMORY
;
1306 FileRecordAllocated
= TRUE
;
1308 // read the file record
1309 ReadFileRecord(Vcb
, Context
->FileMFTIndex
, FileRecord
);
1312 // find where to write the attribute data to
1313 Status
= FindAttribute(Vcb
, FileRecord
,
1314 Context
->pRecord
->Type
,
1315 (PCWSTR
)((ULONG_PTR
)Context
->pRecord
+ Context
->pRecord
->NameOffset
),
1316 Context
->pRecord
->NameLength
,
1320 if (!NT_SUCCESS(Status
))
1322 DPRINT1("ERROR: Couldn't find matching attribute!\n");
1323 if(FileRecordAllocated
)
1324 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
1328 Destination
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttributeOffset
);
1330 DPRINT("Offset: %I64u, AttributeOffset: %u, ValueOffset: %u\n", Offset
, AttributeOffset
, Context
->pRecord
->Resident
.ValueLength
);
1332 // Will we be writing past the end of the allocated file record?
1333 if (Offset
+ Length
+ AttributeOffset
+ Context
->pRecord
->Resident
.ValueOffset
> Vcb
->NtfsInfo
.BytesPerFileRecord
)
1335 DPRINT1("DRIVER ERROR: Data being written extends past end of file record!\n");
1336 ReleaseAttributeContext(FoundContext
);
1337 if (FileRecordAllocated
)
1338 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
1339 return STATUS_INVALID_PARAMETER
;
1342 // copy the data being written into the file record. We cast Offset to ULONG, which is safe because it's range has been verified.
1343 RtlCopyMemory((PCHAR
)((ULONG_PTR
)Destination
+ Context
->pRecord
->Resident
.ValueOffset
+ (ULONG
)Offset
), Buffer
, Length
);
1345 Status
= UpdateFileRecord(Vcb
, Context
->FileMFTIndex
, FileRecord
);
1347 // Update the context's copy of the resident attribute
1348 ASSERT(Context
->pRecord
->Length
== Destination
->Length
);
1349 RtlCopyMemory((PVOID
)Context
->pRecord
, Destination
, Context
->pRecord
->Length
);
1351 ReleaseAttributeContext(FoundContext
);
1352 if (FileRecordAllocated
)
1353 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
1355 if (NT_SUCCESS(Status
))
1356 *RealLengthWritten
= Length
;
1361 // This is a non-resident attribute.
1363 // I. Find the corresponding start data run.
1365 // FIXME: Cache seems to be non-working. Disable it for now
1366 //if(Context->CacheRunOffset <= Offset && Offset < Context->CacheRunOffset + Context->CacheRunLength * Volume->ClusterSize)
1369 DataRun = Context->CacheRun;
1370 LastLCN = Context->CacheRunLastLCN;
1371 DataRunStartLCN = Context->CacheRunStartLCN;
1372 DataRunLength = Context->CacheRunLength;
1373 CurrentOffset = Context->CacheRunCurrentOffset;
1377 ULONG UsedBufferSize
;
1381 // This will be rewritten in the next iteration to just use the DataRuns MCB directly
1382 TempBuffer
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
1384 ConvertLargeMCBToDataRuns(&Context
->DataRunsMCB
,
1386 Vcb
->NtfsInfo
.BytesPerFileRecord
,
1389 DataRun
= TempBuffer
;
1393 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1394 if (DataRunOffset
!= -1)
1397 // DPRINT1("Writing to normal data run, LastLCN %I64u DataRunOffset %I64d\n", LastLCN, DataRunOffset);
1398 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1399 LastLCN
= DataRunStartLCN
;
1403 // Sparse data run. We can't support writing to sparse files yet
1404 // (it may require increasing the allocation size).
1405 DataRunStartLCN
= -1;
1406 DPRINT1("FIXME: Writing to sparse files is not supported yet!\n");
1407 return STATUS_NOT_IMPLEMENTED
;
1410 // Have we reached the data run we're trying to write to?
1411 if (Offset
>= CurrentOffset
&&
1412 Offset
< CurrentOffset
+ (DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
))
1419 // We reached the last assigned cluster
1420 // TODO: assign new clusters to the end of the file.
1421 // (Presently, this code will rarely be reached, the write will usually have already failed by now)
1422 // [We can reach here by creating a new file record when the MFT isn't large enough]
1423 DPRINT1("FIXME: Master File Table needs to be enlarged.\n");
1424 return STATUS_END_OF_FILE
;
1427 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
1431 // II. Go through the run list and write the data
1433 /* REVIEWME -- As adapted from NtfsReadAttribute():
1434 We seem to be making a special case for the first applicable data run, but I'm not sure why.
1435 Does it have something to do with (not) caching? Is this strategy equally applicable to writing? */
1437 WriteLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
), Length
);
1439 StartingOffset
= DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
+ Offset
- CurrentOffset
;
1441 // Write the data to the disk
1442 Status
= NtfsWriteDisk(Vcb
->StorageDevice
,
1445 Vcb
->NtfsInfo
.BytesPerSector
,
1446 (PVOID
)SourceBuffer
);
1448 // Did the write fail?
1449 if (!NT_SUCCESS(Status
))
1451 Context
->CacheRun
= DataRun
;
1452 Context
->CacheRunOffset
= Offset
;
1453 Context
->CacheRunStartLCN
= DataRunStartLCN
;
1454 Context
->CacheRunLength
= DataRunLength
;
1455 Context
->CacheRunLastLCN
= LastLCN
;
1456 Context
->CacheRunCurrentOffset
= CurrentOffset
;
1461 Length
-= WriteLength
;
1462 SourceBuffer
+= WriteLength
;
1463 *RealLengthWritten
+= WriteLength
;
1465 // Did we write to the end of the data run?
1466 if (WriteLength
== DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
- (Offset
- CurrentOffset
))
1468 // Advance to the next data run
1469 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
1470 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1472 if (DataRunOffset
!= (ULONGLONG
)-1)
1474 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1475 LastLCN
= DataRunStartLCN
;
1478 DataRunStartLCN
= -1;
1481 // Do we have more data to write?
1484 // Make sure we don't write past the end of the current data run
1485 WriteLength
= (ULONG
)min(DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
, Length
);
1487 // Are we dealing with a sparse data run?
1488 if (DataRunStartLCN
== -1)
1490 DPRINT1("FIXME: Don't know how to write to sparse files yet! (DataRunStartLCN == -1)\n");
1491 return STATUS_NOT_IMPLEMENTED
;
1495 // write the data to the disk
1496 Status
= NtfsWriteDisk(Vcb
->StorageDevice
,
1497 DataRunStartLCN
* Vcb
->NtfsInfo
.BytesPerCluster
,
1499 Vcb
->NtfsInfo
.BytesPerSector
,
1500 (PVOID
)SourceBuffer
);
1501 if (!NT_SUCCESS(Status
))
1505 Length
-= WriteLength
;
1506 SourceBuffer
+= WriteLength
;
1507 *RealLengthWritten
+= WriteLength
;
1509 // We finished this request, but there's still data in this data run.
1510 if (Length
== 0 && WriteLength
!= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
)
1513 // Go to next run in the list.
1517 // that was the last run
1520 // Failed sanity check.
1521 DPRINT1("Encountered EOF before expected!\n");
1522 return STATUS_END_OF_FILE
;
1528 // Advance to the next data run
1529 CurrentOffset
+= DataRunLength
* Vcb
->NtfsInfo
.BytesPerCluster
;
1530 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1531 if (DataRunOffset
!= -1)
1534 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1535 LastLCN
= DataRunStartLCN
;
1540 DataRunStartLCN
= -1;
1542 } // end while (Length > 0) [more data to write]
1545 if (Context
->pRecord
->IsNonResident
)
1546 ExFreePoolWithTag(TempBuffer
, TAG_NTFS
);
1548 Context
->CacheRun
= DataRun
;
1549 Context
->CacheRunOffset
= Offset
+ *RealLengthWritten
;
1550 Context
->CacheRunStartLCN
= DataRunStartLCN
;
1551 Context
->CacheRunLength
= DataRunLength
;
1552 Context
->CacheRunLastLCN
= LastLCN
;
1553 Context
->CacheRunCurrentOffset
= CurrentOffset
;
1559 ReadFileRecord(PDEVICE_EXTENSION Vcb
,
1561 PFILE_RECORD_HEADER file
)
1563 ULONGLONG BytesRead
;
1565 DPRINT("ReadFileRecord(%p, %I64x, %p)\n", Vcb
, index
, file
);
1567 BytesRead
= ReadAttribute(Vcb
, Vcb
->MFTContext
, index
* Vcb
->NtfsInfo
.BytesPerFileRecord
, (PCHAR
)file
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
1568 if (BytesRead
!= Vcb
->NtfsInfo
.BytesPerFileRecord
)
1570 DPRINT1("ReadFileRecord failed: %I64u read, %lu expected\n", BytesRead
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
1571 return STATUS_PARTIAL_COPY
;
1574 /* Apply update sequence array fixups. */
1575 DPRINT("Sequence number: %u\n", file
->SequenceNumber
);
1576 return FixupUpdateSequenceArray(Vcb
, &file
->Ntfs
);
1581 * Searches a file's parent directory (given the parent's index in the mft)
1582 * for the given file. Upon finding an index entry for that file, updates
1583 * Data Size and Allocated Size values in the $FILE_NAME attribute of that entry.
1585 * (Most of this code was copied from NtfsFindMftRecord)
1588 UpdateFileNameRecord(PDEVICE_EXTENSION Vcb
,
1589 ULONGLONG ParentMFTIndex
,
1590 PUNICODE_STRING FileName
,
1592 ULONGLONG NewDataSize
,
1593 ULONGLONG NewAllocationSize
,
1594 BOOLEAN CaseSensitive
)
1596 PFILE_RECORD_HEADER MftRecord
;
1597 PNTFS_ATTR_CONTEXT IndexRootCtx
;
1598 PINDEX_ROOT_ATTRIBUTE IndexRoot
;
1600 PINDEX_ENTRY_ATTRIBUTE IndexEntry
, IndexEntryEnd
;
1602 ULONG CurrentEntry
= 0;
1604 DPRINT("UpdateFileNameRecord(%p, %I64d, %wZ, %s, %I64u, %I64u, %s)\n",
1608 DirSearch
? "TRUE" : "FALSE",
1611 CaseSensitive
? "TRUE" : "FALSE");
1613 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
1614 Vcb
->NtfsInfo
.BytesPerFileRecord
,
1616 if (MftRecord
== NULL
)
1618 return STATUS_INSUFFICIENT_RESOURCES
;
1621 Status
= ReadFileRecord(Vcb
, ParentMFTIndex
, MftRecord
);
1622 if (!NT_SUCCESS(Status
))
1624 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1628 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
1629 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexRoot
, L
"$I30", 4, &IndexRootCtx
, NULL
);
1630 if (!NT_SUCCESS(Status
))
1632 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1636 IndexRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerIndexRecord
, TAG_NTFS
);
1637 if (IndexRecord
== NULL
)
1639 ReleaseAttributeContext(IndexRootCtx
);
1640 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1641 return STATUS_INSUFFICIENT_RESOURCES
;
1644 Status
= ReadAttribute(Vcb
, IndexRootCtx
, 0, IndexRecord
, AttributeDataLength(IndexRootCtx
->pRecord
));
1645 if (!NT_SUCCESS(Status
))
1647 DPRINT1("ERROR: Failed to read Index Root!\n");
1648 ExFreePoolWithTag(IndexRecord
, TAG_NTFS
);
1649 ReleaseAttributeContext(IndexRootCtx
);
1650 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1653 IndexRoot
= (PINDEX_ROOT_ATTRIBUTE
)IndexRecord
;
1654 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)&IndexRoot
->Header
+ IndexRoot
->Header
.FirstEntryOffset
);
1655 // Index root is always resident.
1656 IndexEntryEnd
= (PINDEX_ENTRY_ATTRIBUTE
)(IndexRecord
+ IndexRoot
->Header
.TotalSizeOfEntries
);
1658 DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb
->NtfsInfo
.BytesPerIndexRecord
, IndexRoot
->SizeOfEntry
);
1660 Status
= UpdateIndexEntryFileNameSize(Vcb
,
1663 IndexRoot
->SizeOfEntry
,
1674 if (Status
== STATUS_PENDING
)
1676 // we need to write the index root attribute back to disk
1677 ULONG LengthWritten
;
1678 Status
= WriteAttribute(Vcb
, IndexRootCtx
, 0, (PUCHAR
)IndexRecord
, AttributeDataLength(IndexRootCtx
->pRecord
), &LengthWritten
, MftRecord
);
1679 if (!NT_SUCCESS(Status
))
1681 DPRINT1("ERROR: Couldn't update Index Root!\n");
1686 ReleaseAttributeContext(IndexRootCtx
);
1687 ExFreePoolWithTag(IndexRecord
, TAG_NTFS
);
1688 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
1694 * Recursively searches directory index and applies the size update to the $FILE_NAME attribute of the
1695 * proper index entry.
1696 * (Heavily based on BrowseIndexEntries)
1699 UpdateIndexEntryFileNameSize(PDEVICE_EXTENSION Vcb
,
1700 PFILE_RECORD_HEADER MftRecord
,
1702 ULONG IndexBlockSize
,
1703 PINDEX_ENTRY_ATTRIBUTE FirstEntry
,
1704 PINDEX_ENTRY_ATTRIBUTE LastEntry
,
1705 PUNICODE_STRING FileName
,
1707 PULONG CurrentEntry
,
1709 ULONGLONG NewDataSize
,
1710 ULONGLONG NewAllocatedSize
,
1711 BOOLEAN CaseSensitive
)
1715 PINDEX_ENTRY_ATTRIBUTE IndexEntry
;
1716 PNTFS_ATTR_CONTEXT IndexAllocationCtx
;
1717 ULONGLONG IndexAllocationSize
;
1718 PINDEX_BUFFER IndexBuffer
;
1720 DPRINT("UpdateIndexEntrySize(%p, %p, %p, %lu, %p, %p, %wZ, %lu, %lu, %s, %I64u, %I64u, %s)\n",
1730 DirSearch
? "TRUE" : "FALSE",
1733 CaseSensitive
? "TRUE" : "FALSE");
1735 // find the index entry responsible for the file we're trying to update
1736 IndexEntry
= FirstEntry
;
1737 while (IndexEntry
< LastEntry
&&
1738 !(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_END
))
1740 if ((IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
) > NTFS_FILE_FIRST_USER_FILE
&&
1741 *CurrentEntry
>= *StartEntry
&&
1742 IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_DOS
&&
1743 CompareFileName(FileName
, IndexEntry
, DirSearch
, CaseSensitive
))
1745 *StartEntry
= *CurrentEntry
;
1746 IndexEntry
->FileName
.DataSize
= NewDataSize
;
1747 IndexEntry
->FileName
.AllocatedSize
= NewAllocatedSize
;
1748 // indicate that the caller will still need to write the structure to the disk
1749 return STATUS_PENDING
;
1752 (*CurrentEntry
) += 1;
1753 ASSERT(IndexEntry
->Length
>= sizeof(INDEX_ENTRY_ATTRIBUTE
));
1754 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)IndexEntry
+ IndexEntry
->Length
);
1757 /* If we're already browsing a subnode */
1758 if (IndexRecord
== NULL
)
1760 return STATUS_OBJECT_PATH_NOT_FOUND
;
1763 /* If there's no subnode */
1764 if (!(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_NODE
))
1766 return STATUS_OBJECT_PATH_NOT_FOUND
;
1769 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexAllocation
, L
"$I30", 4, &IndexAllocationCtx
, NULL
);
1770 if (!NT_SUCCESS(Status
))
1772 DPRINT("Corrupted filesystem!\n");
1776 IndexAllocationSize
= AttributeDataLength(IndexAllocationCtx
->pRecord
);
1777 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
1778 for (RecordOffset
= 0; RecordOffset
< IndexAllocationSize
; RecordOffset
+= IndexBlockSize
)
1780 ReadAttribute(Vcb
, IndexAllocationCtx
, RecordOffset
, IndexRecord
, IndexBlockSize
);
1781 Status
= FixupUpdateSequenceArray(Vcb
, &((PFILE_RECORD_HEADER
)IndexRecord
)->Ntfs
);
1782 if (!NT_SUCCESS(Status
))
1787 IndexBuffer
= (PINDEX_BUFFER
)IndexRecord
;
1788 ASSERT(IndexBuffer
->Ntfs
.Type
== NRH_INDX_TYPE
);
1789 ASSERT(IndexBuffer
->Header
.AllocatedSize
+ FIELD_OFFSET(INDEX_BUFFER
, Header
) == IndexBlockSize
);
1790 FirstEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.FirstEntryOffset
);
1791 LastEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.TotalSizeOfEntries
);
1792 ASSERT(LastEntry
<= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)IndexBuffer
+ IndexBlockSize
));
1794 Status
= UpdateIndexEntryFileNameSize(NULL
,
1807 if (Status
== STATUS_PENDING
)
1809 // write the index record back to disk
1812 // first we need to update the fixup values for the index block
1813 Status
= AddFixupArray(Vcb
, &((PFILE_RECORD_HEADER
)IndexRecord
)->Ntfs
);
1814 if (!NT_SUCCESS(Status
))
1816 DPRINT1("Error: Failed to update fixup sequence array!\n");
1820 Status
= WriteAttribute(Vcb
, IndexAllocationCtx
, RecordOffset
, (const PUCHAR
)IndexRecord
, IndexBlockSize
, &Written
, MftRecord
);
1821 if (!NT_SUCCESS(Status
))
1823 DPRINT1("ERROR Performing write!\n");
1827 Status
= STATUS_SUCCESS
;
1830 if (NT_SUCCESS(Status
))
1836 ReleaseAttributeContext(IndexAllocationCtx
);
1841 * @name UpdateFileRecord
1844 * Writes a file record to the master file table, at a given index.
1847 * Pointer to the DEVICE_EXTENSION of the target drive being written to.
1850 * Target index in the master file table to store the file record.
1853 * Pointer to the complete file record which will be written to the master file table.
1856 * STATUS_SUCCESSFUL on success. An error passed from WriteAttribute() otherwise.
1860 UpdateFileRecord(PDEVICE_EXTENSION Vcb
,
1862 PFILE_RECORD_HEADER FileRecord
)
1865 NTSTATUS Status
= STATUS_SUCCESS
;
1867 DPRINT("UpdateFileRecord(%p, 0x%I64x, %p)\n", Vcb
, MftIndex
, FileRecord
);
1869 // Add the fixup array to prepare the data for writing to disk
1870 AddFixupArray(Vcb
, &FileRecord
->Ntfs
);
1872 // write the file record to the master file table
1873 Status
= WriteAttribute(Vcb
,
1875 MftIndex
* Vcb
->NtfsInfo
.BytesPerFileRecord
,
1876 (const PUCHAR
)FileRecord
,
1877 Vcb
->NtfsInfo
.BytesPerFileRecord
,
1881 if (!NT_SUCCESS(Status
))
1883 DPRINT1("UpdateFileRecord failed: %lu written, %lu expected\n", BytesWritten
, Vcb
->NtfsInfo
.BytesPerFileRecord
);
1886 // remove the fixup array (so the file record pointer can still be used)
1887 FixupUpdateSequenceArray(Vcb
, &FileRecord
->Ntfs
);
1894 FixupUpdateSequenceArray(PDEVICE_EXTENSION Vcb
,
1895 PNTFS_RECORD_HEADER Record
)
1902 USA
= (USHORT
*)((PCHAR
)Record
+ Record
->UsaOffset
);
1903 USANumber
= *(USA
++);
1904 USACount
= Record
->UsaCount
- 1; /* Exclude the USA Number. */
1905 Block
= (USHORT
*)((PCHAR
)Record
+ Vcb
->NtfsInfo
.BytesPerSector
- 2);
1907 DPRINT("FixupUpdateSequenceArray(%p, %p)\nUSANumber: %u\tUSACount: %u\n", Vcb
, Record
, USANumber
, USACount
);
1911 if (*Block
!= USANumber
)
1913 DPRINT1("Mismatch with USA: %u read, %u expected\n" , *Block
, USANumber
);
1914 return STATUS_UNSUCCESSFUL
;
1917 Block
= (USHORT
*)((PCHAR
)Block
+ Vcb
->NtfsInfo
.BytesPerSector
);
1921 return STATUS_SUCCESS
;
1925 * @name AddNewMftEntry
1928 * Adds a file record to the master file table of a given device.
1931 * Pointer to a complete file record which will be saved to disk.
1934 * Pointer to the DEVICE_EXTENSION of the target drive.
1936 * @param DestinationIndex
1937 * Pointer to a ULONGLONG which will receive the MFT index where the file record was stored.
1940 * Boolean indicating if the function is allowed to wait for exclusive access to the master file table.
1941 * This will only be relevant if the MFT doesn't have any free file records and needs to be enlarged.
1944 * STATUS_SUCCESS on success.
1945 * STATUS_OBJECT_NAME_NOT_FOUND if we can't find the MFT's $Bitmap or if we weren't able
1946 * to read the attribute.
1947 * STATUS_INSUFFICIENT_RESOURCES if we can't allocate enough memory for a copy of $Bitmap.
1948 * STATUS_CANT_WAIT if CanWait was FALSE and the function could not get immediate, exclusive access to the MFT.
1951 AddNewMftEntry(PFILE_RECORD_HEADER FileRecord
,
1952 PDEVICE_EXTENSION DeviceExt
,
1953 PULONGLONG DestinationIndex
,
1956 NTSTATUS Status
= STATUS_SUCCESS
;
1959 ULONGLONG BitmapDataSize
;
1960 ULONGLONG AttrBytesRead
;
1962 PUCHAR BitmapBuffer
;
1963 ULONG LengthWritten
;
1964 PNTFS_ATTR_CONTEXT BitmapContext
;
1965 LARGE_INTEGER BitmapBits
;
1966 UCHAR SystemReservedBits
;
1968 DPRINT1("AddNewMftEntry(%p, %p, %p, %s)\n", FileRecord
, DeviceExt
, DestinationIndex
, CanWait
? "TRUE" : "FALSE");
1970 // First, we have to read the mft's $Bitmap attribute
1972 // Find the attribute
1973 Status
= FindAttribute(DeviceExt
, DeviceExt
->MasterFileTable
, AttributeBitmap
, L
"", 0, &BitmapContext
, NULL
);
1974 if (!NT_SUCCESS(Status
))
1976 DPRINT1("ERROR: Couldn't find $Bitmap attribute of master file table!\n");
1980 // Get size of bitmap
1981 BitmapDataSize
= AttributeDataLength(BitmapContext
->pRecord
);
1983 // RtlInitializeBitmap wants a ULONG-aligned pointer, and wants the memory passed to it to be a ULONG-multiple
1984 // Allocate a buffer for the $Bitmap attribute plus enough to ensure we can get a ULONG-aligned pointer
1985 BitmapBuffer
= ExAllocatePoolWithTag(NonPagedPool
, BitmapDataSize
+ sizeof(ULONG
), TAG_NTFS
);
1988 ReleaseAttributeContext(BitmapContext
);
1989 return STATUS_INSUFFICIENT_RESOURCES
;
1992 // Get a ULONG-aligned pointer for the bitmap itself
1993 BitmapData
= (PUCHAR
)ALIGN_UP_BY((ULONG_PTR
)BitmapBuffer
, sizeof(ULONG
));
1995 // read $Bitmap attribute
1996 AttrBytesRead
= ReadAttribute(DeviceExt
, BitmapContext
, 0, (PCHAR
)BitmapData
, BitmapDataSize
);
1998 if (AttrBytesRead
!= BitmapDataSize
)
2000 DPRINT1("ERROR: Unable to read $Bitmap attribute of master file table!\n");
2001 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
2002 ReleaseAttributeContext(BitmapContext
);
2003 return STATUS_OBJECT_NAME_NOT_FOUND
;
2006 // We need to backup the bits for records 0x10 - 0x17 (3rd byte of bitmap) and mark these records
2007 // as in-use so we don't assign files to those indices. They're reserved for the system (e.g. ChkDsk).
2008 SystemReservedBits
= BitmapData
[2];
2009 BitmapData
[2] = 0xff;
2011 // Calculate bit count
2012 BitmapBits
.QuadPart
= AttributeDataLength(DeviceExt
->MFTContext
->pRecord
) /
2013 DeviceExt
->NtfsInfo
.BytesPerFileRecord
;
2014 if (BitmapBits
.HighPart
!= 0)
2016 DPRINT1("\tFIXME: bitmap sizes beyond 32bits are not yet supported! (Your NTFS volume is too large)\n");
2017 NtfsGlobalData
->EnableWriteSupport
= FALSE
;
2018 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
2019 ReleaseAttributeContext(BitmapContext
);
2020 return STATUS_NOT_IMPLEMENTED
;
2023 // convert buffer into bitmap
2024 RtlInitializeBitMap(&Bitmap
, (PULONG
)BitmapData
, BitmapBits
.LowPart
);
2026 // set next available bit, preferrably after 23rd bit
2027 MftIndex
= RtlFindClearBitsAndSet(&Bitmap
, 1, 24);
2028 if ((LONG
)MftIndex
== -1)
2030 DPRINT1("Couldn't find free space in MFT for file record, increasing MFT size.\n");
2032 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
2033 ReleaseAttributeContext(BitmapContext
);
2035 // Couldn't find a free record in the MFT, add some blank records and try again
2036 Status
= IncreaseMftSize(DeviceExt
, CanWait
);
2037 if (!NT_SUCCESS(Status
))
2039 DPRINT1("ERROR: Couldn't find space in MFT for file or increase MFT size!\n");
2043 return AddNewMftEntry(FileRecord
, DeviceExt
, DestinationIndex
, CanWait
);
2046 DPRINT1("Creating file record at MFT index: %I64u\n", MftIndex
);
2048 // update file record with index
2049 FileRecord
->MFTRecordNumber
= MftIndex
;
2051 // [BitmapData should have been updated via RtlFindClearBitsAndSet()]
2053 // Restore the system reserved bits
2054 BitmapData
[2] = SystemReservedBits
;
2056 // write the bitmap back to the MFT's $Bitmap attribute
2057 Status
= WriteAttribute(DeviceExt
, BitmapContext
, 0, BitmapData
, BitmapDataSize
, &LengthWritten
, FileRecord
);
2058 if (!NT_SUCCESS(Status
))
2060 DPRINT1("ERROR encountered when writing $Bitmap attribute!\n");
2061 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
2062 ReleaseAttributeContext(BitmapContext
);
2066 // update the file record (write it to disk)
2067 Status
= UpdateFileRecord(DeviceExt
, MftIndex
, FileRecord
);
2069 if (!NT_SUCCESS(Status
))
2071 DPRINT1("ERROR: Unable to write file record!\n");
2072 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
2073 ReleaseAttributeContext(BitmapContext
);
2077 *DestinationIndex
= MftIndex
;
2079 ExFreePoolWithTag(BitmapBuffer
, TAG_NTFS
);
2080 ReleaseAttributeContext(BitmapContext
);
2086 * @name NtfsAddFilenameToDirectory
2089 * Adds a $FILE_NAME attribute to a given directory index.
2092 * Points to the target disk's DEVICE_EXTENSION.
2094 * @param DirectoryMftIndex
2095 * Mft index of the parent directory which will receive the file.
2097 * @param FileReferenceNumber
2098 * File reference of the file to be added to the directory. This is a combination of the
2099 * Mft index and sequence number.
2101 * @param FilenameAttribute
2102 * Pointer to the FILENAME_ATTRIBUTE of the file being added to the directory.
2104 * @param CaseSensitive
2105 * Boolean indicating if the function should operate in case-sensitive mode. This will be TRUE
2106 * if an application created the file with the FILE_FLAG_POSIX_SEMANTICS flag.
2109 * STATUS_SUCCESS on success.
2110 * STATUS_INSUFFICIENT_RESOURCES if an allocation fails.
2111 * STATUS_NOT_IMPLEMENTED if target address isn't at the end of the given file record.
2114 * WIP - Can only support a few files in a directory.
2115 * One FILENAME_ATTRIBUTE is added to the directory's index for each link to that file. So, each
2116 * file which contains one FILENAME_ATTRIBUTE for a long name and another for the 8.3 name, will
2117 * get both attributes added to its parent directory.
2120 NtfsAddFilenameToDirectory(PDEVICE_EXTENSION DeviceExt
,
2121 ULONGLONG DirectoryMftIndex
,
2122 ULONGLONG FileReferenceNumber
,
2123 PFILENAME_ATTRIBUTE FilenameAttribute
,
2124 BOOLEAN CaseSensitive
)
2126 NTSTATUS Status
= STATUS_SUCCESS
;
2127 PFILE_RECORD_HEADER ParentFileRecord
;
2128 PNTFS_ATTR_CONTEXT IndexRootContext
;
2129 PINDEX_ROOT_ATTRIBUTE I30IndexRoot
;
2130 ULONG IndexRootOffset
;
2131 ULONGLONG I30IndexRootLength
;
2132 ULONG LengthWritten
;
2133 PINDEX_ROOT_ATTRIBUTE NewIndexRoot
;
2134 ULONG AttributeLength
;
2135 PNTFS_ATTR_RECORD NextAttribute
;
2137 ULONG BtreeIndexLength
;
2138 ULONG MaxIndexRootSize
;
2139 PB_TREE_KEY NewLeftKey
;
2140 PB_TREE_FILENAME_NODE NewRightHandNode
;
2141 LARGE_INTEGER MinIndexRootSize
;
2142 ULONG NewMaxIndexRootSize
;
2145 // Allocate memory for the parent directory
2146 ParentFileRecord
= ExAllocatePoolWithTag(NonPagedPool
,
2147 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
2149 if (!ParentFileRecord
)
2151 DPRINT1("ERROR: Couldn't allocate memory for file record!\n");
2152 return STATUS_INSUFFICIENT_RESOURCES
;
2155 // Open the parent directory
2156 Status
= ReadFileRecord(DeviceExt
, DirectoryMftIndex
, ParentFileRecord
);
2157 if (!NT_SUCCESS(Status
))
2159 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2160 DPRINT1("ERROR: Couldn't read parent directory with index %I64u\n",
2166 DPRINT1("Dumping old parent file record:\n");
2167 NtfsDumpFileRecord(DeviceExt
, ParentFileRecord
);
2170 // Find the index root attribute for the directory
2171 Status
= FindAttribute(DeviceExt
,
2178 if (!NT_SUCCESS(Status
))
2180 DPRINT1("ERROR: Couldn't find $I30 $INDEX_ROOT attribute for parent directory with MFT #: %I64u!\n",
2182 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2186 // Find the maximum index size given what the file record can hold
2187 // First, find the max index size assuming index root is the last attribute
2188 MaxIndexRootSize
= DeviceExt
->NtfsInfo
.BytesPerFileRecord
// Start with the size of a file record
2189 - IndexRootOffset
// Subtract the length of everything that comes before index root
2190 - IndexRootContext
->pRecord
->Resident
.ValueOffset
// Subtract the length of the attribute header for index root
2191 - sizeof(INDEX_ROOT_ATTRIBUTE
) // Subtract the length of the index root header
2192 - (sizeof(ULONG
) * 2); // Subtract the length of the file record end marker and padding
2194 // Are there attributes after this one?
2195 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)ParentFileRecord
+ IndexRootOffset
+ IndexRootContext
->pRecord
->Length
);
2196 if (NextAttribute
->Type
!= AttributeEnd
)
2198 // Find the length of all attributes after this one, not counting the end marker
2199 ULONG LengthOfAttributes
= 0;
2200 PNTFS_ATTR_RECORD CurrentAttribute
= NextAttribute
;
2201 while (CurrentAttribute
->Type
!= AttributeEnd
)
2203 LengthOfAttributes
+= CurrentAttribute
->Length
;
2204 CurrentAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)CurrentAttribute
+ CurrentAttribute
->Length
);
2207 // Leave room for the existing attributes
2208 MaxIndexRootSize
-= LengthOfAttributes
;
2211 // Allocate memory for the index root data
2212 I30IndexRootLength
= AttributeDataLength(IndexRootContext
->pRecord
);
2213 I30IndexRoot
= ExAllocatePoolWithTag(NonPagedPool
, I30IndexRootLength
, TAG_NTFS
);
2216 DPRINT1("ERROR: Couldn't allocate memory for index root attribute!\n");
2217 ReleaseAttributeContext(IndexRootContext
);
2218 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2219 return STATUS_INSUFFICIENT_RESOURCES
;
2222 // Read the Index Root
2223 Status
= ReadAttribute(DeviceExt
, IndexRootContext
, 0, (PCHAR
)I30IndexRoot
, I30IndexRootLength
);
2224 if (!NT_SUCCESS(Status
))
2226 DPRINT1("ERROR: Couln't read index root attribute for Mft index #%I64u\n", DirectoryMftIndex
);
2227 ReleaseAttributeContext(IndexRootContext
);
2228 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2229 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2233 // Convert the index to a B*Tree
2234 Status
= CreateBTreeFromIndex(DeviceExt
,
2239 if (!NT_SUCCESS(Status
))
2241 DPRINT1("ERROR: Failed to create B-Tree from Index!\n");
2242 ReleaseAttributeContext(IndexRootContext
);
2243 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2244 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2252 // Insert the key for the file we're adding
2253 Status
= NtfsInsertKey(NewTree
,
2254 FileReferenceNumber
,
2259 I30IndexRoot
->SizeOfEntry
,
2262 if (!NT_SUCCESS(Status
))
2264 DPRINT1("ERROR: Failed to insert key into B-Tree!\n");
2265 DestroyBTree(NewTree
);
2266 ReleaseAttributeContext(IndexRootContext
);
2267 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2268 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2276 // The root node can't be split
2277 ASSERT(NewLeftKey
== NULL
);
2278 ASSERT(NewRightHandNode
== NULL
);
2280 // Convert B*Tree back to Index
2282 // Updating the index allocation can change the size available for the index root,
2283 // And if the index root is demoted, the index allocation will need to be updated again,
2284 // which may change the size available for index root... etc.
2285 // My solution is to decrease index root to the size it would be if it was demoted,
2286 // then UpdateIndexAllocation will have an accurate representation of the maximum space
2287 // it can use in the file record. There's still a chance that the act of allocating an
2288 // index node after demoting the index root will increase the size of the file record beyond
2289 // it's limit, but if that happens, an attribute-list will most definitely be needed.
2290 // This a bit hacky, but it seems to be functional.
2292 // Calculate the minimum size of the index root attribute, considering one dummy key and one VCN
2293 MinIndexRootSize
.QuadPart
= sizeof(INDEX_ROOT_ATTRIBUTE
) // size of the index root headers
2294 + 0x18; // Size of dummy key with a VCN for a subnode
2295 ASSERT(MinIndexRootSize
.QuadPart
% ATTR_RECORD_ALIGNMENT
== 0);
2297 // Temporarily shrink the index root to it's minimal size
2298 AttributeLength
= MinIndexRootSize
.LowPart
;
2299 AttributeLength
+= sizeof(INDEX_ROOT_ATTRIBUTE
);
2302 // FIXME: IndexRoot will probably be invalid until we're finished. If we fail before we finish, the directory will probably be toast.
2303 // The potential for catastrophic data-loss exists!!! :)
2305 // Update the length of the attribute in the file record of the parent directory
2306 Status
= InternalSetResidentAttributeLength(DeviceExt
,
2311 if (!NT_SUCCESS(Status
))
2313 DPRINT1("ERROR: Unable to set length of index root!\n");
2314 DestroyBTree(NewTree
);
2315 ReleaseAttributeContext(IndexRootContext
);
2316 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2317 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2321 // Update the index allocation
2322 Status
= UpdateIndexAllocation(DeviceExt
, NewTree
, I30IndexRoot
->SizeOfEntry
, ParentFileRecord
);
2323 if (!NT_SUCCESS(Status
))
2325 DPRINT1("ERROR: Failed to update index allocation from B-Tree!\n");
2326 DestroyBTree(NewTree
);
2327 ReleaseAttributeContext(IndexRootContext
);
2328 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2329 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2334 DPRINT1("Index Allocation updated\n");
2338 // Find the maximum index root size given what the file record can hold
2339 // First, find the max index size assuming index root is the last attribute
2340 NewMaxIndexRootSize
=
2341 DeviceExt
->NtfsInfo
.BytesPerFileRecord
// Start with the size of a file record
2342 - IndexRootOffset
// Subtract the length of everything that comes before index root
2343 - IndexRootContext
->pRecord
->Resident
.ValueOffset
// Subtract the length of the attribute header for index root
2344 - sizeof(INDEX_ROOT_ATTRIBUTE
) // Subtract the length of the index root header
2345 - (sizeof(ULONG
) * 2); // Subtract the length of the file record end marker and padding
2347 // Are there attributes after this one?
2348 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)ParentFileRecord
+ IndexRootOffset
+ IndexRootContext
->pRecord
->Length
);
2349 if (NextAttribute
->Type
!= AttributeEnd
)
2351 // Find the length of all attributes after this one, not counting the end marker
2352 ULONG LengthOfAttributes
= 0;
2353 PNTFS_ATTR_RECORD CurrentAttribute
= NextAttribute
;
2354 while (CurrentAttribute
->Type
!= AttributeEnd
)
2356 LengthOfAttributes
+= CurrentAttribute
->Length
;
2357 CurrentAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)CurrentAttribute
+ CurrentAttribute
->Length
);
2360 // Leave room for the existing attributes
2361 NewMaxIndexRootSize
-= LengthOfAttributes
;
2364 // The index allocation and index bitmap may have grown, leaving less room for the index root,
2365 // so now we need to double-check that index root isn't too large
2366 NodeSize
= GetSizeOfIndexEntries(NewTree
->RootNode
);
2367 if (NodeSize
> NewMaxIndexRootSize
)
2369 DPRINT1("Demoting index root.\nNodeSize: 0x%lx\nNewMaxIndexRootSize: 0x%lx\n", NodeSize
, NewMaxIndexRootSize
);
2371 Status
= DemoteBTreeRoot(NewTree
);
2372 if (!NT_SUCCESS(Status
))
2374 DPRINT1("ERROR: Failed to demote index root!\n");
2375 DestroyBTree(NewTree
);
2376 ReleaseAttributeContext(IndexRootContext
);
2377 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2378 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2382 // We need to update the index allocation once more
2383 Status
= UpdateIndexAllocation(DeviceExt
, NewTree
, I30IndexRoot
->SizeOfEntry
, ParentFileRecord
);
2384 if (!NT_SUCCESS(Status
))
2386 DPRINT1("ERROR: Failed to update index allocation from B-Tree!\n");
2387 DestroyBTree(NewTree
);
2388 ReleaseAttributeContext(IndexRootContext
);
2389 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2390 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2394 // re-recalculate max size of index root
2395 NewMaxIndexRootSize
=
2396 // Find the maximum index size given what the file record can hold
2397 // First, find the max index size assuming index root is the last attribute
2398 DeviceExt
->NtfsInfo
.BytesPerFileRecord
// Start with the size of a file record
2399 - IndexRootOffset
// Subtract the length of everything that comes before index root
2400 - IndexRootContext
->pRecord
->Resident
.ValueOffset
// Subtract the length of the attribute header for index root
2401 - sizeof(INDEX_ROOT_ATTRIBUTE
) // Subtract the length of the index root header
2402 - (sizeof(ULONG
) * 2); // Subtract the length of the file record end marker and padding
2404 // Are there attributes after this one?
2405 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)ParentFileRecord
+ IndexRootOffset
+ IndexRootContext
->pRecord
->Length
);
2406 if (NextAttribute
->Type
!= AttributeEnd
)
2408 // Find the length of all attributes after this one, not counting the end marker
2409 ULONG LengthOfAttributes
= 0;
2410 PNTFS_ATTR_RECORD CurrentAttribute
= NextAttribute
;
2411 while (CurrentAttribute
->Type
!= AttributeEnd
)
2413 LengthOfAttributes
+= CurrentAttribute
->Length
;
2414 CurrentAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)CurrentAttribute
+ CurrentAttribute
->Length
);
2417 // Leave room for the existing attributes
2418 NewMaxIndexRootSize
-= LengthOfAttributes
;
2424 // Create the Index Root from the B*Tree
2425 Status
= CreateIndexRootFromBTree(DeviceExt
, NewTree
, NewMaxIndexRootSize
, &NewIndexRoot
, &BtreeIndexLength
);
2426 if (!NT_SUCCESS(Status
))
2428 DPRINT1("ERROR: Failed to create Index root from B-Tree!\n");
2429 DestroyBTree(NewTree
);
2430 ReleaseAttributeContext(IndexRootContext
);
2431 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2432 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2436 // We're done with the B-Tree now
2437 DestroyBTree(NewTree
);
2439 // Write back the new index root attribute to the parent directory file record
2441 // First, we need to resize the attribute.
2442 // CreateIndexRootFromBTree() should have verified that the index root fits within MaxIndexSize.
2443 // We can't set the size as we normally would, because $INDEX_ROOT must always be resident.
2444 AttributeLength
= NewIndexRoot
->Header
.AllocatedSize
+ FIELD_OFFSET(INDEX_ROOT_ATTRIBUTE
, Header
);
2446 if (AttributeLength
!= IndexRootContext
->pRecord
->Resident
.ValueLength
)
2448 // Update the length of the attribute in the file record of the parent directory
2449 Status
= InternalSetResidentAttributeLength(DeviceExt
,
2454 if (!NT_SUCCESS(Status
))
2456 ExFreePoolWithTag(NewIndexRoot
, TAG_NTFS
);
2457 ReleaseAttributeContext(IndexRootContext
);
2458 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2459 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2460 DPRINT1("ERROR: Unable to set resident attribute length!\n");
2466 NT_ASSERT(ParentFileRecord
->BytesInUse
<= DeviceExt
->NtfsInfo
.BytesPerFileRecord
);
2468 Status
= UpdateFileRecord(DeviceExt
, DirectoryMftIndex
, ParentFileRecord
);
2469 if (!NT_SUCCESS(Status
))
2471 DPRINT1("ERROR: Failed to update file record of directory with index: %llx\n", DirectoryMftIndex
);
2472 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2473 ExFreePoolWithTag(NewIndexRoot
, TAG_NTFS
);
2474 ReleaseAttributeContext(IndexRootContext
);
2475 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2479 // Write the new index root to disk
2480 Status
= WriteAttribute(DeviceExt
,
2483 (PUCHAR
)NewIndexRoot
,
2487 if (!NT_SUCCESS(Status
) || LengthWritten
!= AttributeLength
)
2489 DPRINT1("ERROR: Unable to write new index root attribute to parent directory!\n");
2490 ExFreePoolWithTag(NewIndexRoot
, TAG_NTFS
);
2491 ReleaseAttributeContext(IndexRootContext
);
2492 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2493 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2497 // re-read the parent file record, so we can dump it
2498 Status
= ReadFileRecord(DeviceExt
, DirectoryMftIndex
, ParentFileRecord
);
2499 if (!NT_SUCCESS(Status
))
2501 DPRINT1("ERROR: Couldn't read parent directory after messing with it!\n");
2506 DPRINT1("Dumping new B-Tree:\n");
2508 Status
= CreateBTreeFromIndex(DeviceExt
, ParentFileRecord
, IndexRootContext
, NewIndexRoot
, &NewTree
);
2509 if (!NT_SUCCESS(Status
))
2511 DPRINT1("ERROR: Couldn't re-create b-tree\n");
2517 DestroyBTree(NewTree
);
2519 NtfsDumpFileRecord(DeviceExt
, ParentFileRecord
);
2524 ExFreePoolWithTag(NewIndexRoot
, TAG_NTFS
);
2525 ReleaseAttributeContext(IndexRootContext
);
2526 ExFreePoolWithTag(I30IndexRoot
, TAG_NTFS
);
2527 ExFreePoolWithTag(ParentFileRecord
, TAG_NTFS
);
2533 AddFixupArray(PDEVICE_EXTENSION Vcb
,
2534 PNTFS_RECORD_HEADER Record
)
2536 USHORT
*pShortToFixUp
;
2537 ULONG ArrayEntryCount
= Record
->UsaCount
- 1;
2538 ULONG Offset
= Vcb
->NtfsInfo
.BytesPerSector
- 2;
2541 PFIXUP_ARRAY fixupArray
= (PFIXUP_ARRAY
)((UCHAR
*)Record
+ Record
->UsaOffset
);
2543 DPRINT("AddFixupArray(%p, %p)\n fixupArray->USN: %u, ArrayEntryCount: %u\n", Vcb
, Record
, fixupArray
->USN
, ArrayEntryCount
);
2547 for (i
= 0; i
< ArrayEntryCount
; i
++)
2549 DPRINT("USN: %u\tOffset: %u\n", fixupArray
->USN
, Offset
);
2551 pShortToFixUp
= (USHORT
*)((PCHAR
)Record
+ Offset
);
2552 fixupArray
->Array
[i
] = *pShortToFixUp
;
2553 *pShortToFixUp
= fixupArray
->USN
;
2554 Offset
+= Vcb
->NtfsInfo
.BytesPerSector
;
2557 return STATUS_SUCCESS
;
2561 ReadLCN(PDEVICE_EXTENSION Vcb
,
2566 LARGE_INTEGER DiskSector
;
2568 DiskSector
.QuadPart
= lcn
;
2570 return NtfsReadSectors(Vcb
->StorageDevice
,
2571 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
2572 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
2573 Vcb
->NtfsInfo
.BytesPerSector
,
2580 CompareFileName(PUNICODE_STRING FileName
,
2581 PINDEX_ENTRY_ATTRIBUTE IndexEntry
,
2583 BOOLEAN CaseSensitive
)
2585 BOOLEAN Ret
, Alloc
= FALSE
;
2586 UNICODE_STRING EntryName
;
2588 EntryName
.Buffer
= IndexEntry
->FileName
.Name
;
2590 EntryName
.MaximumLength
= IndexEntry
->FileName
.NameLength
* sizeof(WCHAR
);
2594 UNICODE_STRING IntFileName
;
2597 NT_VERIFY(NT_SUCCESS(RtlUpcaseUnicodeString(&IntFileName
, FileName
, TRUE
)));
2602 IntFileName
= *FileName
;
2605 Ret
= FsRtlIsNameInExpression(&IntFileName
, &EntryName
, !CaseSensitive
, NULL
);
2609 RtlFreeUnicodeString(&IntFileName
);
2616 return (RtlCompareUnicodeString(FileName
, &EntryName
, !CaseSensitive
) == 0);
2621 * @name UpdateMftMirror
2624 * Backs-up the first ~4 master file table entries to the $MFTMirr file.
2627 * Pointer to an NTFS_VCB for the volume whose Mft mirror is being updated.
2631 * STATUS_SUCCESS on success.
2632 * STATUS_INSUFFICIENT_RESOURCES if an allocation failed.
2633 * STATUS_UNSUCCESSFUL if we couldn't read the master file table.
2636 * NTFS maintains up-to-date copies of the first several mft entries in the $MFTMirr file. Usually, the first 4 file
2637 * records from the mft are stored. The exact number of entries is determined by the size of $MFTMirr's $DATA.
2638 * If $MFTMirr is not up-to-date, chkdsk will reject every change it can find prior to when $MFTMirr was last updated.
2639 * Therefore, it's recommended to call this function if the volume changes considerably. For instance, IncreaseMftSize()
2640 * relies on this function to keep chkdsk from deleting the mft entries it creates. Note that under most instances, creating
2641 * or deleting a file will not affect the first ~four mft entries, and so will not require updating the mft mirror.
2644 UpdateMftMirror(PNTFS_VCB Vcb
)
2646 PFILE_RECORD_HEADER MirrorFileRecord
;
2647 PNTFS_ATTR_CONTEXT MirrDataContext
;
2648 PNTFS_ATTR_CONTEXT MftDataContext
;
2650 ULONGLONG DataLength
;
2653 ULONG LengthWritten
;
2655 // Allocate memory for the Mft mirror file record
2656 MirrorFileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
2657 if (!MirrorFileRecord
)
2659 DPRINT1("Error: Failed to allocate memory for $MFTMirr!\n");
2660 return STATUS_INSUFFICIENT_RESOURCES
;
2663 // Read the Mft Mirror file record
2664 Status
= ReadFileRecord(Vcb
, NTFS_FILE_MFTMIRR
, MirrorFileRecord
);
2665 if (!NT_SUCCESS(Status
))
2667 DPRINT1("ERROR: Failed to read $MFTMirr!\n");
2668 ExFreePoolWithTag(MirrorFileRecord
, TAG_NTFS
);
2672 // Find the $DATA attribute of $MFTMirr
2673 Status
= FindAttribute(Vcb
, MirrorFileRecord
, AttributeData
, L
"", 0, &MirrDataContext
, NULL
);
2674 if (!NT_SUCCESS(Status
))
2676 DPRINT1("ERROR: Couldn't find $DATA attribute!\n");
2677 ExFreePoolWithTag(MirrorFileRecord
, TAG_NTFS
);
2681 // Find the $DATA attribute of $MFT
2682 Status
= FindAttribute(Vcb
, Vcb
->MasterFileTable
, AttributeData
, L
"", 0, &MftDataContext
, NULL
);
2683 if (!NT_SUCCESS(Status
))
2685 DPRINT1("ERROR: Couldn't find $DATA attribute!\n");
2686 ReleaseAttributeContext(MirrDataContext
);
2687 ExFreePoolWithTag(MirrorFileRecord
, TAG_NTFS
);
2691 // Get the size of the mirror's $DATA attribute
2692 DataLength
= AttributeDataLength(MirrDataContext
->pRecord
);
2694 ASSERT(DataLength
% Vcb
->NtfsInfo
.BytesPerFileRecord
== 0);
2696 // Create buffer for the mirror's $DATA attribute
2697 DataBuffer
= ExAllocatePoolWithTag(NonPagedPool
, DataLength
, TAG_NTFS
);
2700 DPRINT1("Error: Couldn't allocate memory for $DATA buffer!\n");
2701 ReleaseAttributeContext(MftDataContext
);
2702 ReleaseAttributeContext(MirrDataContext
);
2703 ExFreePoolWithTag(MirrorFileRecord
, TAG_NTFS
);
2704 return STATUS_INSUFFICIENT_RESOURCES
;
2707 ASSERT(DataLength
< ULONG_MAX
);
2709 // Back up the first several entries of the Mft's $DATA Attribute
2710 BytesRead
= ReadAttribute(Vcb
, MftDataContext
, 0, DataBuffer
, (ULONG
)DataLength
);
2711 if (BytesRead
!= (ULONG
)DataLength
)
2713 DPRINT1("Error: Failed to read $DATA for $MFTMirr!\n");
2714 ReleaseAttributeContext(MftDataContext
);
2715 ReleaseAttributeContext(MirrDataContext
);
2716 ExFreePoolWithTag(DataBuffer
, TAG_NTFS
);
2717 ExFreePoolWithTag(MirrorFileRecord
, TAG_NTFS
);
2718 return STATUS_UNSUCCESSFUL
;
2721 // Write the mirror's $DATA attribute
2722 Status
= WriteAttribute(Vcb
,
2729 if (!NT_SUCCESS(Status
))
2731 DPRINT1("ERROR: Failed to write $DATA attribute of $MFTMirr!\n");
2735 ReleaseAttributeContext(MftDataContext
);
2736 ReleaseAttributeContext(MirrDataContext
);
2737 ExFreePoolWithTag(DataBuffer
, TAG_NTFS
);
2738 ExFreePoolWithTag(MirrorFileRecord
, TAG_NTFS
);
2746 DumpIndexEntry(PINDEX_ENTRY_ATTRIBUTE IndexEntry
)
2748 DPRINT1("Entry: %p\n", IndexEntry
);
2749 DPRINT1("\tData.Directory.IndexedFile: %I64x\n", IndexEntry
->Data
.Directory
.IndexedFile
);
2750 DPRINT1("\tLength: %u\n", IndexEntry
->Length
);
2751 DPRINT1("\tKeyLength: %u\n", IndexEntry
->KeyLength
);
2752 DPRINT1("\tFlags: %x\n", IndexEntry
->Flags
);
2753 DPRINT1("\tReserved: %x\n", IndexEntry
->Reserved
);
2754 DPRINT1("\t\tDirectoryFileReferenceNumber: %I64x\n", IndexEntry
->FileName
.DirectoryFileReferenceNumber
);
2755 DPRINT1("\t\tCreationTime: %I64u\n", IndexEntry
->FileName
.CreationTime
);
2756 DPRINT1("\t\tChangeTime: %I64u\n", IndexEntry
->FileName
.ChangeTime
);
2757 DPRINT1("\t\tLastWriteTime: %I64u\n", IndexEntry
->FileName
.LastWriteTime
);
2758 DPRINT1("\t\tLastAccessTime: %I64u\n", IndexEntry
->FileName
.LastAccessTime
);
2759 DPRINT1("\t\tAllocatedSize: %I64u\n", IndexEntry
->FileName
.AllocatedSize
);
2760 DPRINT1("\t\tDataSize: %I64u\n", IndexEntry
->FileName
.DataSize
);
2761 DPRINT1("\t\tFileAttributes: %x\n", IndexEntry
->FileName
.FileAttributes
);
2762 DPRINT1("\t\tNameLength: %u\n", IndexEntry
->FileName
.NameLength
);
2763 DPRINT1("\t\tNameType: %x\n", IndexEntry
->FileName
.NameType
);
2764 DPRINT1("\t\tName: %.*S\n", IndexEntry
->FileName
.NameLength
, IndexEntry
->FileName
.Name
);
2769 BrowseIndexEntries(PDEVICE_EXTENSION Vcb
,
2770 PFILE_RECORD_HEADER MftRecord
,
2772 ULONG IndexBlockSize
,
2773 PINDEX_ENTRY_ATTRIBUTE FirstEntry
,
2774 PINDEX_ENTRY_ATTRIBUTE LastEntry
,
2775 PUNICODE_STRING FileName
,
2777 PULONG CurrentEntry
,
2779 BOOLEAN CaseSensitive
,
2780 ULONGLONG
*OutMFTIndex
)
2784 PINDEX_ENTRY_ATTRIBUTE IndexEntry
;
2785 PNTFS_ATTR_CONTEXT IndexAllocationCtx
;
2786 ULONGLONG IndexAllocationSize
;
2787 PINDEX_BUFFER IndexBuffer
;
2789 DPRINT("BrowseIndexEntries(%p, %p, %p, %lu, %p, %p, %wZ, %lu, %lu, %s, %s, %p)\n",
2799 DirSearch
? "TRUE" : "FALSE",
2800 CaseSensitive
? "TRUE" : "FALSE",
2803 IndexEntry
= FirstEntry
;
2804 while (IndexEntry
< LastEntry
&&
2805 !(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_END
))
2807 if ((IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
) >= NTFS_FILE_FIRST_USER_FILE
&&
2808 *CurrentEntry
>= *StartEntry
&&
2809 IndexEntry
->FileName
.NameType
!= NTFS_FILE_NAME_DOS
&&
2810 CompareFileName(FileName
, IndexEntry
, DirSearch
, CaseSensitive
))
2812 *StartEntry
= *CurrentEntry
;
2813 *OutMFTIndex
= (IndexEntry
->Data
.Directory
.IndexedFile
& NTFS_MFT_MASK
);
2814 return STATUS_SUCCESS
;
2817 (*CurrentEntry
) += 1;
2818 ASSERT(IndexEntry
->Length
>= sizeof(INDEX_ENTRY_ATTRIBUTE
));
2819 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)IndexEntry
+ IndexEntry
->Length
);
2822 /* If we're already browsing a subnode */
2823 if (IndexRecord
== NULL
)
2825 return STATUS_OBJECT_PATH_NOT_FOUND
;
2828 /* If there's no subnode */
2829 if (!(IndexEntry
->Flags
& NTFS_INDEX_ENTRY_NODE
))
2831 return STATUS_OBJECT_PATH_NOT_FOUND
;
2834 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexAllocation
, L
"$I30", 4, &IndexAllocationCtx
, NULL
);
2835 if (!NT_SUCCESS(Status
))
2837 DPRINT1("Corrupted filesystem!\n");
2841 IndexAllocationSize
= AttributeDataLength(IndexAllocationCtx
->pRecord
);
2842 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
2843 for (RecordOffset
= 0; RecordOffset
< IndexAllocationSize
; RecordOffset
+= IndexBlockSize
)
2845 ReadAttribute(Vcb
, IndexAllocationCtx
, RecordOffset
, IndexRecord
, IndexBlockSize
);
2846 Status
= FixupUpdateSequenceArray(Vcb
, &((PFILE_RECORD_HEADER
)IndexRecord
)->Ntfs
);
2847 if (!NT_SUCCESS(Status
))
2852 IndexBuffer
= (PINDEX_BUFFER
)IndexRecord
;
2853 ASSERT(IndexBuffer
->Ntfs
.Type
== NRH_INDX_TYPE
);
2854 ASSERT(IndexBuffer
->Header
.AllocatedSize
+ FIELD_OFFSET(INDEX_BUFFER
, Header
) == IndexBlockSize
);
2855 FirstEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.FirstEntryOffset
);
2856 LastEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)&IndexBuffer
->Header
+ IndexBuffer
->Header
.TotalSizeOfEntries
);
2857 ASSERT(LastEntry
<= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)IndexBuffer
+ IndexBlockSize
));
2859 Status
= BrowseIndexEntries(NULL
,
2871 if (NT_SUCCESS(Status
))
2877 ReleaseAttributeContext(IndexAllocationCtx
);
2882 NtfsFindMftRecord(PDEVICE_EXTENSION Vcb
,
2884 PUNICODE_STRING FileName
,
2887 BOOLEAN CaseSensitive
,
2888 ULONGLONG
*OutMFTIndex
)
2890 PFILE_RECORD_HEADER MftRecord
;
2891 PNTFS_ATTR_CONTEXT IndexRootCtx
;
2892 PINDEX_ROOT_ATTRIBUTE IndexRoot
;
2894 PINDEX_ENTRY_ATTRIBUTE IndexEntry
, IndexEntryEnd
;
2896 ULONG CurrentEntry
= 0;
2898 DPRINT("NtfsFindMftRecord(%p, %I64d, %wZ, %lu, %s, %s, %p)\n",
2903 DirSearch
? "TRUE" : "FALSE",
2904 CaseSensitive
? "TRUE" : "FALSE",
2907 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
2908 Vcb
->NtfsInfo
.BytesPerFileRecord
,
2910 if (MftRecord
== NULL
)
2912 return STATUS_INSUFFICIENT_RESOURCES
;
2915 Status
= ReadFileRecord(Vcb
, MFTIndex
, MftRecord
);
2916 if (!NT_SUCCESS(Status
))
2918 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
2922 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
2923 Status
= FindAttribute(Vcb
, MftRecord
, AttributeIndexRoot
, L
"$I30", 4, &IndexRootCtx
, NULL
);
2924 if (!NT_SUCCESS(Status
))
2926 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
2930 IndexRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerIndexRecord
, TAG_NTFS
);
2931 if (IndexRecord
== NULL
)
2933 ReleaseAttributeContext(IndexRootCtx
);
2934 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
2935 return STATUS_INSUFFICIENT_RESOURCES
;
2938 ReadAttribute(Vcb
, IndexRootCtx
, 0, IndexRecord
, Vcb
->NtfsInfo
.BytesPerIndexRecord
);
2939 IndexRoot
= (PINDEX_ROOT_ATTRIBUTE
)IndexRecord
;
2940 IndexEntry
= (PINDEX_ENTRY_ATTRIBUTE
)((PCHAR
)&IndexRoot
->Header
+ IndexRoot
->Header
.FirstEntryOffset
);
2941 /* Index root is always resident. */
2942 IndexEntryEnd
= (PINDEX_ENTRY_ATTRIBUTE
)(IndexRecord
+ IndexRoot
->Header
.TotalSizeOfEntries
);
2943 ReleaseAttributeContext(IndexRootCtx
);
2945 DPRINT("IndexRecordSize: %x IndexBlockSize: %x\n", Vcb
->NtfsInfo
.BytesPerIndexRecord
, IndexRoot
->SizeOfEntry
);
2947 Status
= BrowseIndexEntries(Vcb
,
2950 IndexRoot
->SizeOfEntry
,
2960 ExFreePoolWithTag(IndexRecord
, TAG_NTFS
);
2961 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
2967 NtfsLookupFileAt(PDEVICE_EXTENSION Vcb
,
2968 PUNICODE_STRING PathName
,
2969 BOOLEAN CaseSensitive
,
2970 PFILE_RECORD_HEADER
*FileRecord
,
2971 PULONGLONG MFTIndex
,
2972 ULONGLONG CurrentMFTIndex
)
2974 UNICODE_STRING Current
, Remaining
;
2976 ULONG FirstEntry
= 0;
2978 DPRINT("NtfsLookupFileAt(%p, %wZ, %s, %p, %p, %I64x)\n",
2981 CaseSensitive
? "TRUE" : "FALSE",
2986 FsRtlDissectName(*PathName
, &Current
, &Remaining
);
2988 while (Current
.Length
!= 0)
2990 DPRINT("Current: %wZ\n", &Current
);
2992 Status
= NtfsFindMftRecord(Vcb
, CurrentMFTIndex
, &Current
, &FirstEntry
, FALSE
, CaseSensitive
, &CurrentMFTIndex
);
2993 if (!NT_SUCCESS(Status
))
2998 if (Remaining
.Length
== 0)
3001 FsRtlDissectName(Current
, &Current
, &Remaining
);
3004 *FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
3005 if (*FileRecord
== NULL
)
3007 DPRINT("NtfsLookupFileAt: Can't allocate MFT record\n");
3008 return STATUS_INSUFFICIENT_RESOURCES
;
3011 Status
= ReadFileRecord(Vcb
, CurrentMFTIndex
, *FileRecord
);
3012 if (!NT_SUCCESS(Status
))
3014 DPRINT("NtfsLookupFileAt: Can't read MFT record\n");
3015 ExFreePoolWithTag(*FileRecord
, TAG_NTFS
);
3019 *MFTIndex
= CurrentMFTIndex
;
3021 return STATUS_SUCCESS
;
3025 NtfsLookupFile(PDEVICE_EXTENSION Vcb
,
3026 PUNICODE_STRING PathName
,
3027 BOOLEAN CaseSensitive
,
3028 PFILE_RECORD_HEADER
*FileRecord
,
3029 PULONGLONG MFTIndex
)
3031 return NtfsLookupFileAt(Vcb
, PathName
, CaseSensitive
, FileRecord
, MFTIndex
, NTFS_FILE_ROOT
);
3035 * @name NtfsDumpFileRecord
3038 * Provides diagnostic information about a file record. Prints a hex dump
3039 * of the entire record (based on the size reported by FileRecord->ByesInUse),
3040 * then prints a dump of each attribute.
3043 * Pointer to a DEVICE_EXTENSION describing the volume.
3046 * Pointer to the file record to be analyzed.
3049 * FileRecord must be a complete file record at least FileRecord->BytesAllocated
3050 * in size, and not just the header.
3054 NtfsDumpFileRecord(PDEVICE_EXTENSION Vcb
,
3055 PFILE_RECORD_HEADER FileRecord
)
3059 // dump binary data, 8 bytes at a time
3060 for (i
= 0; i
< FileRecord
->BytesInUse
; i
+= 8)
3062 // display current offset, in hex
3063 DbgPrint("\t%03x\t", i
);
3065 // display hex value of each of the next 8 bytes
3066 for (j
= 0; j
< 8; j
++)
3067 DbgPrint("%02x ", *(PUCHAR
)((ULONG_PTR
)FileRecord
+ i
+ j
));
3071 NtfsDumpFileAttributes(Vcb
, FileRecord
);
3075 NtfsFindFileAt(PDEVICE_EXTENSION Vcb
,
3076 PUNICODE_STRING SearchPattern
,
3078 PFILE_RECORD_HEADER
*FileRecord
,
3079 PULONGLONG MFTIndex
,
3080 ULONGLONG CurrentMFTIndex
,
3081 BOOLEAN CaseSensitive
)
3085 DPRINT("NtfsFindFileAt(%p, %wZ, %lu, %p, %p, %I64x, %s)\n",
3092 (CaseSensitive
? "TRUE" : "FALSE"));
3094 Status
= NtfsFindMftRecord(Vcb
, CurrentMFTIndex
, SearchPattern
, FirstEntry
, TRUE
, CaseSensitive
, &CurrentMFTIndex
);
3095 if (!NT_SUCCESS(Status
))
3097 DPRINT("NtfsFindFileAt: NtfsFindMftRecord() failed with status 0x%08lx\n", Status
);
3101 *FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
3102 if (*FileRecord
== NULL
)
3104 DPRINT("NtfsFindFileAt: Can't allocate MFT record\n");
3105 return STATUS_INSUFFICIENT_RESOURCES
;
3108 Status
= ReadFileRecord(Vcb
, CurrentMFTIndex
, *FileRecord
);
3109 if (!NT_SUCCESS(Status
))
3111 DPRINT("NtfsFindFileAt: Can't read MFT record\n");
3112 ExFreePoolWithTag(*FileRecord
, TAG_NTFS
);
3116 *MFTIndex
= CurrentMFTIndex
;
3118 return STATUS_SUCCESS
;