d1c1591dd6790e1ae6e04866fc971f956d94d407
[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 return ((Fcb->Entry.FileAttributes & NTFS_FILE_TYPE_DIRECTORY) == NTFS_FILE_TYPE_DIRECTORY);
122 }
123
124
125 BOOLEAN
126 NtfsFCBIsRoot(PNTFS_FCB Fcb)
127 {
128 return (wcscmp(Fcb->PathName, L"\\") == 0);
129 }
130
131
132 VOID
133 NtfsGrabFCB(PNTFS_VCB Vcb,
134 PNTFS_FCB Fcb)
135 {
136 KIRQL oldIrql;
137
138 DPRINT("grabbing FCB at %p: %S, refCount:%d\n",
139 Fcb,
140 Fcb->PathName,
141 Fcb->RefCount);
142
143 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
144 Fcb->RefCount++;
145 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
146 }
147
148
149 VOID
150 NtfsReleaseFCB(PNTFS_VCB Vcb,
151 PNTFS_FCB Fcb)
152 {
153 KIRQL oldIrql;
154
155 DPRINT("releasing FCB at %p: %S, refCount:%d\n",
156 Fcb,
157 Fcb->PathName,
158 Fcb->RefCount);
159
160 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
161 Fcb->RefCount--;
162 if (Fcb->RefCount <= 0 && !NtfsFCBIsDirectory(Fcb))
163 {
164 RemoveEntryList(&Fcb->FcbListEntry);
165 CcUninitializeCacheMap(Fcb->FileObject, NULL, NULL);
166 NtfsDestroyFCB(Fcb);
167 }
168
169 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
170 }
171
172
173 VOID
174 NtfsAddFCBToTable(PNTFS_VCB Vcb,
175 PNTFS_FCB Fcb)
176 {
177 KIRQL oldIrql;
178
179 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
180 Fcb->Vcb = Vcb;
181 InsertTailList(&Vcb->FcbListHead, &Fcb->FcbListEntry);
182 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
183 }
184
185
186 PNTFS_FCB
187 NtfsGrabFCBFromTable(PNTFS_VCB Vcb,
188 PCWSTR FileName)
189 {
190 KIRQL oldIrql;
191 PNTFS_FCB Fcb;
192 PLIST_ENTRY current_entry;
193
194 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
195
196 if (FileName == NULL || *FileName == 0)
197 {
198 DPRINT("Return FCB for stream file object\n");
199 Fcb = Vcb->StreamFileObject->FsContext;
200 Fcb->RefCount++;
201 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
202 return Fcb;
203 }
204
205 current_entry = Vcb->FcbListHead.Flink;
206 while (current_entry != &Vcb->FcbListHead)
207 {
208 Fcb = CONTAINING_RECORD(current_entry, NTFS_FCB, FcbListEntry);
209
210 DPRINT("Comparing '%S' and '%S'\n", FileName, Fcb->PathName);
211 if (_wcsicmp(FileName, Fcb->PathName) == 0)
212 {
213 Fcb->RefCount++;
214 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
215 return Fcb;
216 }
217
218 //FIXME: need to compare against short name in FCB here
219
220 current_entry = current_entry->Flink;
221 }
222
223 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
224
225 return NULL;
226 }
227
228
229 NTSTATUS
230 NtfsFCBInitializeCache(PNTFS_VCB Vcb,
231 PNTFS_FCB Fcb)
232 {
233 PFILE_OBJECT FileObject;
234 NTSTATUS Status;
235 PNTFS_CCB newCCB;
236
237 FileObject = IoCreateStreamFileObject(NULL, Vcb->StorageDevice);
238
239 newCCB = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_CCB), TAG_CCB);
240 if (newCCB == NULL)
241 {
242 return STATUS_INSUFFICIENT_RESOURCES;
243 }
244
245 RtlZeroMemory(newCCB, sizeof(NTFS_CCB));
246
247 newCCB->Identifier.Type = NTFS_TYPE_CCB;
248 newCCB->Identifier.Size = sizeof(NTFS_TYPE_CCB);
249
250 FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
251 FileObject->FsContext = Fcb;
252 FileObject->FsContext2 = newCCB;
253 newCCB->PtrFileObject = FileObject;
254 Fcb->FileObject = FileObject;
255 Fcb->Vcb = Vcb;
256
257 Status = STATUS_SUCCESS;
258 CcInitializeCacheMap(FileObject,
259 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
260 FALSE,
261 &(NtfsGlobalData->CacheMgrCallbacks),
262 Fcb);
263
264 ObDereferenceObject(FileObject);
265 Fcb->Flags |= FCB_CACHE_INITIALIZED;
266
267 return Status;
268 }
269
270
271 PNTFS_FCB
272 NtfsMakeRootFCB(PNTFS_VCB Vcb)
273 {
274 PNTFS_FCB Fcb;
275 PFILE_RECORD_HEADER MftRecord;
276 PFILENAME_ATTRIBUTE FileName;
277
278 MftRecord = ExAllocatePoolWithTag(NonPagedPool,
279 Vcb->NtfsInfo.BytesPerFileRecord,
280 TAG_NTFS);
281 if (MftRecord == NULL)
282 {
283 return NULL;
284 }
285
286 if (!NT_SUCCESS(ReadFileRecord(Vcb, NTFS_FILE_ROOT, MftRecord)))
287 {
288 return NULL;
289 }
290
291 FileName = GetFileNameFromRecord(MftRecord);
292 if (!FileName)
293 {
294 return NULL;
295 }
296
297 Fcb = NtfsCreateFCB(L"\\", Vcb);
298 if (!Fcb)
299 {
300 return NULL;
301 }
302
303 memcpy(&Fcb->Entry, FileName, FIELD_OFFSET(FILENAME_ATTRIBUTE, NameLength));
304 Fcb->Entry.NameType = FileName->NameType;
305 Fcb->Entry.NameLength = 0;
306 Fcb->Entry.Name[0] = UNICODE_NULL;
307 Fcb->RefCount = 1;
308 Fcb->DirIndex = 0;
309 Fcb->RFCB.FileSize.QuadPart = FileName->DataSize;
310 Fcb->RFCB.ValidDataLength.QuadPart = FileName->DataSize;
311 Fcb->RFCB.AllocationSize.QuadPart = FileName->AllocatedSize;
312 Fcb->MFTIndex = NTFS_FILE_ROOT;
313
314 NtfsFCBInitializeCache(Vcb, Fcb);
315 NtfsAddFCBToTable(Vcb, Fcb);
316 NtfsGrabFCB(Vcb, Fcb);
317
318 return Fcb;
319 }
320
321
322 PNTFS_FCB
323 NtfsOpenRootFCB(PNTFS_VCB Vcb)
324 {
325 PNTFS_FCB Fcb;
326
327 Fcb = NtfsGrabFCBFromTable(Vcb, L"\\");
328 if (Fcb == NULL)
329 {
330 Fcb = NtfsMakeRootFCB(Vcb);
331 }
332
333 return Fcb;
334 }
335
336
337 #if 0
338 static VOID
339 NtfsGetDirEntryName(PDEVICE_EXTENSION DeviceExt,
340 PDIR_RECORD Record,
341 PWSTR Name)
342 /*
343 * FUNCTION: Retrieves the file name, be it in short or long file name format
344 */
345 {
346 if (Record->FileIdLength == 1 && Record->FileId[0] == 0)
347 {
348 wcscpy(Name, L".");
349 }
350 else if (Record->FileIdLength == 1 && Record->FileId[0] == 1)
351 {
352 wcscpy(Name, L"..");
353 }
354 else
355 {
356 if (DeviceExt->CdInfo.JolietLevel == 0)
357 {
358 ULONG i;
359
360 for (i = 0; i < Record->FileIdLength && Record->FileId[i] != ';'; i++)
361 Name[i] = (WCHAR)Record->FileId[i];
362 Name[i] = 0;
363 }
364 else
365 {
366 NtfsSwapString(Name, Record->FileId, Record->FileIdLength);
367 }
368 }
369
370 DPRINT("Name '%S'\n", Name);
371 }
372 #endif
373
374
375 NTSTATUS
376 NtfsMakeFCBFromDirEntry(PNTFS_VCB Vcb,
377 PNTFS_FCB DirectoryFCB,
378 PUNICODE_STRING Name,
379 PFILE_RECORD_HEADER Record,
380 ULONGLONG MFTIndex,
381 PNTFS_FCB * fileFCB)
382 {
383 WCHAR pathName[MAX_PATH];
384 PFILENAME_ATTRIBUTE FileName;
385 PNTFS_FCB rcFCB;
386
387 DPRINT1("NtfsMakeFCBFromDirEntry(%p, %p, %wZ, %p, %p)\n", Vcb, DirectoryFCB, Name, Record, fileFCB);
388
389 FileName = GetFileNameFromRecord(Record);
390 if (!FileName)
391 {
392 return STATUS_OBJECT_NAME_NOT_FOUND; // Not sure that's the best here
393 }
394
395 if (Name->Buffer[0] != 0 && wcslen(DirectoryFCB->PathName) +
396 sizeof(WCHAR) + Name->Length / sizeof(WCHAR) > MAX_PATH)
397 {
398 return STATUS_OBJECT_NAME_INVALID;
399 }
400
401 wcscpy(pathName, DirectoryFCB->PathName);
402 if (!NtfsFCBIsRoot(DirectoryFCB))
403 {
404 wcscat(pathName, L"\\");
405 }
406 wcscat(pathName, Name->Buffer);
407
408 rcFCB = NtfsCreateFCB(pathName, Vcb);
409 if (!rcFCB)
410 {
411 return STATUS_INSUFFICIENT_RESOURCES;
412 }
413
414 memcpy(&rcFCB->Entry, FileName, FIELD_OFFSET(FILENAME_ATTRIBUTE, NameLength));
415 rcFCB->Entry.NameType = FileName->NameType;
416 rcFCB->RFCB.FileSize.QuadPart = FileName->DataSize;
417 rcFCB->RFCB.ValidDataLength.QuadPart = FileName->DataSize;
418 rcFCB->RFCB.AllocationSize.QuadPart = FileName->AllocatedSize;
419
420 NtfsFCBInitializeCache(Vcb, rcFCB);
421 rcFCB->RefCount = 1;
422 rcFCB->MFTIndex = MFTIndex;
423 NtfsAddFCBToTable(Vcb, rcFCB);
424 *fileFCB = rcFCB;
425
426 return STATUS_SUCCESS;
427 }
428
429
430 NTSTATUS
431 NtfsAttachFCBToFileObject(PNTFS_VCB Vcb,
432 PNTFS_FCB Fcb,
433 PFILE_OBJECT FileObject)
434 {
435 PNTFS_CCB newCCB;
436
437 newCCB = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_CCB), TAG_CCB);
438 if (newCCB == NULL)
439 {
440 return STATUS_INSUFFICIENT_RESOURCES;
441 }
442
443 RtlZeroMemory(newCCB, sizeof(NTFS_CCB));
444
445 newCCB->Identifier.Type = NTFS_TYPE_CCB;
446 newCCB->Identifier.Size = sizeof(NTFS_TYPE_CCB);
447
448 FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
449 FileObject->FsContext = Fcb;
450 FileObject->FsContext2 = newCCB;
451 newCCB->PtrFileObject = FileObject;
452 Fcb->Vcb = Vcb;
453
454 if (!(Fcb->Flags & FCB_CACHE_INITIALIZED))
455 {
456 CcInitializeCacheMap(FileObject,
457 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
458 FALSE,
459 NULL,
460 NULL);
461
462 Fcb->Flags |= FCB_CACHE_INITIALIZED;
463 }
464
465 //DPRINT("file open: fcb:%x file size: %d\n", Fcb, Fcb->Entry.DataLengthL);
466
467 return STATUS_SUCCESS;
468 }
469
470
471 static NTSTATUS
472 NtfsDirFindFile(PNTFS_VCB Vcb,
473 PNTFS_FCB DirectoryFcb,
474 PWSTR FileToFind,
475 PNTFS_FCB *FoundFCB)
476 {
477 NTSTATUS Status;
478 ULONGLONG CurrentDir;
479 UNICODE_STRING File;
480 PFILE_RECORD_HEADER FileRecord;
481 PNTFS_ATTR_CONTEXT DataContext;
482 ULONGLONG MFTIndex;
483
484 DPRINT1("NtfsDirFindFile(%p, %p, %S, %p)\n", Vcb, DirectoryFcb, FileToFind, FoundFCB);
485
486 *FoundFCB = NULL;
487 RtlInitUnicodeString(&File, FileToFind);
488 CurrentDir = DirectoryFcb->MFTIndex;
489
490 Status = NtfsLookupFileAt(Vcb, &File, &FileRecord, &DataContext, &MFTIndex, CurrentDir);
491 if (!NT_SUCCESS(Status))
492 {
493 return Status;
494 }
495
496 Status = NtfsMakeFCBFromDirEntry(Vcb, DirectoryFcb, &File, FileRecord, MFTIndex, FoundFCB);
497 ExFreePoolWithTag(FileRecord, TAG_NTFS);
498
499 return Status;
500 }
501
502
503 NTSTATUS
504 NtfsGetFCBForFile(PNTFS_VCB Vcb,
505 PNTFS_FCB *pParentFCB,
506 PNTFS_FCB *pFCB,
507 const PWSTR pFileName)
508 {
509 NTSTATUS Status;
510 WCHAR pathName [MAX_PATH];
511 WCHAR elementName [MAX_PATH];
512 PWCHAR currentElement;
513 PNTFS_FCB FCB;
514 PNTFS_FCB parentFCB;
515
516 DPRINT("NtfsGetFCBForFile(%p, %p, %p, '%S')\n",
517 Vcb,
518 pParentFCB,
519 pFCB,
520 pFileName);
521
522 /* Dummy code */
523 // FCB = NtfsOpenRootFCB(Vcb);
524 // *pFCB = FCB;
525 // *pParentFCB = NULL;
526
527 #if 1
528 /* Trivial case, open of the root directory on volume */
529 if (pFileName [0] == L'\0' || wcscmp(pFileName, L"\\") == 0)
530 {
531 DPRINT("returning root FCB\n");
532
533 FCB = NtfsOpenRootFCB(Vcb);
534 *pFCB = FCB;
535 *pParentFCB = NULL;
536
537 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
538 }
539 else
540 {
541 currentElement = pFileName + 1;
542 wcscpy (pathName, L"\\");
543 FCB = NtfsOpenRootFCB (Vcb);
544 }
545
546 parentFCB = NULL;
547
548 /* Parse filename and check each path element for existance and access */
549 while (NtfsGetNextPathElement(currentElement) != 0)
550 {
551 /* Skip blank directory levels */
552 if ((NtfsGetNextPathElement(currentElement) - currentElement) == 0)
553 {
554 currentElement++;
555 continue;
556 }
557
558 DPRINT("Parsing, currentElement:%S\n", currentElement);
559 DPRINT(" parentFCB:%p FCB:%p\n", parentFCB, FCB);
560
561 /* Descend to next directory level */
562 if (parentFCB)
563 {
564 NtfsReleaseFCB(Vcb, parentFCB);
565 parentFCB = NULL;
566 }
567
568 /* fail if element in FCB is not a directory */
569 if (!NtfsFCBIsDirectory(FCB))
570 {
571 DPRINT("Element in requested path is not a directory\n");
572
573 NtfsReleaseFCB(Vcb, FCB);
574 FCB = 0;
575 *pParentFCB = NULL;
576 *pFCB = NULL;
577
578 return STATUS_OBJECT_PATH_NOT_FOUND;
579 }
580
581 parentFCB = FCB;
582
583 /* Extract next directory level into dirName */
584 NtfsWSubString(pathName,
585 pFileName,
586 NtfsGetNextPathElement(currentElement) - pFileName);
587 DPRINT(" pathName:%S\n", pathName);
588
589 FCB = NtfsGrabFCBFromTable(Vcb, pathName);
590 if (FCB == NULL)
591 {
592 NtfsWSubString(elementName,
593 currentElement,
594 NtfsGetNextPathElement(currentElement) - currentElement);
595 DPRINT(" elementName:%S\n", elementName);
596
597 Status = NtfsDirFindFile(Vcb, parentFCB, elementName, &FCB);
598 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
599 {
600 *pParentFCB = parentFCB;
601 *pFCB = NULL;
602 currentElement = NtfsGetNextPathElement(currentElement);
603 if (*currentElement == L'\0' || NtfsGetNextPathElement(currentElement + 1) == 0)
604 {
605 return STATUS_OBJECT_NAME_NOT_FOUND;
606 }
607 else
608 {
609 return STATUS_OBJECT_PATH_NOT_FOUND;
610 }
611 }
612 else if (!NT_SUCCESS(Status))
613 {
614 NtfsReleaseFCB(Vcb, parentFCB);
615 *pParentFCB = NULL;
616 *pFCB = NULL;
617
618 return Status;
619 }
620 }
621
622 currentElement = NtfsGetNextPathElement(currentElement);
623 }
624
625 *pParentFCB = parentFCB;
626 *pFCB = FCB;
627 #endif
628
629 return STATUS_SUCCESS;
630 }
631
632 /* EOF */