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 /* GLOBALS *****************************************************************/
37 /* FUNCTIONS ****************************************************************/
41 NtfsOpenMft(PDEVICE_EXTENSION Vcb
)
44 PFILE_RECORD_HEADER MftRecord
;
45 PFILE_RECORD_HEADER FileRecord
;
46 // PATTRIBUTE Attribute;
47 // PATTRIBUTE AttrData;
48 // PRESIDENT_ATTRIBUTE ResAttr;
51 ULONG BytesPerFileRecord
;
55 DPRINT1("NtfsOpenMft() called\n");
57 BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
59 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
62 if (MftRecord
== NULL
)
64 return STATUS_INSUFFICIENT_RESOURCES
;
67 Status
= NtfsReadSectors(Vcb
->StorageDevice
,
68 Vcb
->NtfsInfo
.MftStart
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
69 BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerSector
,
70 Vcb
->NtfsInfo
.BytesPerSector
,
73 if (!NT_SUCCESS(Status
))
75 ExFreePool(MftRecord
);
79 FixupUpdateSequenceArray(MftRecord
);
81 // Attribute = FindAttribute(MftRecord, AttributeBitmap, 0);
83 /* Get number of file records*/
84 n
= AttributeDataLength(FindAttribute(MftRecord
, AttributeData
, 0)) / BytesPerFileRecord
;
86 FileRecord
= ExAllocatePoolWithTag(NonPagedPool
,
89 if (FileRecord
== NULL
)
91 ExFreePool(MftRecord
);
92 return STATUS_INSUFFICIENT_RESOURCES
;
95 /* Enumerate MFT Records */
96 DPRINT("Enumerate MFT records\n");
97 for (i
= 0; i
< n
; i
++)
104 if (FileRecord
->Ntfs
.Type
== NRH_FILE_TYPE
&&
105 (FileRecord
->Flags
& FRH_IN_USE
))
107 DPRINT("\nFile %lu\n\n", i
);
109 /* Enumerate attributtes */
110 NtfsDumpFileAttributes (FileRecord
);
115 ExFreePool(FileRecord
);
116 ExFreePool(MftRecord
);
123 FindAttribute(PFILE_RECORD_HEADER FileRecord
,
127 PATTRIBUTE Attribute
;
129 UNREFERENCED_PARAMETER(name
);
131 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
132 while (Attribute
< (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
) &&
133 Attribute
->AttributeType
!= (ATTRIBUTE_TYPE
)-1)
135 if (Attribute
->AttributeType
== Type
)
140 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Length
);
148 AttributeAllocatedLength(PATTRIBUTE Attribute
)
150 if (Attribute
->Nonresident
)
152 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->AllocatedSize
;
155 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
160 AttributeDataLength(PATTRIBUTE Attribute
)
162 if (Attribute
->Nonresident
)
164 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->DataSize
;
167 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
172 ReadAttribute(PATTRIBUTE attr
,
174 PDEVICE_EXTENSION Vcb
,
175 PDEVICE_OBJECT DeviceObject
)
177 PNONRESIDENT_ATTRIBUTE NresAttr
= (PNONRESIDENT_ATTRIBUTE
)attr
;
179 UNREFERENCED_PARAMETER(DeviceObject
);
181 if (attr
->Nonresident
== FALSE
)
184 (PVOID
)((ULONG_PTR
)attr
+ ((PRESIDENT_ATTRIBUTE
)attr
)->ValueOffset
),
185 ((PRESIDENT_ATTRIBUTE
)attr
)->ValueLength
);
188 ReadExternalAttribute(Vcb
,
191 (ULONG
)(NresAttr
->LastVcn
) + 1,
197 ReadFileRecord(PDEVICE_EXTENSION Vcb
,
199 PFILE_RECORD_HEADER file
,
200 PFILE_RECORD_HEADER Mft
)
203 ULONG BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
204 ULONG clusters
= max(BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
, 1);
205 ULONGLONG vcn
= index
* BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
;
206 LONG m
= (Vcb
->NtfsInfo
.BytesPerCluster
/ BytesPerFileRecord
) - 1;
207 ULONG n
= m
> 0 ? (index
& m
) : 0;
209 p
= ExAllocatePoolWithTag(NonPagedPool
,
210 clusters
* Vcb
->NtfsInfo
.BytesPerCluster
,
213 ReadVCN (Vcb
, Mft
, AttributeData
, vcn
, clusters
, p
);
216 (PVOID
)((ULONG_PTR
)p
+ n
* BytesPerFileRecord
),
221 FixupUpdateSequenceArray(file
);
223 return STATUS_SUCCESS
;
228 ReadExternalAttribute(PDEVICE_EXTENSION Vcb
,
229 PNONRESIDENT_ATTRIBUTE NresAttr
,
240 PUCHAR bytes
= (PUCHAR
)buffer
;
242 for (left
= count
; left
> 0; left
-= readcount
)
244 FindRun(NresAttr
, vcn
, &lcn
, &runcount
);
246 // readcount = (ULONG)(__min(runcount, left));
247 readcount
= (ULONG
)min(runcount
, left
);
250 n
= readcount
* Vcb
->NtfsInfo
.BytesPerCluster
;
255 ReadLCN(Vcb
, lcn
, readcount
, bytes
);
264 ReadVCN(PDEVICE_EXTENSION Vcb
,
265 PFILE_RECORD_HEADER file
,
271 PNONRESIDENT_ATTRIBUTE NresAttr
;
274 attr
= FindAttribute(file
, type
, 0);
276 NresAttr
= (PNONRESIDENT_ATTRIBUTE
) attr
;
278 if (NresAttr
== 0 || (vcn
< NresAttr
->StartVcn
||vcn
> NresAttr
->LastVcn
))
280 // PATTRIBUTE attrList = FindAttribute(file,AttributeAttributeList,0);
281 DbgPrint("Exeption \n");
285 ReadExternalAttribute(Vcb
, NresAttr
, vcn
, count
, buffer
);
290 BOOL
bitset(PUCHAR bitmap
, ULONG i
)
292 return (bitmap
[i
>>3] & (1 << (i
& 7))) !=0;
298 FixupUpdateSequenceArray(PFILE_RECORD_HEADER file
)
300 PUSHORT usa
= (PUSHORT
)((ULONG_PTR
)file
+ file
->Ntfs
.UsaOffset
);
301 PUSHORT sector
= (PUSHORT
)file
;
304 for (i
= 1; i
< file
->Ntfs
.UsaCount
; i
++)
306 sector
[255] = usa
[i
];
313 ReadLCN(PDEVICE_EXTENSION Vcb
,
318 LARGE_INTEGER DiskSector
;
320 DiskSector
.QuadPart
= lcn
;
322 return NtfsReadSectors(Vcb
->StorageDevice
,
323 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
324 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
325 Vcb
->NtfsInfo
.BytesPerSector
,