0c3a6ae807dbfba358fe75b42ab2a645cd52cfb2
[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 PWSTR Name,
358 PFILE_RECORD_HEADER Record,
359 PNTFS_FCB * fileFCB)
360 {
361 #if 0
362 WCHAR pathName[MAX_PATH];
363 PFCB rcFCB;
364 ULONG Size;
365
366 if (Name [0] != 0 && wcslen (DirectoryFCB->PathName) +
367 sizeof(WCHAR) + wcslen (Name) > MAX_PATH)
368 {
369 return(STATUS_OBJECT_NAME_INVALID);
370 }
371
372 wcscpy(pathName, DirectoryFCB->PathName);
373 if (!NtfsFCBIsRoot(DirectoryFCB))
374 {
375 wcscat(pathName, L"\\");
376 }
377
378 if (Name[0] != 0)
379 {
380 wcscat(pathName, Name);
381 }
382 else
383 {
384 WCHAR entryName[MAX_PATH];
385
386 NtfsGetDirEntryName(Vcb, Record, entryName);
387 wcscat(pathName, entryName);
388 }
389
390 rcFCB = NtfsCreateFCB(pathName, Vcb);
391 memcpy(&rcFCB->Entry, Record, sizeof(DIR_RECORD));
392
393 Size = rcFCB->Entry.DataLengthL;
394
395 rcFCB->RFCB.FileSize.QuadPart = Size;
396 rcFCB->RFCB.ValidDataLength.QuadPart = Size;
397 rcFCB->RFCB.AllocationSize.QuadPart = ROUND_UP(Size, BLOCKSIZE);
398 // DPRINT1("%S %d %d\n", longName, Size, (ULONG)rcFCB->RFCB.AllocationSize.QuadPart);
399 NtfsFCBInitializeCache(Vcb, rcFCB);
400 rcFCB->RefCount++;
401 NtfsAddFCBToTable(Vcb, rcFCB);
402 *fileFCB = rcFCB;
403
404 return(STATUS_SUCCESS);
405 #else
406 return STATUS_NOT_IMPLEMENTED;
407 #endif
408 }
409
410
411 NTSTATUS
412 NtfsAttachFCBToFileObject(PNTFS_VCB Vcb,
413 PNTFS_FCB Fcb,
414 PFILE_OBJECT FileObject)
415 {
416 PNTFS_CCB newCCB;
417
418 newCCB = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_CCB), TAG_CCB);
419 if (newCCB == NULL)
420 {
421 return STATUS_INSUFFICIENT_RESOURCES;
422 }
423
424 RtlZeroMemory(newCCB, sizeof(NTFS_CCB));
425
426 newCCB->Identifier.Type = NTFS_TYPE_CCB;
427 newCCB->Identifier.Size = sizeof(NTFS_TYPE_CCB);
428
429 FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
430 FileObject->FsContext = Fcb;
431 FileObject->FsContext2 = newCCB;
432 newCCB->PtrFileObject = FileObject;
433 Fcb->Vcb = Vcb;
434
435 if (!(Fcb->Flags & FCB_CACHE_INITIALIZED))
436 {
437 CcInitializeCacheMap(FileObject,
438 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
439 FALSE,
440 NULL,
441 NULL);
442
443 Fcb->Flags |= FCB_CACHE_INITIALIZED;
444 }
445
446 //DPRINT("file open: fcb:%x file size: %d\n", Fcb, Fcb->Entry.DataLengthL);
447
448 return STATUS_SUCCESS;
449 }
450
451
452 static NTSTATUS
453 NtfsDirFindFile(PNTFS_VCB Vcb,
454 PNTFS_FCB DirectoryFcb,
455 PWSTR FileToFind,
456 PNTFS_FCB *FoundFCB)
457 {
458 NTSTATUS Status;
459 ULONGLONG CurrentDir;
460 UNICODE_STRING File;
461 PFILE_RECORD_HEADER FileRecord;
462 PNTFS_ATTR_CONTEXT DataContext;
463
464 *FoundFCB = NULL;
465 RtlInitUnicodeString(&File, FileToFind);
466 CurrentDir = DirectoryFcb->MFTIndex;
467
468 Status = NtfsLookupFileAt(Vcb, &File, &FileRecord, &DataContext, CurrentDir);
469 if (!NT_SUCCESS(Status))
470 {
471 return Status;
472 }
473
474 Status = NtfsMakeFCBFromDirEntry(Vcb, DirectoryFcb, FileToFind, FileRecord, FoundFCB);
475 ExFreePoolWithTag(FileRecord, TAG_NTFS);
476
477 return Status;
478 }
479
480
481 NTSTATUS
482 NtfsGetFCBForFile(PNTFS_VCB Vcb,
483 PNTFS_FCB *pParentFCB,
484 PNTFS_FCB *pFCB,
485 const PWSTR pFileName)
486 {
487 NTSTATUS Status;
488 WCHAR pathName [MAX_PATH];
489 WCHAR elementName [MAX_PATH];
490 PWCHAR currentElement;
491 PNTFS_FCB FCB;
492 PNTFS_FCB parentFCB;
493
494 DPRINT("NtfsGetFCBForFile(%p, %p, %p, '%S')\n",
495 Vcb,
496 pParentFCB,
497 pFCB,
498 pFileName);
499
500 /* Dummy code */
501 // FCB = NtfsOpenRootFCB(Vcb);
502 // *pFCB = FCB;
503 // *pParentFCB = NULL;
504
505 #if 1
506 /* Trivial case, open of the root directory on volume */
507 if (pFileName [0] == L'\0' || wcscmp(pFileName, L"\\") == 0)
508 {
509 DPRINT("returning root FCB\n");
510
511 FCB = NtfsOpenRootFCB(Vcb);
512 *pFCB = FCB;
513 *pParentFCB = NULL;
514
515 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
516 }
517 else
518 {
519 currentElement = pFileName + 1;
520 wcscpy (pathName, L"\\");
521 FCB = NtfsOpenRootFCB (Vcb);
522 }
523
524 parentFCB = NULL;
525
526 /* Parse filename and check each path element for existance and access */
527 while (NtfsGetNextPathElement(currentElement) != 0)
528 {
529 /* Skip blank directory levels */
530 if ((NtfsGetNextPathElement(currentElement) - currentElement) == 0)
531 {
532 currentElement++;
533 continue;
534 }
535
536 DPRINT("Parsing, currentElement:%S\n", currentElement);
537 DPRINT(" parentFCB:%p FCB:%p\n", parentFCB, FCB);
538
539 /* Descend to next directory level */
540 if (parentFCB)
541 {
542 NtfsReleaseFCB(Vcb, parentFCB);
543 parentFCB = NULL;
544 }
545
546 /* fail if element in FCB is not a directory */
547 if (!NtfsFCBIsDirectory(FCB))
548 {
549 DPRINT("Element in requested path is not a directory\n");
550
551 NtfsReleaseFCB(Vcb, FCB);
552 FCB = 0;
553 *pParentFCB = NULL;
554 *pFCB = NULL;
555
556 return STATUS_OBJECT_PATH_NOT_FOUND;
557 }
558
559 parentFCB = FCB;
560
561 /* Extract next directory level into dirName */
562 NtfsWSubString(pathName,
563 pFileName,
564 NtfsGetNextPathElement(currentElement) - pFileName);
565 DPRINT(" pathName:%S\n", pathName);
566
567 FCB = NtfsGrabFCBFromTable(Vcb, pathName);
568 if (FCB == NULL)
569 {
570 NtfsWSubString(elementName,
571 currentElement,
572 NtfsGetNextPathElement(currentElement) - currentElement);
573 DPRINT(" elementName:%S\n", elementName);
574
575 Status = NtfsDirFindFile(Vcb, parentFCB, elementName, &FCB);
576 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
577 {
578 *pParentFCB = parentFCB;
579 *pFCB = NULL;
580 currentElement = NtfsGetNextPathElement(currentElement);
581 if (*currentElement == L'\0' || NtfsGetNextPathElement(currentElement + 1) == 0)
582 {
583 return STATUS_OBJECT_NAME_NOT_FOUND;
584 }
585 else
586 {
587 return STATUS_OBJECT_PATH_NOT_FOUND;
588 }
589 }
590 else if (!NT_SUCCESS(Status))
591 {
592 NtfsReleaseFCB(Vcb, parentFCB);
593 *pParentFCB = NULL;
594 *pFCB = NULL;
595
596 return Status;
597 }
598 }
599
600 currentElement = NtfsGetNextPathElement(currentElement);
601 }
602
603 *pParentFCB = parentFCB;
604 *pFCB = FCB;
605 #endif
606
607 return STATUS_SUCCESS;
608 }
609
610 /* EOF */