3 * Copyright (C) 2002 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 * PROGRAMMER: Eric Kohl
24 * Updated by Valentin Verkhovsky 2003/09/12
27 /* INCLUDES *****************************************************************/
34 /* FUNCTIONS ****************************************************************/
37 NtfsOpenMft(PDEVICE_EXTENSION Vcb
)
40 PFILE_RECORD_HEADER MftRecord
;
41 PFILE_RECORD_HEADER FileRecord
;
42 // PATTRIBUTE Attribute;
43 // PATTRIBUTE AttrData;
44 // PRESIDENT_ATTRIBUTE ResAttr;
47 ULONG BytesPerFileRecord
;
51 DPRINT1("NtfsOpenMft() called\n");
53 BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
55 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
58 if (MftRecord
== NULL
)
60 return STATUS_INSUFFICIENT_RESOURCES
;
63 Status
= NtfsReadSectors(Vcb
->StorageDevice
,
64 Vcb
->NtfsInfo
.MftStart
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
65 BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerSector
,
66 Vcb
->NtfsInfo
.BytesPerSector
,
69 if (!NT_SUCCESS(Status
))
71 ExFreePool(MftRecord
);
75 FixupUpdateSequenceArray(MftRecord
);
77 // Attribute = FindAttribute(MftRecord, AttributeBitmap, 0);
79 /* Get number of file records*/
80 n
= AttributeDataLength(FindAttribute(MftRecord
, AttributeData
, 0)) / BytesPerFileRecord
;
82 FileRecord
= ExAllocatePoolWithTag(NonPagedPool
,
85 if (FileRecord
== NULL
)
87 ExFreePool(MftRecord
);
88 return STATUS_INSUFFICIENT_RESOURCES
;
91 /* Enumerate MFT Records */
92 DPRINT("Enumerate MFT records\n");
93 for (i
= 0; i
< n
; i
++)
100 if (FileRecord
->Ntfs
.Type
== NRH_FILE_TYPE
&&
101 (FileRecord
->Flags
& FRH_IN_USE
))
103 DPRINT("\nFile %lu\n\n", i
);
105 /* Enumerate attributtes */
106 NtfsDumpFileAttributes (FileRecord
);
111 ExFreePool(FileRecord
);
112 ExFreePool(MftRecord
);
119 FindAttribute(PFILE_RECORD_HEADER FileRecord
,
123 PATTRIBUTE Attribute
;
125 UNREFERENCED_PARAMETER(name
);
127 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
128 while (Attribute
< (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
) &&
129 Attribute
->AttributeType
!= (ATTRIBUTE_TYPE
)-1)
131 if (Attribute
->AttributeType
== Type
)
136 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Length
);
144 AttributeAllocatedLength(PATTRIBUTE Attribute
)
146 if (Attribute
->Nonresident
)
148 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->AllocatedSize
;
151 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
156 AttributeDataLength(PATTRIBUTE Attribute
)
158 if (Attribute
->Nonresident
)
160 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->DataSize
;
163 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
168 ReadAttribute(PATTRIBUTE attr
,
170 PDEVICE_EXTENSION Vcb
,
171 PDEVICE_OBJECT DeviceObject
)
173 PNONRESIDENT_ATTRIBUTE NresAttr
= (PNONRESIDENT_ATTRIBUTE
)attr
;
175 UNREFERENCED_PARAMETER(DeviceObject
);
177 if (attr
->Nonresident
== FALSE
)
180 (PVOID
)((ULONG_PTR
)attr
+ ((PRESIDENT_ATTRIBUTE
)attr
)->ValueOffset
),
181 ((PRESIDENT_ATTRIBUTE
)attr
)->ValueLength
);
184 ReadExternalAttribute(Vcb
,
187 (ULONG
)(NresAttr
->LastVcn
) + 1,
193 ReadFileRecord(PDEVICE_EXTENSION Vcb
,
195 PFILE_RECORD_HEADER file
,
196 PFILE_RECORD_HEADER Mft
)
199 ULONG BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
200 ULONG clusters
= max(BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
, 1);
201 ULONGLONG vcn
= index
* BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
;
202 LONG m
= (Vcb
->NtfsInfo
.BytesPerCluster
/ BytesPerFileRecord
) - 1;
203 ULONG n
= m
> 0 ? (index
& m
) : 0;
205 p
= ExAllocatePoolWithTag(NonPagedPool
,
206 clusters
* Vcb
->NtfsInfo
.BytesPerCluster
,
209 ReadVCN (Vcb
, Mft
, AttributeData
, vcn
, clusters
, p
);
212 (PVOID
)((ULONG_PTR
)p
+ n
* BytesPerFileRecord
),
217 FixupUpdateSequenceArray(file
);
219 return STATUS_SUCCESS
;
224 ReadExternalAttribute(PDEVICE_EXTENSION Vcb
,
225 PNONRESIDENT_ATTRIBUTE NresAttr
,
236 PUCHAR bytes
= (PUCHAR
)buffer
;
238 for (left
= count
; left
> 0; left
-= readcount
)
240 FindRun(NresAttr
, vcn
, &lcn
, &runcount
);
242 // readcount = (ULONG)(__min(runcount, left));
243 readcount
= (ULONG
)min(runcount
, left
);
246 n
= readcount
* Vcb
->NtfsInfo
.BytesPerCluster
;
251 ReadLCN(Vcb
, lcn
, readcount
, bytes
);
260 ReadVCN(PDEVICE_EXTENSION Vcb
,
261 PFILE_RECORD_HEADER file
,
267 PNONRESIDENT_ATTRIBUTE NresAttr
;
270 attr
= FindAttribute(file
, type
, 0);
272 NresAttr
= (PNONRESIDENT_ATTRIBUTE
) attr
;
274 if (NresAttr
== 0 || (vcn
< NresAttr
->StartVcn
||vcn
> NresAttr
->LastVcn
))
276 // PATTRIBUTE attrList = FindAttribute(file,AttributeAttributeList,0);
277 DbgPrint("Exeption \n");
281 ReadExternalAttribute(Vcb
, NresAttr
, vcn
, count
, buffer
);
286 BOOL
bitset(PUCHAR bitmap
, ULONG i
)
288 return (bitmap
[i
>>3] & (1 << (i
& 7))) !=0;
294 FixupUpdateSequenceArray(PFILE_RECORD_HEADER file
)
296 PUSHORT usa
= (PUSHORT
)((ULONG_PTR
)file
+ file
->Ntfs
.UsaOffset
);
297 PUSHORT sector
= (PUSHORT
)file
;
300 for (i
= 1; i
< file
->Ntfs
.UsaCount
; i
++)
302 sector
[255] = usa
[i
];
309 ReadLCN(PDEVICE_EXTENSION Vcb
,
314 LARGE_INTEGER DiskSector
;
316 DiskSector
.QuadPart
= lcn
;
318 return NtfsReadSectors(Vcb
->StorageDevice
,
319 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
320 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
321 Vcb
->NtfsInfo
.BytesPerSector
,