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