f0235354bf4b38293ff24790a85d71321a08d677
[reactos.git] / reactos / drivers / filesystems / ntfs / fcb.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/fcb.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #include "ntfs.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 /* MACROS *******************************************************************/
34
35 #define TAG_FCB 'BCFI'
36
37 /* FUNCTIONS ****************************************************************/
38
39 static
40 PWCHAR
41 NtfsGetNextPathElement(PWCHAR FileName)
42 {
43 if (*FileName == L'\0')
44 {
45 return NULL;
46 }
47
48 while (*FileName != L'\0' && *FileName != L'\\')
49 {
50 FileName++;
51 }
52
53 return FileName;
54 }
55
56
57 static
58 VOID
59 NtfsWSubString(PWCHAR pTarget,
60 const PWCHAR pSource,
61 size_t pLength)
62 {
63 wcsncpy(pTarget, pSource, pLength);
64 pTarget[pLength] = L'\0';
65 }
66
67
68 PNTFS_FCB
69 NtfsCreateFCB(PCWSTR FileName,
70 PNTFS_VCB Vcb)
71 {
72 PNTFS_FCB Fcb;
73
74 ASSERT(Vcb);
75 ASSERT(Vcb->Identifier.Type == NTFS_TYPE_VCB);
76
77 Fcb = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_FCB), TAG_FCB);
78 RtlZeroMemory(Fcb, sizeof(NTFS_FCB));
79
80 Fcb->Identifier.Type = NTFS_TYPE_FCB;
81 Fcb->Identifier.Size = sizeof(NTFS_TYPE_FCB);
82
83 Fcb->Vcb = Vcb;
84
85 if (FileName)
86 {
87 wcscpy(Fcb->PathName, FileName);
88 if (wcsrchr(Fcb->PathName, '\\') != 0)
89 {
90 Fcb->ObjectName = wcsrchr(Fcb->PathName, '\\');
91 }
92 else
93 {
94 Fcb->ObjectName = Fcb->PathName;
95 }
96 }
97
98 ExInitializeResourceLite(&Fcb->MainResource);
99
100 Fcb->RFCB.Resource = &(Fcb->MainResource);
101
102 return Fcb;
103 }
104
105
106 VOID
107 NtfsDestroyFCB(PNTFS_FCB Fcb)
108 {
109 ASSERT(Fcb);
110 ASSERT(Fcb->Identifier.Type == NTFS_TYPE_FCB);
111
112 ExDeleteResourceLite(&Fcb->MainResource);
113
114 ExFreePool(Fcb);
115 }
116
117
118 BOOLEAN
119 NtfsFCBIsDirectory(PNTFS_FCB Fcb)
120 {
121 UNREFERENCED_PARAMETER(Fcb);
122 // return(Fcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY);
123 // return(Fcb->Entry.FileFlags & 0x02);
124 return TRUE;
125 }
126
127
128 BOOLEAN
129 NtfsFCBIsRoot(PNTFS_FCB Fcb)
130 {
131 return (wcscmp(Fcb->PathName, L"\\") == 0);
132 }
133
134
135 VOID
136 NtfsGrabFCB(PNTFS_VCB Vcb,
137 PNTFS_FCB Fcb)
138 {
139 KIRQL oldIrql;
140
141 DPRINT("grabbing FCB at %p: %S, refCount:%d\n",
142 Fcb,
143 Fcb->PathName,
144 Fcb->RefCount);
145
146 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
147 Fcb->RefCount++;
148 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
149 }
150
151
152 VOID
153 NtfsReleaseFCB(PNTFS_VCB Vcb,
154 PNTFS_FCB Fcb)
155 {
156 KIRQL oldIrql;
157
158 DPRINT("releasing FCB at %p: %S, refCount:%d\n",
159 Fcb,
160 Fcb->PathName,
161 Fcb->RefCount);
162
163 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
164 Fcb->RefCount--;
165 if (Fcb->RefCount <= 0 && !NtfsFCBIsDirectory(Fcb))
166 {
167 RemoveEntryList(&Fcb->FcbListEntry);
168 CcUninitializeCacheMap(Fcb->FileObject, NULL, NULL);
169 NtfsDestroyFCB(Fcb);
170 }
171
172 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
173 }
174
175
176 VOID
177 NtfsAddFCBToTable(PNTFS_VCB Vcb,
178 PNTFS_FCB Fcb)
179 {
180 KIRQL oldIrql;
181
182 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
183 Fcb->Vcb = Vcb;
184 InsertTailList(&Vcb->FcbListHead, &Fcb->FcbListEntry);
185 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
186 }
187
188
189 PNTFS_FCB
190 NtfsGrabFCBFromTable(PNTFS_VCB Vcb,
191 PCWSTR FileName)
192 {
193 KIRQL oldIrql;
194 PNTFS_FCB Fcb;
195 PLIST_ENTRY current_entry;
196
197 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
198
199 if (FileName == NULL || *FileName == 0)
200 {
201 DPRINT("Return FCB for stream file object\n");
202 Fcb = Vcb->StreamFileObject->FsContext;
203 Fcb->RefCount++;
204 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
205 return Fcb;
206 }
207
208 current_entry = Vcb->FcbListHead.Flink;
209 while (current_entry != &Vcb->FcbListHead)
210 {
211 Fcb = CONTAINING_RECORD(current_entry, NTFS_FCB, FcbListEntry);
212
213 DPRINT("Comparing '%S' and '%S'\n", FileName, Fcb->PathName);
214 if (_wcsicmp(FileName, Fcb->PathName) == 0)
215 {
216 Fcb->RefCount++;
217 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
218 return Fcb;
219 }
220
221 //FIXME: need to compare against short name in FCB here
222
223 current_entry = current_entry->Flink;
224 }
225
226 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
227
228 return NULL;
229 }
230
231
232 NTSTATUS
233 NtfsFCBInitializeCache(PNTFS_VCB Vcb,
234 PNTFS_FCB Fcb)
235 {
236 PFILE_OBJECT FileObject;
237 NTSTATUS Status;
238 PNTFS_CCB newCCB;
239
240 FileObject = IoCreateStreamFileObject(NULL, Vcb->StorageDevice);
241
242 newCCB = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_CCB), TAG_CCB);
243 if (newCCB == NULL)
244 {
245 return STATUS_INSUFFICIENT_RESOURCES;
246 }
247
248 RtlZeroMemory(newCCB, sizeof(NTFS_CCB));
249
250 newCCB->Identifier.Type = NTFS_TYPE_CCB;
251 newCCB->Identifier.Size = sizeof(NTFS_TYPE_CCB);
252
253 FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
254 FileObject->FsContext = Fcb;
255 FileObject->FsContext2 = newCCB;
256 newCCB->PtrFileObject = FileObject;
257 Fcb->FileObject = FileObject;
258 Fcb->Vcb = Vcb;
259
260 Status = STATUS_SUCCESS;
261 CcInitializeCacheMap(FileObject,
262 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
263 FALSE,
264 &(NtfsGlobalData->CacheMgrCallbacks),
265 Fcb);
266
267 ObDereferenceObject(FileObject);
268 Fcb->Flags |= FCB_CACHE_INITIALIZED;
269
270 return Status;
271 }
272
273
274 PNTFS_FCB
275 NtfsMakeRootFCB(PNTFS_VCB Vcb)
276 {
277 PNTFS_FCB Fcb;
278
279 Fcb = NtfsCreateFCB(L"\\", Vcb);
280
281 // memset(Fcb->entry.Filename, ' ', 11);
282
283 // Fcb->Entry.DataLengthL = Vcb->CdInfo.RootSize;
284 // Fcb->Entry.ExtentLocationL = Vcb->CdInfo.RootStart;
285 // Fcb->Entry.FileFlags = 0x02; // FILE_ATTRIBUTE_DIRECTORY;
286 Fcb->RefCount = 1;
287 Fcb->DirIndex = 0;
288 Fcb->RFCB.FileSize.QuadPart = PAGE_SIZE;//Vcb->CdInfo.RootSize;
289 Fcb->RFCB.ValidDataLength.QuadPart = PAGE_SIZE;//Vcb->CdInfo.RootSize;
290 Fcb->RFCB.AllocationSize.QuadPart = PAGE_SIZE;//Vcb->CdInfo.RootSize;
291 Fcb->MFTIndex = NTFS_FILE_ROOT;
292
293 NtfsFCBInitializeCache(Vcb, Fcb);
294 NtfsAddFCBToTable(Vcb, Fcb);
295 NtfsGrabFCB(Vcb, Fcb);
296
297 return Fcb;
298 }
299
300
301 PNTFS_FCB
302 NtfsOpenRootFCB(PNTFS_VCB Vcb)
303 {
304 PNTFS_FCB Fcb;
305
306 Fcb = NtfsGrabFCBFromTable(Vcb, L"\\");
307 if (Fcb == NULL)
308 {
309 Fcb = NtfsMakeRootFCB(Vcb);
310 }
311
312 return Fcb;
313 }
314
315
316 #if 0
317 static VOID
318 NtfsGetDirEntryName(PDEVICE_EXTENSION DeviceExt,
319 PDIR_RECORD Record,
320 PWSTR Name)
321 /*
322 * FUNCTION: Retrieves the file name, be it in short or long file name format
323 */
324 {
325 if (Record->FileIdLength == 1 && Record->FileId[0] == 0)
326 {
327 wcscpy(Name, L".");
328 }
329 else if (Record->FileIdLength == 1 && Record->FileId[0] == 1)
330 {
331 wcscpy(Name, L"..");
332 }
333 else
334 {
335 if (DeviceExt->CdInfo.JolietLevel == 0)
336 {
337 ULONG i;
338
339 for (i = 0; i < Record->FileIdLength && Record->FileId[i] != ';'; i++)
340 Name[i] = (WCHAR)Record->FileId[i];
341 Name[i] = 0;
342 }
343 else
344 {
345 NtfsSwapString(Name, Record->FileId, Record->FileIdLength);
346 }
347 }
348
349 DPRINT("Name '%S'\n", Name);
350 }
351 #endif
352
353
354 NTSTATUS
355 NtfsMakeFCBFromDirEntry(PNTFS_VCB Vcb,
356 PNTFS_FCB DirectoryFCB,
357 PUNICODE_STRING Name,
358 PFILE_RECORD_HEADER Record,
359 ULONGLONG MFTIndex,
360 PNTFS_FCB * fileFCB)
361 {
362 WCHAR pathName[MAX_PATH];
363 PFILENAME_ATTRIBUTE FileName;
364 PNTFS_FCB rcFCB;
365
366 DPRINT1("NtfsMakeFCBFromDirEntry(%p, %p, %wZ, %p, %p)\n", Vcb, DirectoryFCB, Name, Record, fileFCB);
367
368 FileName = GetFileNameFromRecord(Record);
369 if (!FileName)
370 {
371 return STATUS_OBJECT_NAME_NOT_FOUND; // Not sure that's the best here
372 }
373
374 if (Name->Buffer[0] != 0 && wcslen(DirectoryFCB->PathName) +
375 sizeof(WCHAR) + Name->Length / sizeof(WCHAR) > MAX_PATH)
376 {
377 return STATUS_OBJECT_NAME_INVALID;
378 }
379
380 wcscpy(pathName, DirectoryFCB->PathName);
381 if (!NtfsFCBIsRoot(DirectoryFCB))
382 {
383 wcscat(pathName, L"\\");
384 }
385 wcscat(pathName, Name->Buffer);
386
387 rcFCB = NtfsCreateFCB(pathName, Vcb);
388 if (!rcFCB)
389 {
390 return STATUS_INSUFFICIENT_RESOURCES;
391 }
392
393 rcFCB->RFCB.FileSize.QuadPart = FileName->DataSize;
394 rcFCB->RFCB.ValidDataLength.QuadPart = FileName->DataSize;
395 rcFCB->RFCB.AllocationSize.QuadPart = FileName->AllocatedSize;
396
397 NtfsFCBInitializeCache(Vcb, rcFCB);
398 rcFCB->RefCount = 1;
399 rcFCB->MFTIndex = MFTIndex;
400 NtfsAddFCBToTable(Vcb, rcFCB);
401 *fileFCB = rcFCB;
402
403 return STATUS_SUCCESS;
404 }
405
406
407 NTSTATUS
408 NtfsAttachFCBToFileObject(PNTFS_VCB Vcb,
409 PNTFS_FCB Fcb,
410 PFILE_OBJECT FileObject)
411 {
412 PNTFS_CCB newCCB;
413
414 newCCB = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_CCB), TAG_CCB);
415 if (newCCB == NULL)
416 {
417 return STATUS_INSUFFICIENT_RESOURCES;
418 }
419
420 RtlZeroMemory(newCCB, sizeof(NTFS_CCB));
421
422 newCCB->Identifier.Type = NTFS_TYPE_CCB;
423 newCCB->Identifier.Size = sizeof(NTFS_TYPE_CCB);
424
425 FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
426 FileObject->FsContext = Fcb;
427 FileObject->FsContext2 = newCCB;
428 newCCB->PtrFileObject = FileObject;
429 Fcb->Vcb = Vcb;
430
431 if (!(Fcb->Flags & FCB_CACHE_INITIALIZED))
432 {
433 CcInitializeCacheMap(FileObject,
434 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
435 FALSE,
436 NULL,
437 NULL);
438
439 Fcb->Flags |= FCB_CACHE_INITIALIZED;
440 }
441
442 //DPRINT("file open: fcb:%x file size: %d\n", Fcb, Fcb->Entry.DataLengthL);
443
444 return STATUS_SUCCESS;
445 }
446
447
448 static NTSTATUS
449 NtfsDirFindFile(PNTFS_VCB Vcb,
450 PNTFS_FCB DirectoryFcb,
451 PWSTR FileToFind,
452 PNTFS_FCB *FoundFCB)
453 {
454 NTSTATUS Status;
455 ULONGLONG CurrentDir;
456 UNICODE_STRING File;
457 PFILE_RECORD_HEADER FileRecord;
458 PNTFS_ATTR_CONTEXT DataContext;
459 ULONGLONG MFTIndex;
460
461 DPRINT1("NtfsDirFindFile(%p, %p, %S, %p)\n", Vcb, DirectoryFcb, FileToFind, FoundFCB);
462
463 *FoundFCB = NULL;
464 RtlInitUnicodeString(&File, FileToFind);
465 CurrentDir = DirectoryFcb->MFTIndex;
466
467 Status = NtfsLookupFileAt(Vcb, &File, &FileRecord, &DataContext, &MFTIndex, CurrentDir);
468 if (!NT_SUCCESS(Status))
469 {
470 return Status;
471 }
472
473 Status = NtfsMakeFCBFromDirEntry(Vcb, DirectoryFcb, &File, FileRecord, MFTIndex, FoundFCB);
474 ExFreePoolWithTag(FileRecord, TAG_NTFS);
475
476 return Status;
477 }
478
479
480 NTSTATUS
481 NtfsGetFCBForFile(PNTFS_VCB Vcb,
482 PNTFS_FCB *pParentFCB,
483 PNTFS_FCB *pFCB,
484 const PWSTR pFileName)
485 {
486 NTSTATUS Status;
487 WCHAR pathName [MAX_PATH];
488 WCHAR elementName [MAX_PATH];
489 PWCHAR currentElement;
490 PNTFS_FCB FCB;
491 PNTFS_FCB parentFCB;
492
493 DPRINT("NtfsGetFCBForFile(%p, %p, %p, '%S')\n",
494 Vcb,
495 pParentFCB,
496 pFCB,
497 pFileName);
498
499 /* Dummy code */
500 // FCB = NtfsOpenRootFCB(Vcb);
501 // *pFCB = FCB;
502 // *pParentFCB = NULL;
503
504 #if 1
505 /* Trivial case, open of the root directory on volume */
506 if (pFileName [0] == L'\0' || wcscmp(pFileName, L"\\") == 0)
507 {
508 DPRINT("returning root FCB\n");
509
510 FCB = NtfsOpenRootFCB(Vcb);
511 *pFCB = FCB;
512 *pParentFCB = NULL;
513
514 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
515 }
516 else
517 {
518 currentElement = pFileName + 1;
519 wcscpy (pathName, L"\\");
520 FCB = NtfsOpenRootFCB (Vcb);
521 }
522
523 parentFCB = NULL;
524
525 /* Parse filename and check each path element for existance and access */
526 while (NtfsGetNextPathElement(currentElement) != 0)
527 {
528 /* Skip blank directory levels */
529 if ((NtfsGetNextPathElement(currentElement) - currentElement) == 0)
530 {
531 currentElement++;
532 continue;
533 }
534
535 DPRINT("Parsing, currentElement:%S\n", currentElement);
536 DPRINT(" parentFCB:%p FCB:%p\n", parentFCB, FCB);
537
538 /* Descend to next directory level */
539 if (parentFCB)
540 {
541 NtfsReleaseFCB(Vcb, parentFCB);
542 parentFCB = NULL;
543 }
544
545 /* fail if element in FCB is not a directory */
546 if (!NtfsFCBIsDirectory(FCB))
547 {
548 DPRINT("Element in requested path is not a directory\n");
549
550 NtfsReleaseFCB(Vcb, FCB);
551 FCB = 0;
552 *pParentFCB = NULL;
553 *pFCB = NULL;
554
555 return STATUS_OBJECT_PATH_NOT_FOUND;
556 }
557
558 parentFCB = FCB;
559
560 /* Extract next directory level into dirName */
561 NtfsWSubString(pathName,
562 pFileName,
563 NtfsGetNextPathElement(currentElement) - pFileName);
564 DPRINT(" pathName:%S\n", pathName);
565
566 FCB = NtfsGrabFCBFromTable(Vcb, pathName);
567 if (FCB == NULL)
568 {
569 NtfsWSubString(elementName,
570 currentElement,
571 NtfsGetNextPathElement(currentElement) - currentElement);
572 DPRINT(" elementName:%S\n", elementName);
573
574 Status = NtfsDirFindFile(Vcb, parentFCB, elementName, &FCB);
575 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
576 {
577 *pParentFCB = parentFCB;
578 *pFCB = NULL;
579 currentElement = NtfsGetNextPathElement(currentElement);
580 if (*currentElement == L'\0' || NtfsGetNextPathElement(currentElement + 1) == 0)
581 {
582 return STATUS_OBJECT_NAME_NOT_FOUND;
583 }
584 else
585 {
586 return STATUS_OBJECT_PATH_NOT_FOUND;
587 }
588 }
589 else if (!NT_SUCCESS(Status))
590 {
591 NtfsReleaseFCB(Vcb, parentFCB);
592 *pParentFCB = NULL;
593 *pFCB = NULL;
594
595 return Status;
596 }
597 }
598
599 currentElement = NtfsGetNextPathElement(currentElement);
600 }
601
602 *pParentFCB = parentFCB;
603 *pFCB = FCB;
604 #endif
605
606 return STATUS_SUCCESS;
607 }
608
609 /* EOF */