2 * FILE: drivers/filesystems/fastfat/fcb.c
3 * PURPOSE: Routines to manipulate FCBs.
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS kernel
6 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
7 * Rex Jolliff (rex@lvcablemodem.com)
8 * Herve Poussineau (reactos@poussine.freesurf.fr)
9 * Pierre Schweitzer (pierre@reactos.org)
12 /* ------------------------------------------------------- INCLUDES */
20 #include <wctype.h> /* towlower prototype */
23 /* -------------------------------------------------------- DEFINES */
25 #define TAG_FCB 'BCFV'
27 /* -------------------------------------------------------- PUBLICS */
33 PUNICODE_STRING NameU
)
39 // LFN could start from "."
40 //ASSERT(NameU->Buffer[0] != L'.');
42 last
= NameU
->Buffer
+ NameU
->Length
/ sizeof(WCHAR
);
46 c
= towlower(*curr
++);
47 hash
= (hash
+ (c
<< 4) + (c
>> 4)) * 11;
54 PUNICODE_STRING PathNameU
,
55 PUNICODE_STRING DirNameU
,
56 PUNICODE_STRING FileNameU
)
60 pName
= PathNameU
->Buffer
+ PathNameU
->Length
/ sizeof(WCHAR
) - 1;
61 while (*pName
!= L
'\\' && pName
>= PathNameU
->Buffer
)
66 ASSERT(*pName
== L
'\\' || pName
< PathNameU
->Buffer
);
69 FileNameU
->Buffer
= pName
+ 1;
70 FileNameU
->Length
= FileNameU
->MaximumLength
= Length
* sizeof(WCHAR
);
74 DirNameU
->Buffer
= PathNameU
->Buffer
;
75 DirNameU
->Length
= (pName
+ 1 - PathNameU
->Buffer
) * sizeof(WCHAR
);
76 DirNameU
->MaximumLength
= DirNameU
->Length
;
84 PUNICODE_STRING NameU
)
86 USHORT PathNameBufferLength
;
89 PathNameBufferLength
= NameU
->Length
+ sizeof(WCHAR
);
91 PathNameBufferLength
= 0;
93 Fcb
->PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameBufferLength
, TAG_FCB
);
94 if (!Fcb
->PathNameBuffer
)
96 /* FIXME: what to do if no more memory? */
97 DPRINT1("Unable to initialize FCB for filename '%wZ'\n", NameU
);
98 KeBugCheckEx(FAT_FILE_SYSTEM
, (ULONG_PTR
)Fcb
, (ULONG_PTR
)NameU
, 0, 0);
101 Fcb
->RFCB
.NodeTypeCode
= NODE_TYPE_FCB
;
102 Fcb
->RFCB
.NodeByteSize
= sizeof(VFATFCB
);
104 Fcb
->PathNameU
.Length
= 0;
105 Fcb
->PathNameU
.Buffer
= Fcb
->PathNameBuffer
;
106 Fcb
->PathNameU
.MaximumLength
= PathNameBufferLength
;
107 Fcb
->ShortNameU
.Length
= 0;
108 Fcb
->ShortNameU
.Buffer
= Fcb
->ShortNameBuffer
;
109 Fcb
->ShortNameU
.MaximumLength
= sizeof(Fcb
->ShortNameBuffer
);
110 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
111 if (NameU
&& NameU
->Length
)
113 RtlCopyUnicodeString(&Fcb
->PathNameU
, NameU
);
114 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
118 Fcb
->DirNameU
.Buffer
= Fcb
->LongNameU
.Buffer
= NULL
;
119 Fcb
->DirNameU
.MaximumLength
= Fcb
->DirNameU
.Length
= 0;
120 Fcb
->LongNameU
.MaximumLength
= Fcb
->LongNameU
.Length
= 0;
122 RtlZeroMemory(&Fcb
->FCBShareAccess
, sizeof(SHARE_ACCESS
));
123 Fcb
->OpenHandleCount
= 0;
128 PDEVICE_EXTENSION pVCB
,
129 PUNICODE_STRING pFileNameU
)
133 DPRINT("'%wZ'\n", pFileNameU
);
135 rcFCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
);
140 RtlZeroMemory(rcFCB
, sizeof(VFATFCB
));
141 vfatInitFcb(rcFCB
, pFileNameU
);
142 if (vfatVolumeIsFatX(pVCB
))
143 rcFCB
->Attributes
= &rcFCB
->entry
.FatX
.Attrib
;
145 rcFCB
->Attributes
= &rcFCB
->entry
.Fat
.Attrib
;
146 rcFCB
->Hash
.Hash
= vfatNameHash(0, &rcFCB
->PathNameU
);
147 rcFCB
->Hash
.self
= rcFCB
;
148 rcFCB
->ShortHash
.self
= rcFCB
;
149 ExInitializeResourceLite(&rcFCB
->PagingIoResource
);
150 ExInitializeResourceLite(&rcFCB
->MainResource
);
151 FsRtlInitializeFileLock(&rcFCB
->FileLock
, NULL
, NULL
);
152 ExInitializeFastMutex(&rcFCB
->LastMutex
);
153 rcFCB
->RFCB
.PagingIoResource
= &rcFCB
->PagingIoResource
;
154 rcFCB
->RFCB
.Resource
= &rcFCB
->MainResource
;
155 rcFCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
156 InitializeListHead(&rcFCB
->ParentListHead
);
164 PDEVICE_EXTENSION pVCB
,
171 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
172 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
174 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
176 entry
= pVCB
->FcbHashTable
[ShortIndex
];
177 if (entry
->self
== pFCB
)
179 pVCB
->FcbHashTable
[ShortIndex
] = entry
->next
;
183 while (entry
->next
->self
!= pFCB
)
187 entry
->next
= pFCB
->ShortHash
.next
;
190 entry
= pVCB
->FcbHashTable
[Index
];
191 if (entry
->self
== pFCB
)
193 pVCB
->FcbHashTable
[Index
] = entry
->next
;
197 while (entry
->next
->self
!= pFCB
)
201 entry
->next
= pFCB
->Hash
.next
;
204 RemoveEntryList(&pFCB
->FcbListEntry
);
210 PVFATFCB directoryFCB
,
211 PUNICODE_STRING LongNameU
,
212 PUNICODE_STRING ShortNameU
,
213 PUNICODE_STRING NameU
)
215 PWCHAR PathNameBuffer
;
216 USHORT PathNameLength
;
218 PathNameLength
= directoryFCB
->PathNameU
.Length
+ max(LongNameU
->Length
, ShortNameU
->Length
);
219 if (!vfatFCBIsRoot(directoryFCB
))
221 PathNameLength
+= sizeof(WCHAR
);
224 if (PathNameLength
> LONGNAME_MAX_LENGTH
* sizeof(WCHAR
))
226 return STATUS_OBJECT_NAME_INVALID
;
228 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameLength
+ sizeof(WCHAR
), TAG_FCB
);
231 return STATUS_INSUFFICIENT_RESOURCES
;
233 NameU
->Buffer
= PathNameBuffer
;
235 NameU
->MaximumLength
= PathNameLength
;
237 RtlCopyUnicodeString(NameU
, &directoryFCB
->PathNameU
);
238 if (!vfatFCBIsRoot(directoryFCB
))
240 RtlAppendUnicodeToString(NameU
, L
"\\");
242 if (LongNameU
->Length
> 0)
244 RtlAppendUnicodeStringToString(NameU
, LongNameU
);
248 RtlAppendUnicodeStringToString(NameU
, ShortNameU
);
250 NameU
->Buffer
[NameU
->Length
/ sizeof(WCHAR
)] = 0;
252 return STATUS_SUCCESS
;
259 if (pCcb
->SearchPattern
.Buffer
)
261 ExFreePoolWithTag(pCcb
->SearchPattern
.Buffer
, TAG_VFAT
);
263 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, pCcb
);
270 FsRtlUninitializeFileLock(&pFCB
->FileLock
);
271 if (!vfatFCBIsRoot(pFCB
) &&
272 !BooleanFlagOn(pFCB
->Flags
, FCB_IS_FAT
) && !BooleanFlagOn(pFCB
->Flags
, FCB_IS_VOLUME
))
274 RemoveEntryList(&pFCB
->ParentListEntry
);
276 ExFreePool(pFCB
->PathNameBuffer
);
277 ExDeleteResourceLite(&pFCB
->PagingIoResource
);
278 ExDeleteResourceLite(&pFCB
->MainResource
);
279 ASSERT(IsListEmpty(&pFCB
->ParentListHead
));
280 ExFreeToNPagedLookasideList(&VfatGlobalData
->FcbLookasideList
, pFCB
);
287 return FCB
->PathNameU
.Length
== sizeof(WCHAR
) && FCB
->PathNameU
.Buffer
[0] == L
'\\' ? TRUE
: FALSE
;
292 PDEVICE_EXTENSION pVCB
,
295 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
297 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
298 ASSERT(pFCB
->RefCount
> 0);
304 PDEVICE_EXTENSION pVCB
,
309 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
310 pFCB
, &pFCB
->PathNameU
, pFCB
->RefCount
);
312 ASSERT(ExIsResourceAcquiredExclusive(&pVCB
->DirResource
));
316 ASSERT(pFCB
!= pVCB
->VolumeFcb
);
317 ASSERT(pFCB
->RefCount
> 0);
319 if (pFCB
->RefCount
== 0)
321 ASSERT(pFCB
->OpenHandleCount
== 0);
322 tmpFcb
= pFCB
->parentFcb
;
323 vfatDelFCBFromTable(pVCB
, pFCB
);
324 vfatDestroyFCB(pFCB
);
337 PDEVICE_EXTENSION pVCB
,
343 ASSERT(pFCB
->Hash
.Hash
== vfatNameHash(0, &pFCB
->PathNameU
));
344 Index
= pFCB
->Hash
.Hash
% pVCB
->HashTableSize
;
345 ShortIndex
= pFCB
->ShortHash
.Hash
% pVCB
->HashTableSize
;
347 InsertTailList(&pVCB
->FcbListHead
, &pFCB
->FcbListEntry
);
349 pFCB
->Hash
.next
= pVCB
->FcbHashTable
[Index
];
350 pVCB
->FcbHashTable
[Index
] = &pFCB
->Hash
;
351 if (pFCB
->Hash
.Hash
!= pFCB
->ShortHash
.Hash
)
353 pFCB
->ShortHash
.next
= pVCB
->FcbHashTable
[ShortIndex
];
354 pVCB
->FcbHashTable
[ShortIndex
] = &pFCB
->ShortHash
;
358 vfatGrabFCB(pVCB
, pFCB
->parentFcb
);
364 vfatInitFCBFromDirEntry(
365 PDEVICE_EXTENSION Vcb
,
367 PVFAT_DIRENTRY_CONTEXT DirContext
)
371 RtlCopyMemory(&Fcb
->entry
, &DirContext
->DirEntry
, sizeof (DIR_ENTRY
));
372 RtlCopyUnicodeString(&Fcb
->ShortNameU
, &DirContext
->ShortNameU
);
373 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
374 if (vfatVolumeIsFatX(Vcb
))
376 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
380 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
381 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
384 if (vfatFCBIsDirectory(Fcb
))
386 ULONG FirstCluster
, CurrentCluster
;
387 NTSTATUS Status
= STATUS_SUCCESS
;
389 FirstCluster
= vfatDirEntryGetFirstCluster(Vcb
, &Fcb
->entry
);
390 if (FirstCluster
== 1)
392 Size
= Vcb
->FatInfo
.rootDirectorySectors
* Vcb
->FatInfo
.BytesPerSector
;
394 else if (FirstCluster
!= 0)
396 CurrentCluster
= FirstCluster
;
397 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
399 Size
+= Vcb
->FatInfo
.BytesPerCluster
;
400 Status
= NextCluster(Vcb
, FirstCluster
, &CurrentCluster
, FALSE
);
404 else if (vfatVolumeIsFatX(Vcb
))
406 Size
= Fcb
->entry
.FatX
.FileSize
;
410 Size
= Fcb
->entry
.Fat
.FileSize
;
412 Fcb
->dirIndex
= DirContext
->DirIndex
;
413 Fcb
->startIndex
= DirContext
->StartIndex
;
414 if (vfatVolumeIsFatX(Vcb
) && !vfatFCBIsRoot(Fcb
))
416 ASSERT(DirContext
->DirIndex
>= 2 && DirContext
->StartIndex
>= 2);
417 Fcb
->dirIndex
= DirContext
->DirIndex
-2;
418 Fcb
->startIndex
= DirContext
->StartIndex
-2;
420 Fcb
->RFCB
.FileSize
.QuadPart
= Size
;
421 Fcb
->RFCB
.ValidDataLength
.QuadPart
= Size
;
422 Fcb
->RFCB
.AllocationSize
.QuadPart
= ROUND_UP_64(Size
, Vcb
->FatInfo
.BytesPerCluster
);
426 vfatSetFCBNewDirName(
427 PDEVICE_EXTENSION pVCB
,
432 UNICODE_STRING NewNameU
;
434 /* Get full path name */
435 Status
= vfatMakeFullName(ParentFcb
, &Fcb
->LongNameU
, &Fcb
->ShortNameU
, &NewNameU
);
436 if (!NT_SUCCESS(Status
))
441 /* Delete old name */
442 if (Fcb
->PathNameBuffer
)
444 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
446 Fcb
->PathNameU
= NewNameU
;
448 /* Delete from table */
449 vfatDelFCBFromTable(pVCB
, Fcb
);
451 /* Split it properly */
452 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
453 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
454 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
455 Fcb
->Hash
.Hash
= vfatNameHash(0, &Fcb
->PathNameU
);
456 if (vfatVolumeIsFatX(pVCB
))
458 Fcb
->ShortHash
.Hash
= Fcb
->Hash
.Hash
;
462 Fcb
->ShortHash
.Hash
= vfatNameHash(0, &Fcb
->DirNameU
);
463 Fcb
->ShortHash
.Hash
= vfatNameHash(Fcb
->ShortHash
.Hash
, &Fcb
->ShortNameU
);
466 vfatAddFCBToTable(pVCB
, Fcb
);
467 vfatReleaseFCB(pVCB
, ParentFcb
);
469 return STATUS_SUCCESS
;
474 PDEVICE_EXTENSION pVCB
,
476 PVFAT_DIRENTRY_CONTEXT DirContext
,
482 DPRINT("vfatUpdateFCB(%p, %p, %p, %p)\n", pVCB
, Fcb
, DirContext
, ParentFcb
);
484 /* Get full path name */
485 Status
= vfatMakeFullName(ParentFcb
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &Fcb
->PathNameU
);
486 if (!NT_SUCCESS(Status
))
491 /* Delete old name */
492 if (Fcb
->PathNameBuffer
)
494 ExFreePoolWithTag(Fcb
->PathNameBuffer
, TAG_FCB
);
497 /* Delete from table */
498 vfatDelFCBFromTable(pVCB
, Fcb
);
500 /* Split it properly */
501 Fcb
->PathNameBuffer
= Fcb
->PathNameU
.Buffer
;
502 Fcb
->DirNameU
.Buffer
= Fcb
->PathNameU
.Buffer
;
503 vfatSplitPathName(&Fcb
->PathNameU
, &Fcb
->DirNameU
, &Fcb
->LongNameU
);
505 /* Save old parent */
506 OldParent
= Fcb
->parentFcb
;
507 RemoveEntryList(&Fcb
->ParentListEntry
);
510 vfatInitFCBFromDirEntry(pVCB
, Fcb
, DirContext
);
512 if (vfatFCBIsDirectory(Fcb
))
514 CcFlushCache(&Fcb
->SectionObjectPointers
, NULL
, 0, NULL
);
516 Fcb
->parentFcb
= ParentFcb
;
517 InsertTailList(&ParentFcb
->ParentListHead
, &Fcb
->ParentListEntry
);
518 vfatAddFCBToTable(pVCB
, Fcb
);
520 /* If we moved across directories, dereference our old parent
521 * We also dereference in case we're just renaming since AddFCBToTable references it
523 vfatReleaseFCB(pVCB
, OldParent
);
525 return STATUS_SUCCESS
;
529 vfatGrabFCBFromTable(
530 PDEVICE_EXTENSION pVCB
,
531 PUNICODE_STRING PathNameU
)
535 UNICODE_STRING DirNameU
;
536 UNICODE_STRING FileNameU
;
537 PUNICODE_STRING FcbNameU
;
541 DPRINT("'%wZ'\n", PathNameU
);
543 ASSERT(PathNameU
->Length
>= sizeof(WCHAR
) && PathNameU
->Buffer
[0] == L
'\\');
544 Hash
= vfatNameHash(0, PathNameU
);
546 entry
= pVCB
->FcbHashTable
[Hash
% pVCB
->HashTableSize
];
549 vfatSplitPathName(PathNameU
, &DirNameU
, &FileNameU
);
554 if (entry
->Hash
== Hash
)
557 DPRINT("'%wZ' '%wZ'\n", &DirNameU
, &rcFCB
->DirNameU
);
558 if (RtlEqualUnicodeString(&DirNameU
, &rcFCB
->DirNameU
, TRUE
))
560 if (rcFCB
->Hash
.Hash
== Hash
)
562 FcbNameU
= &rcFCB
->LongNameU
;
566 FcbNameU
= &rcFCB
->ShortNameU
;
568 /* compare the file name */
569 DPRINT("'%wZ' '%wZ'\n", &FileNameU
, FcbNameU
);
570 if (RtlEqualUnicodeString(&FileNameU
, FcbNameU
, TRUE
))
572 vfatGrabFCB(pVCB
, rcFCB
);
584 vfatFCBInitializeCacheFromVolume(
588 PFILE_OBJECT fileObject
;
592 fileObject
= IoCreateStreamFileObject (NULL
, vcb
->StorageDevice
);
594 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
597 ObDereferenceObject(fileObject
);
598 return STATUS_INSUFFICIENT_RESOURCES
;
600 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
602 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
603 fileObject
->FsContext
= fcb
;
604 fileObject
->FsContext2
= newCCB
;
605 fcb
->FileObject
= fileObject
;
609 CcInitializeCacheMap(fileObject
,
610 (PCC_FILE_SIZES
)(&fcb
->RFCB
.AllocationSize
),
612 &VfatGlobalData
->CacheMgrCallbacks
,
615 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
617 status
= _SEH2_GetExceptionCode();
618 fcb
->FileObject
= NULL
;
619 ExFreeToNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
, newCCB
);
620 ObDereferenceObject(fileObject
);
625 vfatGrabFCB(vcb
, fcb
);
626 fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
627 return STATUS_SUCCESS
;
632 PDEVICE_EXTENSION pVCB
)
635 ULONG FirstCluster
, CurrentCluster
, Size
= 0;
636 NTSTATUS Status
= STATUS_SUCCESS
;
637 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
639 FCB
= vfatNewFCB(pVCB
, &NameU
);
640 if (vfatVolumeIsFatX(pVCB
))
642 memset(FCB
->entry
.FatX
.Filename
, ' ', 42);
643 FCB
->entry
.FatX
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
644 FCB
->entry
.FatX
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
645 FCB
->entry
.FatX
.FirstCluster
= 1;
646 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
650 memset(FCB
->entry
.Fat
.ShortName
, ' ', 11);
651 FCB
->entry
.Fat
.FileSize
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
652 FCB
->entry
.Fat
.Attrib
= FILE_ATTRIBUTE_DIRECTORY
;
653 if (pVCB
->FatInfo
.FatType
== FAT32
)
655 CurrentCluster
= FirstCluster
= pVCB
->FatInfo
.RootCluster
;
656 FCB
->entry
.Fat
.FirstCluster
= (unsigned short)(FirstCluster
& 0xffff);
657 FCB
->entry
.Fat
.FirstClusterHigh
= (unsigned short)(FirstCluster
>> 16);
659 while (CurrentCluster
!= 0xffffffff && NT_SUCCESS(Status
))
661 Size
+= pVCB
->FatInfo
.BytesPerCluster
;
662 Status
= NextCluster (pVCB
, FirstCluster
, &CurrentCluster
, FALSE
);
667 FCB
->entry
.Fat
.FirstCluster
= 1;
668 Size
= pVCB
->FatInfo
.rootDirectorySectors
* pVCB
->FatInfo
.BytesPerSector
;
671 FCB
->ShortHash
.Hash
= FCB
->Hash
.Hash
;
674 FCB
->RFCB
.FileSize
.QuadPart
= Size
;
675 FCB
->RFCB
.ValidDataLength
.QuadPart
= Size
;
676 FCB
->RFCB
.AllocationSize
.QuadPart
= Size
;
677 FCB
->RFCB
.IsFastIoPossible
= FastIoIsNotPossible
;
679 vfatFCBInitializeCacheFromVolume(pVCB
, FCB
);
680 vfatAddFCBToTable(pVCB
, FCB
);
687 PDEVICE_EXTENSION pVCB
)
690 UNICODE_STRING NameU
= RTL_CONSTANT_STRING(L
"\\");
692 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
695 FCB
= vfatMakeRootFCB(pVCB
);
702 vfatMakeFCBFromDirEntry(
704 PVFATFCB directoryFCB
,
705 PVFAT_DIRENTRY_CONTEXT DirContext
,
709 UNICODE_STRING NameU
;
712 Status
= vfatMakeFullName(directoryFCB
, &DirContext
->LongNameU
, &DirContext
->ShortNameU
, &NameU
);
713 if (!NT_SUCCESS(Status
))
718 rcFCB
= vfatNewFCB(vcb
, &NameU
);
719 vfatInitFCBFromDirEntry(vcb
, rcFCB
, DirContext
);
722 if (vfatFCBIsDirectory(rcFCB
))
724 Status
= vfatFCBInitializeCacheFromVolume(vcb
, rcFCB
);
725 if (!NT_SUCCESS(Status
))
727 vfatReleaseFCB(vcb
, rcFCB
);
728 ExFreePoolWithTag(NameU
.Buffer
, TAG_FCB
);
732 rcFCB
->parentFcb
= directoryFCB
;
733 InsertTailList(&directoryFCB
->ParentListHead
, &rcFCB
->ParentListEntry
);
734 vfatAddFCBToTable(vcb
, rcFCB
);
737 ExFreePoolWithTag(NameU
.Buffer
, TAG_FCB
);
738 return STATUS_SUCCESS
;
742 vfatAttachFCBToFileObject(
743 PDEVICE_EXTENSION vcb
,
745 PFILE_OBJECT fileObject
)
749 UNREFERENCED_PARAMETER(vcb
);
751 newCCB
= ExAllocateFromNPagedLookasideList(&VfatGlobalData
->CcbLookasideList
);
754 return STATUS_INSUFFICIENT_RESOURCES
;
756 RtlZeroMemory(newCCB
, sizeof (VFATCCB
));
758 fileObject
->SectionObjectPointer
= &fcb
->SectionObjectPointers
;
759 fileObject
->FsContext
= fcb
;
760 fileObject
->FsContext2
= newCCB
;
761 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb
, &fcb
->PathNameU
);
763 return STATUS_SUCCESS
;
768 PDEVICE_EXTENSION pDeviceExt
,
769 PVFATFCB pDirectoryFCB
,
770 PUNICODE_STRING FileToFindU
,
774 PVOID Context
= NULL
;
776 BOOLEAN First
= TRUE
;
777 VFAT_DIRENTRY_CONTEXT DirContext
;
778 /* This buffer must have a size of 260 characters, because
779 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
780 WCHAR LongNameBuffer
[260];
781 WCHAR ShortNameBuffer
[13];
782 BOOLEAN FoundLong
= FALSE
;
783 BOOLEAN FoundShort
= FALSE
;
784 BOOLEAN IsFatX
= vfatVolumeIsFatX(pDeviceExt
);
787 ASSERT(pDirectoryFCB
);
790 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
791 pDeviceExt
, pDirectoryFCB
, FileToFindU
);
792 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB
->PathNameU
);
794 DirContext
.DirIndex
= 0;
795 DirContext
.LongNameU
.Buffer
= LongNameBuffer
;
796 DirContext
.LongNameU
.Length
= 0;
797 DirContext
.LongNameU
.MaximumLength
= sizeof(LongNameBuffer
);
798 DirContext
.ShortNameU
.Buffer
= ShortNameBuffer
;
799 DirContext
.ShortNameU
.Length
= 0;
800 DirContext
.ShortNameU
.MaximumLength
= sizeof(ShortNameBuffer
);
804 status
= VfatGetNextDirEntry(pDeviceExt
,
811 if (status
== STATUS_NO_MORE_ENTRIES
)
813 return STATUS_OBJECT_NAME_NOT_FOUND
;
815 if (!NT_SUCCESS(status
))
820 DPRINT(" Index:%u longName:%wZ\n",
821 DirContext
.DirIndex
, &DirContext
.LongNameU
);
823 if (!ENTRY_VOLUME(IsFatX
, &DirContext
.DirEntry
))
825 if (DirContext
.LongNameU
.Length
== 0 ||
826 DirContext
.ShortNameU
.Length
== 0)
828 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
829 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
831 ASSERT(DirContext
.LongNameU
.Length
!= 0 &&
832 DirContext
.ShortNameU
.Length
!= 0);
834 DirContext
.DirIndex
++;
837 FoundLong
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.LongNameU
, TRUE
);
838 if (FoundLong
== FALSE
)
840 FoundShort
= RtlEqualUnicodeString(FileToFindU
, &DirContext
.ShortNameU
, TRUE
);
842 if (FoundLong
|| FoundShort
)
844 status
= vfatMakeFCBFromDirEntry(pDeviceExt
,
848 CcUnpinData(Context
);
852 DirContext
.DirIndex
++;
855 return STATUS_OBJECT_NAME_NOT_FOUND
;
860 PDEVICE_EXTENSION pVCB
,
861 PVFATFCB
*pParentFCB
,
863 PUNICODE_STRING pFileNameU
)
868 UNICODE_STRING NameU
;
869 UNICODE_STRING RootNameU
= RTL_CONSTANT_STRING(L
"\\");
870 UNICODE_STRING FileNameU
;
871 WCHAR NameBuffer
[260];
872 PWCHAR curr
, prev
, last
;
875 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
876 pVCB
, pParentFCB
, pFCB
, pFileNameU
);
878 RtlInitEmptyUnicodeString(&FileNameU
, NameBuffer
, sizeof(NameBuffer
));
880 parentFCB
= *pParentFCB
;
882 if (parentFCB
== NULL
)
884 /* Passed-in name is the full name */
885 RtlCopyUnicodeString(&FileNameU
, pFileNameU
);
887 // Trivial case, open of the root directory on volume
888 if (RtlEqualUnicodeString(&FileNameU
, &RootNameU
, FALSE
))
890 DPRINT("returning root FCB\n");
892 FCB
= vfatOpenRootFCB(pVCB
);
896 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
899 /* Check for an existing FCB */
900 FCB
= vfatGrabFCBFromTable(pVCB
, &FileNameU
);
904 *pParentFCB
= FCB
->parentFcb
;
905 vfatGrabFCB(pVCB
, *pParentFCB
);
906 return STATUS_SUCCESS
;
909 last
= curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
910 while (*curr
!= L
'\\' && curr
> FileNameU
.Buffer
)
915 if (curr
> FileNameU
.Buffer
)
917 NameU
.Buffer
= FileNameU
.Buffer
;
918 NameU
.MaximumLength
= NameU
.Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
919 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
922 Length
= (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
);
923 if (Length
!= FCB
->PathNameU
.Length
)
925 if (FileNameU
.Length
+ FCB
->PathNameU
.Length
- Length
> FileNameU
.MaximumLength
)
927 vfatReleaseFCB(pVCB
, FCB
);
928 return STATUS_OBJECT_NAME_INVALID
;
930 RtlMoveMemory(FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
),
931 curr
, FileNameU
.Length
- Length
);
932 FileNameU
.Length
+= (USHORT
)(FCB
->PathNameU
.Length
- Length
);
933 curr
= FileNameU
.Buffer
+ FCB
->PathNameU
.Length
/ sizeof(WCHAR
);
934 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
936 RtlCopyMemory(FileNameU
.Buffer
, FCB
->PathNameU
.Buffer
, FCB
->PathNameU
.Length
);
946 FCB
= vfatOpenRootFCB(pVCB
);
947 curr
= FileNameU
.Buffer
;
955 /* Make absolute path */
956 RtlCopyUnicodeString(&FileNameU
, &parentFCB
->PathNameU
);
957 curr
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
960 RtlAppendUnicodeToString(&FileNameU
, L
"\\");
963 ASSERT(*curr
== L
'\\');
964 RtlAppendUnicodeStringToString(&FileNameU
, pFileNameU
);
969 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
976 vfatReleaseFCB(pVCB
, parentFCB
);
979 // fail if element in FCB is not a directory
980 if (!vfatFCBIsDirectory(FCB
))
982 DPRINT ("Element in requested path is not a directory\n");
984 vfatReleaseFCB(pVCB
, FCB
);
989 return STATUS_OBJECT_PATH_NOT_FOUND
;
994 Length
= (curr
- prev
) * sizeof(WCHAR
);
995 if (Length
!= parentFCB
->LongNameU
.Length
)
997 if (FileNameU
.Length
+ parentFCB
->LongNameU
.Length
- Length
> FileNameU
.MaximumLength
)
999 vfatReleaseFCB(pVCB
, parentFCB
);
1002 return STATUS_OBJECT_NAME_INVALID
;
1004 RtlMoveMemory(prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
), curr
,
1005 FileNameU
.Length
- (curr
- FileNameU
.Buffer
) * sizeof(WCHAR
));
1006 FileNameU
.Length
+= (USHORT
)(parentFCB
->LongNameU
.Length
- Length
);
1007 curr
= prev
+ parentFCB
->LongNameU
.Length
/ sizeof(WCHAR
);
1008 last
= FileNameU
.Buffer
+ FileNameU
.Length
/ sizeof(WCHAR
) - 1;
1010 RtlCopyMemory(prev
, parentFCB
->LongNameU
.Buffer
, parentFCB
->LongNameU
.Length
);
1014 while (*curr
!= L
'\\' && curr
<= last
)
1018 NameU
.Buffer
= FileNameU
.Buffer
;
1019 NameU
.Length
= (curr
- NameU
.Buffer
) * sizeof(WCHAR
);
1020 NameU
.MaximumLength
= FileNameU
.MaximumLength
;
1021 DPRINT("%wZ\n", &NameU
);
1022 FCB
= vfatGrabFCBFromTable(pVCB
, &NameU
);
1025 NameU
.Buffer
= prev
;
1026 NameU
.MaximumLength
= NameU
.Length
= (curr
- prev
) * sizeof(WCHAR
);
1027 status
= vfatDirFindFile(pVCB
, parentFCB
, &NameU
, &FCB
);
1028 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1033 *pParentFCB
= parentFCB
;
1034 return STATUS_OBJECT_NAME_NOT_FOUND
;
1038 vfatReleaseFCB(pVCB
, parentFCB
);
1040 return STATUS_OBJECT_PATH_NOT_FOUND
;
1043 else if (!NT_SUCCESS(status
))
1045 vfatReleaseFCB(pVCB
, parentFCB
);
1054 *pParentFCB
= parentFCB
;
1057 return STATUS_SUCCESS
;