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