[FASTFAT]
[reactos.git] / reactos / drivers / filesystems / fastfat / fcb.c
1 /*
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)
10 */
11
12 /* ------------------------------------------------------- INCLUDES */
13
14 #include "vfat.h"
15
16 #define NDEBUG
17 #include <debug.h>
18
19 #ifdef __GNUC__
20 #include <wctype.h> /* towlower prototype */
21 #endif
22
23 /* -------------------------------------------------------- DEFINES */
24
25 #define TAG_FCB 'BCFV'
26
27 /* -------------------------------------------------------- PUBLICS */
28
29 static
30 ULONG
31 vfatNameHash(
32 ULONG hash,
33 PUNICODE_STRING NameU)
34 {
35 PWCHAR last;
36 PWCHAR curr;
37 register WCHAR c;
38
39 // LFN could start from "."
40 //ASSERT(NameU->Buffer[0] != L'.');
41 curr = NameU->Buffer;
42 last = NameU->Buffer + NameU->Length / sizeof(WCHAR);
43
44 while(curr < last)
45 {
46 c = towlower(*curr++);
47 hash = (hash + (c << 4) + (c >> 4)) * 11;
48 }
49 return hash;
50 }
51
52 VOID
53 vfatSplitPathName(
54 PUNICODE_STRING PathNameU,
55 PUNICODE_STRING DirNameU,
56 PUNICODE_STRING FileNameU)
57 {
58 PWCHAR pName;
59 USHORT Length = 0;
60 pName = PathNameU->Buffer + PathNameU->Length / sizeof(WCHAR) - 1;
61 while (*pName != L'\\' && pName >= PathNameU->Buffer)
62 {
63 pName--;
64 Length++;
65 }
66 ASSERT(*pName == L'\\' || pName < PathNameU->Buffer);
67 if (FileNameU)
68 {
69 FileNameU->Buffer = pName + 1;
70 FileNameU->Length = FileNameU->MaximumLength = Length * sizeof(WCHAR);
71 }
72 if (DirNameU)
73 {
74 DirNameU->Buffer = PathNameU->Buffer;
75 DirNameU->Length = (pName + 1 - PathNameU->Buffer) * sizeof(WCHAR);
76 DirNameU->MaximumLength = DirNameU->Length;
77 }
78 }
79
80 static
81 VOID
82 vfatInitFcb(
83 PVFATFCB Fcb,
84 PUNICODE_STRING NameU)
85 {
86 USHORT PathNameBufferLength;
87
88 if (NameU)
89 PathNameBufferLength = NameU->Length + sizeof(WCHAR);
90 else
91 PathNameBufferLength = 0;
92
93 Fcb->PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameBufferLength, TAG_FCB);
94 if (!Fcb->PathNameBuffer)
95 {
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);
99 }
100
101 Fcb->PathNameU.Length = 0;
102 Fcb->PathNameU.Buffer = Fcb->PathNameBuffer;
103 Fcb->PathNameU.MaximumLength = PathNameBufferLength;
104 Fcb->ShortNameU.Length = 0;
105 Fcb->ShortNameU.Buffer = Fcb->ShortNameBuffer;
106 Fcb->ShortNameU.MaximumLength = sizeof(Fcb->ShortNameBuffer);
107 Fcb->DirNameU.Buffer = Fcb->PathNameU.Buffer;
108 if (NameU && NameU->Length)
109 {
110 RtlCopyUnicodeString(&Fcb->PathNameU, NameU);
111 vfatSplitPathName(&Fcb->PathNameU, &Fcb->DirNameU, &Fcb->LongNameU);
112 }
113 else
114 {
115 Fcb->DirNameU.Buffer = Fcb->LongNameU.Buffer = NULL;
116 Fcb->DirNameU.MaximumLength = Fcb->DirNameU.Length = 0;
117 Fcb->LongNameU.MaximumLength = Fcb->LongNameU.Length = 0;
118 }
119 RtlZeroMemory(&Fcb->FCBShareAccess, sizeof(SHARE_ACCESS));
120 Fcb->OpenHandleCount = 0;
121 }
122
123 PVFATFCB
124 vfatNewFCB(
125 PDEVICE_EXTENSION pVCB,
126 PUNICODE_STRING pFileNameU)
127 {
128 PVFATFCB rcFCB;
129
130 DPRINT("'%wZ'\n", pFileNameU);
131
132 rcFCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->FcbLookasideList);
133 if (rcFCB == NULL)
134 {
135 return NULL;
136 }
137 RtlZeroMemory(rcFCB, sizeof(VFATFCB));
138 vfatInitFcb(rcFCB, pFileNameU);
139 if (pVCB->Flags & VCB_IS_FATX)
140 {
141 rcFCB->Flags |= FCB_IS_FATX_ENTRY;
142 rcFCB->Attributes = &rcFCB->entry.FatX.Attrib;
143 }
144 else
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);
157
158 return rcFCB;
159 }
160
161 static
162 VOID
163 vfatDelFCBFromTable(
164 PDEVICE_EXTENSION pVCB,
165 PVFATFCB pFCB)
166 {
167 ULONG Index;
168 ULONG ShortIndex;
169 HASHENTRY* entry;
170
171 Index = pFCB->Hash.Hash % pVCB->HashTableSize;
172 ShortIndex = pFCB->ShortHash.Hash % pVCB->HashTableSize;
173
174 if (pFCB->Hash.Hash != pFCB->ShortHash.Hash)
175 {
176 entry = pVCB->FcbHashTable[ShortIndex];
177 if (entry->self == pFCB)
178 {
179 pVCB->FcbHashTable[ShortIndex] = entry->next;
180 }
181 else
182 {
183 while (entry->next->self != pFCB)
184 {
185 entry = entry->next;
186 }
187 entry->next = pFCB->ShortHash.next;
188 }
189 }
190 entry = pVCB->FcbHashTable[Index];
191 if (entry->self == pFCB)
192 {
193 pVCB->FcbHashTable[Index] = entry->next;
194 }
195 else
196 {
197 while (entry->next->self != pFCB)
198 {
199 entry = entry->next;
200 }
201 entry->next = pFCB->Hash.next;
202 }
203
204 RemoveEntryList(&pFCB->FcbListEntry);
205 }
206
207 static
208 NTSTATUS
209 vfatMakeFullName(
210 PVFATFCB directoryFCB,
211 PUNICODE_STRING LongNameU,
212 PUNICODE_STRING ShortNameU,
213 PUNICODE_STRING NameU)
214 {
215 PWCHAR PathNameBuffer;
216 USHORT PathNameLength;
217
218 PathNameLength = directoryFCB->PathNameU.Length + max(LongNameU->Length, ShortNameU->Length);
219 if (!vfatFCBIsRoot(directoryFCB))
220 {
221 PathNameLength += sizeof(WCHAR);
222 }
223
224 if (PathNameLength > LONGNAME_MAX_LENGTH * sizeof(WCHAR))
225 {
226 return STATUS_OBJECT_NAME_INVALID;
227 }
228 PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameLength + sizeof(WCHAR), TAG_FCB);
229 if (!PathNameBuffer)
230 {
231 return STATUS_INSUFFICIENT_RESOURCES;
232 }
233 NameU->Buffer = PathNameBuffer;
234 NameU->Length = 0;
235 NameU->MaximumLength = PathNameLength;
236
237 RtlCopyUnicodeString(NameU, &directoryFCB->PathNameU);
238 if (!vfatFCBIsRoot(directoryFCB))
239 {
240 RtlAppendUnicodeToString(NameU, L"\\");
241 }
242 if (LongNameU->Length > 0)
243 {
244 RtlAppendUnicodeStringToString(NameU, LongNameU);
245 }
246 else
247 {
248 RtlAppendUnicodeStringToString(NameU, ShortNameU);
249 }
250 NameU->Buffer[NameU->Length / sizeof(WCHAR)] = 0;
251
252 return STATUS_SUCCESS;
253 }
254
255 VOID
256 vfatDestroyCCB(
257 PVFATCCB pCcb)
258 {
259 if (pCcb->SearchPattern.Buffer)
260 {
261 ExFreePoolWithTag(pCcb->SearchPattern.Buffer, TAG_VFAT);
262 }
263 ExFreeToNPagedLookasideList(&VfatGlobalData->CcbLookasideList, pCcb);
264 }
265
266 VOID
267 vfatDestroyFCB(
268 PVFATFCB pFCB)
269 {
270 FsRtlUninitializeFileLock(&pFCB->FileLock);
271 if (!vfatFCBIsRoot(pFCB) &&
272 !BooleanFlagOn(pFCB->Flags, FCB_IS_FAT) && !BooleanFlagOn(pFCB->Flags, FCB_IS_VOLUME))
273 {
274 RemoveEntryList(&pFCB->ParentListEntry);
275 }
276 ExFreePool(pFCB->PathNameBuffer);
277 ExDeleteResourceLite(&pFCB->PagingIoResource);
278 ExDeleteResourceLite(&pFCB->MainResource);
279 ExFreeToNPagedLookasideList(&VfatGlobalData->FcbLookasideList, pFCB);
280 ASSERT(IsListEmpty(&pFCB->ParentListHead));
281 }
282
283 BOOLEAN
284 vfatFCBIsDirectory(
285 PVFATFCB FCB)
286 {
287 return ((*FCB->Attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
288 }
289
290 BOOLEAN
291 vfatFCBIsRoot(
292 PVFATFCB FCB)
293 {
294 return FCB->PathNameU.Length == sizeof(WCHAR) && FCB->PathNameU.Buffer[0] == L'\\' ? TRUE : FALSE;
295 }
296
297 VOID
298 vfatGrabFCB(
299 PDEVICE_EXTENSION pVCB,
300 PVFATFCB pFCB)
301 {
302 ASSERT(ExIsResourceAcquiredExclusive(&pVCB->DirResource));
303
304 ASSERT(pFCB != pVCB->VolumeFcb);
305 ASSERT(pFCB->RefCount > 0);
306 ++pFCB->RefCount;
307 }
308
309 VOID
310 vfatReleaseFCB(
311 PDEVICE_EXTENSION pVCB,
312 PVFATFCB pFCB)
313 {
314 PVFATFCB tmpFcb;
315
316 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
317 pFCB, &pFCB->PathNameU, pFCB->RefCount);
318
319 ASSERT(ExIsResourceAcquiredExclusive(&pVCB->DirResource));
320
321 while (pFCB)
322 {
323 ASSERT(pFCB != pVCB->VolumeFcb);
324 ASSERT(pFCB->RefCount > 0);
325 pFCB->RefCount--;
326 if (pFCB->RefCount == 0)
327 {
328 ASSERT(pFCB->OpenHandleCount == 0);
329 tmpFcb = pFCB->parentFcb;
330 vfatDelFCBFromTable(pVCB, pFCB);
331 vfatDestroyFCB(pFCB);
332 }
333 else
334 {
335 tmpFcb = NULL;
336 }
337 pFCB = tmpFcb;
338 }
339 }
340
341 static
342 VOID
343 vfatAddFCBToTable(
344 PDEVICE_EXTENSION pVCB,
345 PVFATFCB pFCB)
346 {
347 ULONG Index;
348 ULONG ShortIndex;
349
350 ASSERT(pFCB->Hash.Hash == vfatNameHash(0, &pFCB->PathNameU));
351 Index = pFCB->Hash.Hash % pVCB->HashTableSize;
352 ShortIndex = pFCB->ShortHash.Hash % pVCB->HashTableSize;
353
354 InsertTailList(&pVCB->FcbListHead, &pFCB->FcbListEntry);
355
356 pFCB->Hash.next = pVCB->FcbHashTable[Index];
357 pVCB->FcbHashTable[Index] = &pFCB->Hash;
358 if (pFCB->Hash.Hash != pFCB->ShortHash.Hash)
359 {
360 pFCB->ShortHash.next = pVCB->FcbHashTable[ShortIndex];
361 pVCB->FcbHashTable[ShortIndex] = &pFCB->ShortHash;
362 }
363 if (pFCB->parentFcb)
364 {
365 vfatGrabFCB(pVCB, pFCB->parentFcb);
366 }
367 }
368
369 static
370 VOID
371 vfatInitFCBFromDirEntry(
372 PDEVICE_EXTENSION Vcb,
373 PVFATFCB Fcb,
374 PVFAT_DIRENTRY_CONTEXT DirContext)
375 {
376 ULONG Size;
377
378 RtlCopyMemory(&Fcb->entry, &DirContext->DirEntry, sizeof (DIR_ENTRY));
379 RtlCopyUnicodeString(&Fcb->ShortNameU, &DirContext->ShortNameU);
380 Fcb->Hash.Hash = vfatNameHash(0, &Fcb->PathNameU);
381 if (Vcb->Flags & VCB_IS_FATX)
382 {
383 Fcb->ShortHash.Hash = Fcb->Hash.Hash;
384 }
385 else
386 {
387 Fcb->ShortHash.Hash = vfatNameHash(0, &Fcb->DirNameU);
388 Fcb->ShortHash.Hash = vfatNameHash(Fcb->ShortHash.Hash, &Fcb->ShortNameU);
389 }
390
391 if (vfatFCBIsDirectory(Fcb))
392 {
393 ULONG FirstCluster, CurrentCluster;
394 NTSTATUS Status = STATUS_SUCCESS;
395 Size = 0;
396 FirstCluster = vfatDirEntryGetFirstCluster(Vcb, &Fcb->entry);
397 if (FirstCluster == 1)
398 {
399 Size = Vcb->FatInfo.rootDirectorySectors * Vcb->FatInfo.BytesPerSector;
400 }
401 else if (FirstCluster != 0)
402 {
403 CurrentCluster = FirstCluster;
404 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
405 {
406 Size += Vcb->FatInfo.BytesPerCluster;
407 Status = NextCluster(Vcb, FirstCluster, &CurrentCluster, FALSE);
408 }
409 }
410 }
411 else if (Fcb->Flags & FCB_IS_FATX_ENTRY)
412 {
413 Size = Fcb->entry.FatX.FileSize;
414 }
415 else
416 {
417 Size = Fcb->entry.Fat.FileSize;
418 }
419 Fcb->dirIndex = DirContext->DirIndex;
420 Fcb->startIndex = DirContext->StartIndex;
421 if ((Fcb->Flags & FCB_IS_FATX_ENTRY) && !vfatFCBIsRoot(Fcb))
422 {
423 ASSERT(DirContext->DirIndex >= 2 && DirContext->StartIndex >= 2);
424 Fcb->dirIndex = DirContext->DirIndex-2;
425 Fcb->startIndex = DirContext->StartIndex-2;
426 }
427 Fcb->RFCB.FileSize.QuadPart = Size;
428 Fcb->RFCB.ValidDataLength.QuadPart = Size;
429 Fcb->RFCB.AllocationSize.QuadPart = ROUND_UP_64(Size, Vcb->FatInfo.BytesPerCluster);
430 }
431
432 NTSTATUS
433 vfatSetFCBNewDirName(
434 PDEVICE_EXTENSION pVCB,
435 PVFATFCB Fcb,
436 PVFATFCB ParentFcb)
437 {
438 NTSTATUS Status;
439 UNICODE_STRING NewNameU;
440
441 /* Get full path name */
442 Status = vfatMakeFullName(ParentFcb, &Fcb->LongNameU, &Fcb->ShortNameU, &NewNameU);
443 if (!NT_SUCCESS(Status))
444 {
445 return Status;
446 }
447
448 /* Delete old name */
449 if (Fcb->PathNameBuffer)
450 {
451 ExFreePoolWithTag(Fcb->PathNameBuffer, TAG_FCB);
452 }
453 Fcb->PathNameU = NewNameU;
454
455 /* Delete from table */
456 vfatDelFCBFromTable(pVCB, Fcb);
457
458 /* Split it properly */
459 Fcb->PathNameBuffer = Fcb->PathNameU.Buffer;
460 Fcb->DirNameU.Buffer = Fcb->PathNameU.Buffer;
461 vfatSplitPathName(&Fcb->PathNameU, &Fcb->DirNameU, &Fcb->LongNameU);
462 Fcb->Hash.Hash = vfatNameHash(0, &Fcb->PathNameU);
463 if (pVCB->Flags & VCB_IS_FATX)
464 {
465 Fcb->ShortHash.Hash = Fcb->Hash.Hash;
466 }
467 else
468 {
469 Fcb->ShortHash.Hash = vfatNameHash(0, &Fcb->DirNameU);
470 Fcb->ShortHash.Hash = vfatNameHash(Fcb->ShortHash.Hash, &Fcb->ShortNameU);
471 }
472
473 vfatAddFCBToTable(pVCB, Fcb);
474 vfatReleaseFCB(pVCB, ParentFcb);
475
476 return STATUS_SUCCESS;
477 }
478
479 NTSTATUS
480 vfatUpdateFCB(
481 PDEVICE_EXTENSION pVCB,
482 PVFATFCB Fcb,
483 PVFAT_DIRENTRY_CONTEXT DirContext,
484 PVFATFCB ParentFcb)
485 {
486 NTSTATUS Status;
487 PVFATFCB OldParent;
488
489 DPRINT("vfatUpdateFCB(%p, %p, %p, %p)\n", pVCB, Fcb, DirContext, ParentFcb);
490
491 /* Get full path name */
492 Status = vfatMakeFullName(ParentFcb, &DirContext->LongNameU, &DirContext->ShortNameU, &Fcb->PathNameU);
493 if (!NT_SUCCESS(Status))
494 {
495 return Status;
496 }
497
498 /* Delete old name */
499 if (Fcb->PathNameBuffer)
500 {
501 ExFreePoolWithTag(Fcb->PathNameBuffer, TAG_FCB);
502 }
503
504 /* Delete from table */
505 vfatDelFCBFromTable(pVCB, Fcb);
506
507 /* Split it properly */
508 Fcb->PathNameBuffer = Fcb->PathNameU.Buffer;
509 Fcb->DirNameU.Buffer = Fcb->PathNameU.Buffer;
510 vfatSplitPathName(&Fcb->PathNameU, &Fcb->DirNameU, &Fcb->LongNameU);
511
512 /* Save old parent */
513 OldParent = Fcb->parentFcb;
514 RemoveEntryList(&Fcb->ParentListEntry);
515
516 /* Reinit FCB */
517 vfatInitFCBFromDirEntry(pVCB, Fcb, DirContext);
518
519 if (vfatFCBIsDirectory(Fcb))
520 {
521 CcFlushCache(&Fcb->SectionObjectPointers, NULL, 0, NULL);
522 }
523 Fcb->parentFcb = ParentFcb;
524 InsertTailList(&ParentFcb->ParentListHead, &Fcb->ParentListEntry);
525 vfatAddFCBToTable(pVCB, Fcb);
526
527 /* If we moved across directories, dereference our old parent
528 * We also dereference in case we're just renaming since AddFCBToTable references it
529 */
530 vfatReleaseFCB(pVCB, OldParent);
531
532 return STATUS_SUCCESS;
533 }
534
535 PVFATFCB
536 vfatGrabFCBFromTable(
537 PDEVICE_EXTENSION pVCB,
538 PUNICODE_STRING PathNameU)
539 {
540 PVFATFCB rcFCB;
541 ULONG Hash;
542 UNICODE_STRING DirNameU;
543 UNICODE_STRING FileNameU;
544 PUNICODE_STRING FcbNameU;
545
546 HASHENTRY* entry;
547
548 DPRINT("'%wZ'\n", PathNameU);
549
550 ASSERT(PathNameU->Length >= sizeof(WCHAR) && PathNameU->Buffer[0] == L'\\');
551 Hash = vfatNameHash(0, PathNameU);
552
553 entry = pVCB->FcbHashTable[Hash % pVCB->HashTableSize];
554 if (entry)
555 {
556 vfatSplitPathName(PathNameU, &DirNameU, &FileNameU);
557 }
558
559 while (entry)
560 {
561 if (entry->Hash == Hash)
562 {
563 rcFCB = entry->self;
564 DPRINT("'%wZ' '%wZ'\n", &DirNameU, &rcFCB->DirNameU);
565 if (RtlEqualUnicodeString(&DirNameU, &rcFCB->DirNameU, TRUE))
566 {
567 if (rcFCB->Hash.Hash == Hash)
568 {
569 FcbNameU = &rcFCB->LongNameU;
570 }
571 else
572 {
573 FcbNameU = &rcFCB->ShortNameU;
574 }
575 /* compare the file name */
576 DPRINT("'%wZ' '%wZ'\n", &FileNameU, FcbNameU);
577 if (RtlEqualUnicodeString(&FileNameU, FcbNameU, TRUE))
578 {
579 vfatGrabFCB(pVCB, rcFCB);
580 return rcFCB;
581 }
582 }
583 }
584 entry = entry->next;
585 }
586 return NULL;
587 }
588
589 static
590 NTSTATUS
591 vfatFCBInitializeCacheFromVolume(
592 PVCB vcb,
593 PVFATFCB fcb)
594 {
595 PFILE_OBJECT fileObject;
596 PVFATCCB newCCB;
597 NTSTATUS status;
598
599 fileObject = IoCreateStreamFileObject (NULL, vcb->StorageDevice);
600
601 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
602 if (newCCB == NULL)
603 {
604 ObDereferenceObject(fileObject);
605 return STATUS_INSUFFICIENT_RESOURCES;
606 }
607 RtlZeroMemory(newCCB, sizeof (VFATCCB));
608
609 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
610 fileObject->FsContext = fcb;
611 fileObject->FsContext2 = newCCB;
612 fcb->FileObject = fileObject;
613
614 _SEH2_TRY
615 {
616 CcInitializeCacheMap(fileObject,
617 (PCC_FILE_SIZES)(&fcb->RFCB.AllocationSize),
618 TRUE,
619 &VfatGlobalData->CacheMgrCallbacks,
620 fcb);
621 }
622 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
623 {
624 status = _SEH2_GetExceptionCode();
625 fcb->FileObject = NULL;
626 ExFreeToNPagedLookasideList(&VfatGlobalData->CcbLookasideList, newCCB);
627 ObDereferenceObject(fileObject);
628 return status;
629 }
630 _SEH2_END;
631
632 vfatGrabFCB(vcb, fcb);
633 fcb->Flags |= FCB_CACHE_INITIALIZED;
634 return STATUS_SUCCESS;
635 }
636
637 PVFATFCB
638 vfatMakeRootFCB(
639 PDEVICE_EXTENSION pVCB)
640 {
641 PVFATFCB FCB;
642 ULONG FirstCluster, CurrentCluster, Size = 0;
643 NTSTATUS Status = STATUS_SUCCESS;
644 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
645
646 FCB = vfatNewFCB(pVCB, &NameU);
647 if (FCB->Flags & FCB_IS_FATX_ENTRY)
648 {
649 memset(FCB->entry.FatX.Filename, ' ', 42);
650 FCB->entry.FatX.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
651 FCB->entry.FatX.Attrib = FILE_ATTRIBUTE_DIRECTORY;
652 FCB->entry.FatX.FirstCluster = 1;
653 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
654 }
655 else
656 {
657 memset(FCB->entry.Fat.ShortName, ' ', 11);
658 FCB->entry.Fat.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
659 FCB->entry.Fat.Attrib = FILE_ATTRIBUTE_DIRECTORY;
660 if (pVCB->FatInfo.FatType == FAT32)
661 {
662 CurrentCluster = FirstCluster = pVCB->FatInfo.RootCluster;
663 FCB->entry.Fat.FirstCluster = (unsigned short)(FirstCluster & 0xffff);
664 FCB->entry.Fat.FirstClusterHigh = (unsigned short)(FirstCluster >> 16);
665
666 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
667 {
668 Size += pVCB->FatInfo.BytesPerCluster;
669 Status = NextCluster (pVCB, FirstCluster, &CurrentCluster, FALSE);
670 }
671 }
672 else
673 {
674 FCB->entry.Fat.FirstCluster = 1;
675 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
676 }
677 }
678 FCB->ShortHash.Hash = FCB->Hash.Hash;
679 FCB->RefCount = 2;
680 FCB->dirIndex = 0;
681 FCB->RFCB.FileSize.QuadPart = Size;
682 FCB->RFCB.ValidDataLength.QuadPart = Size;
683 FCB->RFCB.AllocationSize.QuadPart = Size;
684 FCB->RFCB.IsFastIoPossible = FastIoIsNotPossible;
685
686 vfatFCBInitializeCacheFromVolume(pVCB, FCB);
687 vfatAddFCBToTable(pVCB, FCB);
688
689 return FCB;
690 }
691
692 PVFATFCB
693 vfatOpenRootFCB(
694 PDEVICE_EXTENSION pVCB)
695 {
696 PVFATFCB FCB;
697 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
698
699 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
700 if (FCB == NULL)
701 {
702 FCB = vfatMakeRootFCB(pVCB);
703 }
704
705 return FCB;
706 }
707
708 NTSTATUS
709 vfatMakeFCBFromDirEntry(
710 PVCB vcb,
711 PVFATFCB directoryFCB,
712 PVFAT_DIRENTRY_CONTEXT DirContext,
713 PVFATFCB *fileFCB)
714 {
715 PVFATFCB rcFCB;
716 UNICODE_STRING NameU;
717 NTSTATUS Status;
718
719 Status = vfatMakeFullName(directoryFCB, &DirContext->LongNameU, &DirContext->ShortNameU, &NameU);
720 if (!NT_SUCCESS(Status))
721 {
722 return Status;
723 }
724
725 rcFCB = vfatNewFCB(vcb, &NameU);
726 vfatInitFCBFromDirEntry(vcb, rcFCB, DirContext);
727
728 rcFCB->RefCount = 1;
729 if (vfatFCBIsDirectory(rcFCB))
730 {
731 vfatFCBInitializeCacheFromVolume(vcb, rcFCB);
732 }
733 rcFCB->parentFcb = directoryFCB;
734 InsertTailList(&directoryFCB->ParentListHead, &rcFCB->ParentListEntry);
735 vfatAddFCBToTable(vcb, rcFCB);
736 *fileFCB = rcFCB;
737
738 ExFreePool(NameU.Buffer);
739 return STATUS_SUCCESS;
740 }
741
742 NTSTATUS
743 vfatAttachFCBToFileObject(
744 PDEVICE_EXTENSION vcb,
745 PVFATFCB fcb,
746 PFILE_OBJECT fileObject)
747 {
748 PVFATCCB newCCB;
749
750 UNREFERENCED_PARAMETER(vcb);
751
752 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
753 if (newCCB == NULL)
754 {
755 return STATUS_INSUFFICIENT_RESOURCES;
756 }
757 RtlZeroMemory(newCCB, sizeof (VFATCCB));
758
759 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
760 fileObject->FsContext = fcb;
761 fileObject->FsContext2 = newCCB;
762 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb, &fcb->PathNameU);
763
764 return STATUS_SUCCESS;
765 }
766
767 NTSTATUS
768 vfatDirFindFile(
769 PDEVICE_EXTENSION pDeviceExt,
770 PVFATFCB pDirectoryFCB,
771 PUNICODE_STRING FileToFindU,
772 PVFATFCB *pFoundFCB)
773 {
774 NTSTATUS status;
775 PVOID Context = NULL;
776 PVOID Page = NULL;
777 BOOLEAN First = TRUE;
778 VFAT_DIRENTRY_CONTEXT DirContext;
779 /* This buffer must have a size of 260 characters, because
780 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
781 WCHAR LongNameBuffer[260];
782 WCHAR ShortNameBuffer[13];
783 BOOLEAN FoundLong = FALSE;
784 BOOLEAN FoundShort = FALSE;
785
786 ASSERT(pDeviceExt);
787 ASSERT(pDirectoryFCB);
788 ASSERT(FileToFindU);
789
790 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
791 pDeviceExt, pDirectoryFCB, FileToFindU);
792 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB->PathNameU);
793
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);
801
802 while (TRUE)
803 {
804 status = pDeviceExt->GetNextDirEntry(&Context,
805 &Page,
806 pDirectoryFCB,
807 &DirContext,
808 First);
809 First = FALSE;
810 if (status == STATUS_NO_MORE_ENTRIES)
811 {
812 return STATUS_OBJECT_NAME_NOT_FOUND;
813 }
814 if (!NT_SUCCESS(status))
815 {
816 return status;
817 }
818
819 DPRINT(" Index:%u longName:%wZ\n",
820 DirContext.DirIndex, &DirContext.LongNameU);
821
822 if (!ENTRY_VOLUME(pDeviceExt, &DirContext.DirEntry))
823 {
824 if (DirContext.LongNameU.Length == 0 ||
825 DirContext.ShortNameU.Length == 0)
826 {
827 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
828 if (VfatGlobalData->Flags & VFAT_BREAK_ON_CORRUPTION)
829 {
830 ASSERT(DirContext.LongNameU.Length != 0 &&
831 DirContext.ShortNameU.Length != 0);
832 }
833 DirContext.DirIndex++;
834 continue;
835 }
836 FoundLong = RtlEqualUnicodeString(FileToFindU, &DirContext.LongNameU, TRUE);
837 if (FoundLong == FALSE)
838 {
839 FoundShort = RtlEqualUnicodeString(FileToFindU, &DirContext.ShortNameU, TRUE);
840 }
841 if (FoundLong || FoundShort)
842 {
843 status = vfatMakeFCBFromDirEntry(pDeviceExt,
844 pDirectoryFCB,
845 &DirContext,
846 pFoundFCB);
847 CcUnpinData(Context);
848 return status;
849 }
850 }
851 DirContext.DirIndex++;
852 }
853
854 return STATUS_OBJECT_NAME_NOT_FOUND;
855 }
856
857 NTSTATUS
858 vfatGetFCBForFile(
859 PDEVICE_EXTENSION pVCB,
860 PVFATFCB *pParentFCB,
861 PVFATFCB *pFCB,
862 PUNICODE_STRING pFileNameU)
863 {
864 NTSTATUS status;
865 PVFATFCB FCB = NULL;
866 PVFATFCB parentFCB;
867 UNICODE_STRING NameU;
868 UNICODE_STRING RootNameU = RTL_CONSTANT_STRING(L"\\");
869 UNICODE_STRING FileNameU;
870 WCHAR NameBuffer[260];
871 PWCHAR curr, prev, last;
872 ULONG Length;
873
874 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
875 pVCB, pParentFCB, pFCB, pFileNameU);
876
877 RtlInitEmptyUnicodeString(&FileNameU, NameBuffer, sizeof(NameBuffer));
878
879 parentFCB = *pParentFCB;
880
881 if (parentFCB == NULL)
882 {
883 /* Passed-in name is the full name */
884 RtlCopyUnicodeString(&FileNameU, pFileNameU);
885
886 // Trivial case, open of the root directory on volume
887 if (RtlEqualUnicodeString(&FileNameU, &RootNameU, FALSE))
888 {
889 DPRINT("returning root FCB\n");
890
891 FCB = vfatOpenRootFCB(pVCB);
892 *pFCB = FCB;
893 *pParentFCB = NULL;
894
895 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
896 }
897
898 /* Check for an existing FCB */
899 FCB = vfatGrabFCBFromTable(pVCB, &FileNameU);
900 if (FCB)
901 {
902 *pFCB = FCB;
903 *pParentFCB = FCB->parentFcb;
904 vfatGrabFCB(pVCB, *pParentFCB);
905 return STATUS_SUCCESS;
906 }
907
908 last = curr = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
909 while (*curr != L'\\' && curr > FileNameU.Buffer)
910 {
911 curr--;
912 }
913
914 if (curr > FileNameU.Buffer)
915 {
916 NameU.Buffer = FileNameU.Buffer;
917 NameU.MaximumLength = NameU.Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
918 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
919 if (FCB)
920 {
921 Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
922 if (Length != FCB->PathNameU.Length)
923 {
924 if (FileNameU.Length + FCB->PathNameU.Length - Length > FileNameU.MaximumLength)
925 {
926 vfatReleaseFCB(pVCB, FCB);
927 return STATUS_OBJECT_NAME_INVALID;
928 }
929 RtlMoveMemory(FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR),
930 curr, FileNameU.Length - Length);
931 FileNameU.Length += (USHORT)(FCB->PathNameU.Length - Length);
932 curr = FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR);
933 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
934 }
935 RtlCopyMemory(FileNameU.Buffer, FCB->PathNameU.Buffer, FCB->PathNameU.Length);
936 }
937 }
938 else
939 {
940 FCB = NULL;
941 }
942
943 if (FCB == NULL)
944 {
945 FCB = vfatOpenRootFCB(pVCB);
946 curr = FileNameU.Buffer;
947 }
948
949 parentFCB = NULL;
950 prev = curr;
951 }
952 else
953 {
954 /* Make absolute path */
955 RtlCopyUnicodeString(&FileNameU, &parentFCB->PathNameU);
956 curr = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
957 if (*curr != L'\\')
958 {
959 RtlAppendUnicodeToString(&FileNameU, L"\\");
960 curr++;
961 }
962 ASSERT(*curr == L'\\');
963 RtlAppendUnicodeStringToString(&FileNameU, pFileNameU);
964
965 FCB = parentFCB;
966 parentFCB = NULL;
967 prev = curr;
968 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
969 }
970
971 while (curr <= last)
972 {
973 if (parentFCB)
974 {
975 vfatReleaseFCB(pVCB, parentFCB);
976 parentFCB = NULL;
977 }
978 // fail if element in FCB is not a directory
979 if (!vfatFCBIsDirectory(FCB))
980 {
981 DPRINT ("Element in requested path is not a directory\n");
982
983 vfatReleaseFCB(pVCB, FCB);
984 FCB = NULL;
985 *pParentFCB = NULL;
986 *pFCB = NULL;
987
988 return STATUS_OBJECT_PATH_NOT_FOUND;
989 }
990 parentFCB = FCB;
991 if (prev < curr)
992 {
993 Length = (curr - prev) * sizeof(WCHAR);
994 if (Length != parentFCB->LongNameU.Length)
995 {
996 if (FileNameU.Length + parentFCB->LongNameU.Length - Length > FileNameU.MaximumLength)
997 {
998 vfatReleaseFCB(pVCB, parentFCB);
999 *pParentFCB = NULL;
1000 *pFCB = NULL;
1001 return STATUS_OBJECT_NAME_INVALID;
1002 }
1003 RtlMoveMemory(prev + parentFCB->LongNameU.Length / sizeof(WCHAR), curr,
1004 FileNameU.Length - (curr - FileNameU.Buffer) * sizeof(WCHAR));
1005 FileNameU.Length += (USHORT)(parentFCB->LongNameU.Length - Length);
1006 curr = prev + parentFCB->LongNameU.Length / sizeof(WCHAR);
1007 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
1008 }
1009 RtlCopyMemory(prev, parentFCB->LongNameU.Buffer, parentFCB->LongNameU.Length);
1010 }
1011 curr++;
1012 prev = curr;
1013 while (*curr != L'\\' && curr <= last)
1014 {
1015 curr++;
1016 }
1017 NameU.Buffer = FileNameU.Buffer;
1018 NameU.Length = (curr - NameU.Buffer) * sizeof(WCHAR);
1019 NameU.MaximumLength = FileNameU.MaximumLength;
1020 DPRINT("%wZ\n", &NameU);
1021 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
1022 if (FCB == NULL)
1023 {
1024 NameU.Buffer = prev;
1025 NameU.MaximumLength = NameU.Length = (curr - prev) * sizeof(WCHAR);
1026 status = vfatDirFindFile(pVCB, parentFCB, &NameU, &FCB);
1027 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1028 {
1029 *pFCB = NULL;
1030 if (curr > last)
1031 {
1032 *pParentFCB = parentFCB;
1033 return STATUS_OBJECT_NAME_NOT_FOUND;
1034 }
1035 else
1036 {
1037 vfatReleaseFCB(pVCB, parentFCB);
1038 *pParentFCB = NULL;
1039 return STATUS_OBJECT_PATH_NOT_FOUND;
1040 }
1041 }
1042 else if (!NT_SUCCESS(status))
1043 {
1044 vfatReleaseFCB(pVCB, parentFCB);
1045 *pParentFCB = NULL;
1046 *pFCB = NULL;
1047
1048 return status;
1049 }
1050 }
1051 }
1052
1053 *pParentFCB = parentFCB;
1054 *pFCB = FCB;
1055
1056 return STATUS_SUCCESS;
1057 }