fb4261b99b7e8c949f99b0371d442fcae9c8025e
[reactos.git] / reactos / drivers / filesystems / fastfat / create.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystems/fastfat/create.c
22 * PURPOSE: VFAT Filesystem
23 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
24 * Pierre Schweitzer (pierre@reactos.org)
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "vfat.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* FUNCTIONS *****************************************************************/
35
36 VOID
37 vfat8Dot3ToString(
38 PFAT_DIR_ENTRY pEntry,
39 PUNICODE_STRING NameU)
40 {
41 OEM_STRING StringA;
42 USHORT Length;
43 CHAR cString[12];
44
45 RtlCopyMemory(cString, pEntry->ShortName, 11);
46 cString[11] = 0;
47 if (cString[0] == 0x05)
48 {
49 cString[0] = 0xe5;
50 }
51
52 StringA.Buffer = cString;
53 for (StringA.Length = 0;
54 StringA.Length < 8 && StringA.Buffer[StringA.Length] != ' ';
55 StringA.Length++);
56 StringA.MaximumLength = StringA.Length;
57
58 RtlOemStringToUnicodeString(NameU, &StringA, FALSE);
59
60 if (pEntry->lCase & VFAT_CASE_LOWER_BASE)
61 {
62 RtlDowncaseUnicodeString(NameU, NameU, FALSE);
63 }
64
65 if (cString[8] != ' ')
66 {
67 Length = NameU->Length;
68 NameU->Buffer += Length / sizeof(WCHAR);
69 if (!FAT_ENTRY_VOLUME(pEntry))
70 {
71 Length += sizeof(WCHAR);
72 NameU->Buffer[0] = L'.';
73 NameU->Buffer++;
74 }
75 NameU->Length = 0;
76 NameU->MaximumLength -= Length;
77
78 StringA.Buffer = &cString[8];
79 for (StringA.Length = 0;
80 StringA.Length < 3 && StringA.Buffer[StringA.Length] != ' ';
81 StringA.Length++);
82 StringA.MaximumLength = StringA.Length;
83 RtlOemStringToUnicodeString(NameU, &StringA, FALSE);
84 if (pEntry->lCase & VFAT_CASE_LOWER_EXT)
85 {
86 RtlDowncaseUnicodeString(NameU, NameU, FALSE);
87 }
88 NameU->Buffer -= Length / sizeof(WCHAR);
89 NameU->Length += Length;
90 NameU->MaximumLength += Length;
91 }
92
93 NameU->Buffer[NameU->Length / sizeof(WCHAR)] = 0;
94 DPRINT("'%wZ'\n", NameU);
95 }
96
97 /*
98 * FUNCTION: Read the volume label
99 */
100 NTSTATUS
101 ReadVolumeLabel(
102 PDEVICE_EXTENSION DeviceExt,
103 PVPB Vpb)
104 {
105 PVOID Context = NULL;
106 ULONG DirIndex = 0;
107 PDIR_ENTRY Entry;
108 PVFATFCB pFcb;
109 LARGE_INTEGER FileOffset;
110 UNICODE_STRING NameU;
111 ULONG SizeDirEntry;
112 ULONG EntriesPerPage;
113 OEM_STRING StringO;
114
115 NameU.Buffer = Vpb->VolumeLabel;
116 NameU.Length = 0;
117 NameU.MaximumLength = sizeof(Vpb->VolumeLabel);
118 *(Vpb->VolumeLabel) = 0;
119 Vpb->VolumeLabelLength = 0;
120
121 if (DeviceExt->Flags & VCB_IS_FATX)
122 {
123 SizeDirEntry = sizeof(FATX_DIR_ENTRY);
124 EntriesPerPage = FATX_ENTRIES_PER_PAGE;
125 }
126 else
127 {
128 SizeDirEntry = sizeof(FAT_DIR_ENTRY);
129 EntriesPerPage = FAT_ENTRIES_PER_PAGE;
130 }
131
132 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
133 pFcb = vfatOpenRootFCB(DeviceExt);
134 ExReleaseResourceLite(&DeviceExt->DirResource);
135
136 FileOffset.QuadPart = 0;
137 if (CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, TRUE, &Context, (PVOID*)&Entry))
138 {
139 while (TRUE)
140 {
141 if (ENTRY_VOLUME(DeviceExt, Entry))
142 {
143 /* copy volume label */
144 if (DeviceExt->Flags & VCB_IS_FATX)
145 {
146 StringO.Buffer = (PCHAR)Entry->FatX.Filename;
147 StringO.MaximumLength = StringO.Length = Entry->FatX.FilenameLength;
148 RtlOemStringToUnicodeString(&NameU, &StringO, FALSE);
149 }
150 else
151 {
152 vfat8Dot3ToString(&Entry->Fat, &NameU);
153 }
154 Vpb->VolumeLabelLength = NameU.Length;
155 break;
156 }
157 if (ENTRY_END(DeviceExt, Entry))
158 {
159 break;
160 }
161 DirIndex++;
162 Entry = (PDIR_ENTRY)((ULONG_PTR)Entry + SizeDirEntry);
163 if ((DirIndex % EntriesPerPage) == 0)
164 {
165 CcUnpinData(Context);
166 FileOffset.u.LowPart += PAGE_SIZE;
167 if (!CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, TRUE, &Context, (PVOID*)&Entry))
168 {
169 Context = NULL;
170 break;
171 }
172 }
173 }
174 if (Context)
175 {
176 CcUnpinData(Context);
177 }
178 }
179 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
180 vfatReleaseFCB(DeviceExt, pFcb);
181 ExReleaseResourceLite(&DeviceExt->DirResource);
182
183 return STATUS_SUCCESS;
184 }
185
186 /*
187 * FUNCTION: Find a file
188 */
189 NTSTATUS
190 FindFile(
191 PDEVICE_EXTENSION DeviceExt,
192 PVFATFCB Parent,
193 PUNICODE_STRING FileToFindU,
194 PVFAT_DIRENTRY_CONTEXT DirContext,
195 BOOLEAN First)
196 {
197 PWCHAR PathNameBuffer;
198 USHORT PathNameBufferLength;
199 NTSTATUS Status;
200 PVOID Context = NULL;
201 PVOID Page;
202 PVFATFCB rcFcb;
203 BOOLEAN Found;
204 UNICODE_STRING PathNameU;
205 UNICODE_STRING FileToFindUpcase;
206 BOOLEAN WildCard;
207
208 DPRINT("FindFile(Parent %p, FileToFind '%wZ', DirIndex: %u)\n",
209 Parent, FileToFindU, DirContext->DirIndex);
210 DPRINT("FindFile: Path %wZ\n",&Parent->PathNameU);
211
212 PathNameBufferLength = LONGNAME_MAX_LENGTH * sizeof(WCHAR);
213 PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameBufferLength + sizeof(WCHAR), TAG_VFAT);
214 if (!PathNameBuffer)
215 {
216 return STATUS_INSUFFICIENT_RESOURCES;
217 }
218
219 PathNameU.Buffer = PathNameBuffer;
220 PathNameU.Length = 0;
221 PathNameU.MaximumLength = PathNameBufferLength;
222
223 DirContext->LongNameU.Length = 0;
224 DirContext->ShortNameU.Length = 0;
225
226 WildCard = FsRtlDoesNameContainWildCards(FileToFindU);
227
228 if (WildCard == FALSE)
229 {
230 /* if there is no '*?' in the search name, than look first for an existing fcb */
231 RtlCopyUnicodeString(&PathNameU, &Parent->PathNameU);
232 if (!vfatFCBIsRoot(Parent))
233 {
234 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = L'\\';
235 PathNameU.Length += sizeof(WCHAR);
236 }
237 RtlAppendUnicodeStringToString(&PathNameU, FileToFindU);
238 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = 0;
239 rcFcb = vfatGrabFCBFromTable(DeviceExt, &PathNameU);
240 if (rcFcb)
241 {
242 ULONG startIndex = rcFcb->startIndex;
243 if ((rcFcb->Flags & FCB_IS_FATX_ENTRY) && !vfatFCBIsRoot(Parent))
244 {
245 startIndex += 2;
246 }
247 if(startIndex >= DirContext->DirIndex)
248 {
249 RtlCopyUnicodeString(&DirContext->LongNameU, &rcFcb->LongNameU);
250 RtlCopyUnicodeString(&DirContext->ShortNameU, &rcFcb->ShortNameU);
251 RtlCopyMemory(&DirContext->DirEntry, &rcFcb->entry, sizeof(DIR_ENTRY));
252 DirContext->StartIndex = rcFcb->startIndex;
253 DirContext->DirIndex = rcFcb->dirIndex;
254 DPRINT("FindFile: new Name %wZ, DirIndex %u (%u)\n",
255 &DirContext->LongNameU, DirContext->DirIndex, DirContext->StartIndex);
256 Status = STATUS_SUCCESS;
257 }
258 else
259 {
260 DPRINT("FCB not found for %wZ\n", &PathNameU);
261 Status = STATUS_UNSUCCESSFUL;
262 }
263 vfatReleaseFCB(DeviceExt, rcFcb);
264 ExFreePool(PathNameBuffer);
265 return Status;
266 }
267 }
268
269 /* FsRtlIsNameInExpression need the searched string to be upcase,
270 * even if IgnoreCase is specified */
271 Status = RtlUpcaseUnicodeString(&FileToFindUpcase, FileToFindU, TRUE);
272 if (!NT_SUCCESS(Status))
273 {
274 ExFreePool(PathNameBuffer);
275 return Status;
276 }
277
278 while (TRUE)
279 {
280 Status = DeviceExt->GetNextDirEntry(&Context, &Page, Parent, DirContext, First);
281 First = FALSE;
282 if (Status == STATUS_NO_MORE_ENTRIES)
283 {
284 break;
285 }
286 if (ENTRY_VOLUME(DeviceExt, &DirContext->DirEntry))
287 {
288 DirContext->DirIndex++;
289 continue;
290 }
291 if (WildCard)
292 {
293 Found = FsRtlIsNameInExpression(&FileToFindUpcase, &DirContext->LongNameU, TRUE, NULL) ||
294 FsRtlIsNameInExpression(&FileToFindUpcase, &DirContext->ShortNameU, TRUE, NULL);
295 }
296 else
297 {
298 Found = FsRtlAreNamesEqual(&DirContext->LongNameU, FileToFindU, TRUE, NULL) ||
299 FsRtlAreNamesEqual(&DirContext->ShortNameU, FileToFindU, TRUE, NULL);
300 }
301
302 if (Found)
303 {
304 if (WildCard)
305 {
306 RtlCopyUnicodeString(&PathNameU, &Parent->PathNameU);
307 if (!vfatFCBIsRoot(Parent))
308 {
309 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = L'\\';
310 PathNameU.Length += sizeof(WCHAR);
311 }
312 RtlAppendUnicodeStringToString(&PathNameU, &DirContext->LongNameU);
313 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = 0;
314 rcFcb = vfatGrabFCBFromTable(DeviceExt, &PathNameU);
315 if (rcFcb != NULL)
316 {
317 RtlCopyMemory(&DirContext->DirEntry, &rcFcb->entry, sizeof(DIR_ENTRY));
318 vfatReleaseFCB(DeviceExt, rcFcb);
319 }
320 }
321 DPRINT("%u\n", DirContext->LongNameU.Length);
322 DPRINT("FindFile: new Name %wZ, DirIndex %u\n",
323 &DirContext->LongNameU, DirContext->DirIndex);
324
325 if (Context)
326 {
327 CcUnpinData(Context);
328 }
329 RtlFreeUnicodeString(&FileToFindUpcase);
330 ExFreePool(PathNameBuffer);
331 return STATUS_SUCCESS;
332 }
333 DirContext->DirIndex++;
334 }
335
336 if (Context)
337 {
338 CcUnpinData(Context);
339 }
340
341 RtlFreeUnicodeString(&FileToFindUpcase);
342 ExFreePool(PathNameBuffer);
343 return Status;
344 }
345
346 /*
347 * FUNCTION: Opens a file
348 */
349 static
350 NTSTATUS
351 VfatOpenFile(
352 PDEVICE_EXTENSION DeviceExt,
353 PUNICODE_STRING PathNameU,
354 PFILE_OBJECT FileObject,
355 ULONG RequestedDisposition,
356 PVFATFCB *ParentFcb)
357 {
358 PVFATFCB Fcb;
359 NTSTATUS Status;
360
361 DPRINT("VfatOpenFile(%p, '%wZ', %p, %p)\n", DeviceExt, PathNameU, FileObject, ParentFcb);
362
363 if (FileObject->RelatedFileObject)
364 {
365 DPRINT("'%wZ'\n", &FileObject->RelatedFileObject->FileName);
366
367 *ParentFcb = FileObject->RelatedFileObject->FsContext;
368 vfatGrabFCB(DeviceExt, *ParentFcb);
369 }
370 else
371 {
372 *ParentFcb = NULL;
373 }
374
375 if (!DeviceExt->FatInfo.FixedMedia)
376 {
377 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
378 IOCTL_DISK_CHECK_VERIFY,
379 NULL,
380 0,
381 NULL,
382 0,
383 FALSE);
384 if (!NT_SUCCESS(Status))
385 {
386 DPRINT("Status %lx\n", Status);
387 *ParentFcb = NULL;
388 return Status;
389 }
390 }
391
392 if (*ParentFcb)
393 {
394 vfatGrabFCB(DeviceExt, *ParentFcb);
395 }
396
397 /* try first to find an existing FCB in memory */
398 DPRINT("Checking for existing FCB in memory\n");
399
400 Status = vfatGetFCBForFile(DeviceExt, ParentFcb, &Fcb, PathNameU);
401 if (!NT_SUCCESS(Status))
402 {
403 DPRINT ("Could not make a new FCB, status: %x\n", Status);
404 return Status;
405 }
406
407 if (Fcb->Flags & FCB_DELETE_PENDING)
408 {
409 vfatReleaseFCB(DeviceExt, Fcb);
410 return STATUS_DELETE_PENDING;
411 }
412
413 /* Fail, if we try to overwrite a read-only file */
414 if ((*Fcb->Attributes & FILE_ATTRIBUTE_READONLY) &&
415 (RequestedDisposition == FILE_OVERWRITE))
416 {
417 vfatReleaseFCB(DeviceExt, Fcb);
418 return STATUS_ACCESS_DENIED;
419 }
420
421 DPRINT("Attaching FCB to fileObject\n");
422 Status = vfatAttachFCBToFileObject(DeviceExt, Fcb, FileObject);
423 if (!NT_SUCCESS(Status))
424 {
425 vfatReleaseFCB(DeviceExt, Fcb);
426 }
427 return Status;
428 }
429
430 /*
431 * FUNCTION: Create or open a file
432 */
433 static NTSTATUS
434 VfatCreateFile(
435 PDEVICE_OBJECT DeviceObject,
436 PIRP Irp)
437 {
438 PIO_STACK_LOCATION Stack;
439 PFILE_OBJECT FileObject;
440 NTSTATUS Status = STATUS_SUCCESS;
441 PDEVICE_EXTENSION DeviceExt;
442 ULONG RequestedDisposition, RequestedOptions;
443 PVFATFCB pFcb = NULL;
444 PVFATFCB ParentFcb = NULL;
445 PWCHAR c, last;
446 BOOLEAN PagingFileCreate = FALSE;
447 BOOLEAN Dots;
448 BOOLEAN OpenTargetDir = FALSE;
449 UNICODE_STRING FileNameU;
450 UNICODE_STRING PathNameU;
451 ULONG Attributes;
452
453 /* Unpack the various parameters. */
454 Stack = IoGetCurrentIrpStackLocation(Irp);
455 RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
456 RequestedOptions = Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
457 PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
458 OpenTargetDir = (Stack->Flags & SL_OPEN_TARGET_DIRECTORY) ? TRUE : FALSE;
459
460 FileObject = Stack->FileObject;
461 DeviceExt = DeviceObject->DeviceExtension;
462
463 /* Check their validity. */
464 if (RequestedOptions & FILE_DIRECTORY_FILE &&
465 RequestedDisposition == FILE_SUPERSEDE)
466 {
467 return STATUS_INVALID_PARAMETER;
468 }
469
470 if (RequestedOptions & FILE_DIRECTORY_FILE &&
471 RequestedOptions & FILE_NON_DIRECTORY_FILE)
472 {
473 return STATUS_INVALID_PARAMETER;
474 }
475
476 /* This a open operation for the volume itself */
477 if (FileObject->FileName.Length == 0 &&
478 (FileObject->RelatedFileObject == NULL || FileObject->RelatedFileObject->FsContext2 != NULL))
479 {
480 if (RequestedDisposition != FILE_OPEN &&
481 RequestedDisposition != FILE_OPEN_IF)
482 {
483 return STATUS_ACCESS_DENIED;
484 }
485 #if 0
486 /* In spite of what is shown in WDK, it seems that Windows FAT driver doesn't perform that test */
487 if (RequestedOptions & FILE_DIRECTORY_FILE)
488 {
489 return STATUS_NOT_A_DIRECTORY;
490 }
491 #endif
492
493 if (OpenTargetDir)
494 {
495 return STATUS_INVALID_PARAMETER;
496 }
497
498 pFcb = DeviceExt->VolumeFcb;
499 vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
500 vfatGrabFCB(DeviceExt, pFcb);
501
502 Irp->IoStatus.Information = FILE_OPENED;
503 return STATUS_SUCCESS;
504 }
505
506 /* Check for illegal characters and illegale dot sequences in the file name */
507 PathNameU = FileObject->FileName;
508 c = PathNameU.Buffer + PathNameU.Length / sizeof(WCHAR);
509 last = c - 1;
510 Dots = TRUE;
511 while (c-- > PathNameU.Buffer)
512 {
513 if (*c == L'\\' || c == PathNameU.Buffer)
514 {
515 if (Dots && last > c)
516 {
517 return STATUS_OBJECT_NAME_INVALID;
518 }
519 last = c - 1;
520 Dots = TRUE;
521 }
522 else if (*c != L'.')
523 {
524 Dots = FALSE;
525 }
526
527 if (*c != '\\' && vfatIsLongIllegal(*c))
528 {
529 return STATUS_OBJECT_NAME_INVALID;
530 }
531 }
532
533 /* Check if we try to open target directory of root dir */
534 if (OpenTargetDir && FileObject->RelatedFileObject == NULL && PathNameU.Length == sizeof(WCHAR) &&
535 PathNameU.Buffer[0] == L'\\')
536 {
537 return STATUS_INVALID_PARAMETER;
538 }
539
540 if (FileObject->RelatedFileObject && PathNameU.Length >= sizeof(WCHAR) && PathNameU.Buffer[0] == L'\\')
541 {
542 return STATUS_OBJECT_NAME_INVALID;
543 }
544
545 if (PathNameU.Length > sizeof(WCHAR) && PathNameU.Buffer[PathNameU.Length/sizeof(WCHAR)-1] == L'\\')
546 {
547 PathNameU.Length -= sizeof(WCHAR);
548 }
549
550 /* Try opening the file. */
551 if (!OpenTargetDir)
552 {
553 Status = VfatOpenFile(DeviceExt, &PathNameU, FileObject, RequestedDisposition, &ParentFcb);
554 }
555 else
556 {
557 PVFATFCB TargetFcb;
558 LONG idx, FileNameLen;
559
560 ParentFcb = (FileObject->RelatedFileObject != NULL) ? FileObject->RelatedFileObject->FsContext : NULL;
561 Status = vfatGetFCBForFile(DeviceExt, &ParentFcb, &TargetFcb, &PathNameU);
562
563 if (Status == STATUS_SUCCESS)
564 {
565 vfatGrabFCB(DeviceExt, ParentFcb);
566 vfatReleaseFCB(DeviceExt, TargetFcb);
567 Irp->IoStatus.Information = FILE_EXISTS;
568 }
569 else
570 {
571 Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
572 }
573
574 idx = FileObject->FileName.Length / sizeof(WCHAR) - 1;
575
576 /* Skip tailing \ - if any */
577 if (PathNameU.Buffer[idx] == L'\\')
578 {
579 --idx;
580 PathNameU.Length -= sizeof(WCHAR);
581 }
582
583 /* Get file name */
584 while (idx >= 0 && PathNameU.Buffer[idx] != L'\\')
585 {
586 --idx;
587 }
588
589 if (idx > 0 || PathNameU.Buffer[0] == L'\\')
590 {
591 /* We don't want to include / in the name */
592 FileNameLen = PathNameU.Length - ((idx + 1) * sizeof(WCHAR));
593
594 /* Update FO just to keep file name */
595 /* Skip first slash */
596 ++idx;
597 FileObject->FileName.Length = FileNameLen;
598 RtlMoveMemory(&PathNameU.Buffer[0], &PathNameU.Buffer[idx], FileObject->FileName.Length);
599 }
600 else
601 {
602 /* This is a relative open and we have only the filename, so open the parent directory
603 * It is in RelatedFileObject
604 */
605 ASSERT(FileObject->RelatedFileObject != NULL);
606
607 /* No need to modify the FO, it already has the name */
608 }
609
610 /* We're done with opening! */
611 if (ParentFcb != NULL)
612 {
613 Status = vfatAttachFCBToFileObject(DeviceExt, ParentFcb, FileObject);
614 }
615
616 if (NT_SUCCESS(Status))
617 {
618 pFcb = FileObject->FsContext;
619
620 if (pFcb->OpenHandleCount == 0)
621 {
622 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
623 Stack->Parameters.Create.ShareAccess,
624 FileObject,
625 &pFcb->FCBShareAccess);
626 }
627 else
628 {
629 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
630 Stack->Parameters.Create.ShareAccess,
631 FileObject,
632 &pFcb->FCBShareAccess,
633 FALSE);
634 if (!NT_SUCCESS(Status))
635 {
636 VfatCloseFile(DeviceExt, FileObject);
637 return Status;
638 }
639 }
640
641 pFcb->OpenHandleCount++;
642 }
643 else if (ParentFcb != NULL)
644 {
645 vfatReleaseFCB(DeviceExt, ParentFcb);
646 }
647
648 return Status;
649 }
650
651 /*
652 * If the directory containing the file to open doesn't exist then
653 * fail immediately
654 */
655 if (Status == STATUS_OBJECT_PATH_NOT_FOUND ||
656 Status == STATUS_INVALID_PARAMETER ||
657 Status == STATUS_DELETE_PENDING)
658 {
659 if (ParentFcb)
660 {
661 vfatReleaseFCB(DeviceExt, ParentFcb);
662 }
663 return Status;
664 }
665
666 if (!NT_SUCCESS(Status) && ParentFcb == NULL)
667 {
668 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU, Status);
669 return Status;
670 }
671
672 /* If the file open failed then create the required file */
673 if (!NT_SUCCESS (Status))
674 {
675 if (RequestedDisposition == FILE_CREATE ||
676 RequestedDisposition == FILE_OPEN_IF ||
677 RequestedDisposition == FILE_OVERWRITE_IF ||
678 RequestedDisposition == FILE_SUPERSEDE)
679 {
680 Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
681 if (!(RequestedOptions & FILE_DIRECTORY_FILE))
682 Attributes |= FILE_ATTRIBUTE_ARCHIVE;
683 vfatSplitPathName(&PathNameU, NULL, &FileNameU);
684 Status = VfatAddEntry(DeviceExt, &FileNameU, &pFcb, ParentFcb, RequestedOptions,
685 (UCHAR)(Attributes & FILE_ATTRIBUTE_VALID_FLAGS), NULL);
686 vfatReleaseFCB(DeviceExt, ParentFcb);
687 if (NT_SUCCESS(Status))
688 {
689 Status = vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
690 if (!NT_SUCCESS(Status))
691 {
692 vfatReleaseFCB(DeviceExt, pFcb);
693 return Status;
694 }
695
696 Irp->IoStatus.Information = FILE_CREATED;
697 VfatSetAllocationSizeInformation(FileObject,
698 pFcb,
699 DeviceExt,
700 &Irp->Overlay.AllocationSize);
701 VfatSetExtendedAttributes(FileObject,
702 Irp->AssociatedIrp.SystemBuffer,
703 Stack->Parameters.Create.EaLength);
704
705 if (PagingFileCreate)
706 {
707 pFcb->Flags |= FCB_IS_PAGE_FILE;
708 }
709 }
710 else
711 {
712 return Status;
713 }
714 }
715 else
716 {
717 if (ParentFcb)
718 {
719 vfatReleaseFCB(DeviceExt, ParentFcb);
720 }
721 return Status;
722 }
723 }
724 else
725 {
726 if (ParentFcb)
727 {
728 vfatReleaseFCB(DeviceExt, ParentFcb);
729 }
730
731 /* Otherwise fail if the caller wanted to create a new file */
732 if (RequestedDisposition == FILE_CREATE)
733 {
734 Irp->IoStatus.Information = FILE_EXISTS;
735 VfatCloseFile(DeviceExt, FileObject);
736 return STATUS_OBJECT_NAME_COLLISION;
737 }
738
739 pFcb = FileObject->FsContext;
740
741 if (pFcb->OpenHandleCount != 0)
742 {
743 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
744 Stack->Parameters.Create.ShareAccess,
745 FileObject,
746 &pFcb->FCBShareAccess,
747 FALSE);
748 if (!NT_SUCCESS(Status))
749 {
750 VfatCloseFile(DeviceExt, FileObject);
751 return Status;
752 }
753 }
754
755 /*
756 * Check the file has the requested attributes
757 */
758 if (RequestedOptions & FILE_NON_DIRECTORY_FILE &&
759 *pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY)
760 {
761 VfatCloseFile (DeviceExt, FileObject);
762 return STATUS_FILE_IS_A_DIRECTORY;
763 }
764 if (RequestedOptions & FILE_DIRECTORY_FILE &&
765 !(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
766 {
767 VfatCloseFile (DeviceExt, FileObject);
768 return STATUS_NOT_A_DIRECTORY;
769 }
770 #ifndef USE_ROS_CC_AND_FS
771 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
772 {
773 if (Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA ||
774 RequestedDisposition == FILE_OVERWRITE ||
775 RequestedDisposition == FILE_OVERWRITE_IF)
776 {
777 if (!MmFlushImageSection(&pFcb->SectionObjectPointers, MmFlushForWrite))
778 {
779 DPRINT1("%wZ\n", &pFcb->PathNameU);
780 DPRINT1("%d %d %d\n", Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA,
781 RequestedDisposition == FILE_OVERWRITE, RequestedDisposition == FILE_OVERWRITE_IF);
782 VfatCloseFile (DeviceExt, FileObject);
783 return STATUS_SHARING_VIOLATION;
784 }
785 }
786 }
787 #endif
788 if (PagingFileCreate)
789 {
790 /* FIXME:
791 * Do more checking for page files. It is possible,
792 * that the file was opened and closed previously
793 * as a normal cached file. In this case, the cache
794 * manager has referenced the fileobject and the fcb
795 * is held in memory. Try to remove the fileobject
796 * from cache manager and use the fcb.
797 */
798 if (pFcb->RefCount > 1)
799 {
800 if(!(pFcb->Flags & FCB_IS_PAGE_FILE))
801 {
802 VfatCloseFile(DeviceExt, FileObject);
803 return STATUS_INVALID_PARAMETER;
804 }
805 }
806 else
807 {
808 pFcb->Flags |= FCB_IS_PAGE_FILE;
809 }
810 }
811 else
812 {
813 if (pFcb->Flags & FCB_IS_PAGE_FILE)
814 {
815 VfatCloseFile(DeviceExt, FileObject);
816 return STATUS_INVALID_PARAMETER;
817 }
818 }
819
820 if (RequestedDisposition == FILE_OVERWRITE ||
821 RequestedDisposition == FILE_OVERWRITE_IF ||
822 RequestedDisposition == FILE_SUPERSEDE)
823 {
824 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
825 {
826 *pFcb->Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
827 *pFcb->Attributes |= FILE_ATTRIBUTE_ARCHIVE;
828 VfatUpdateEntry(pFcb);
829 }
830
831 ExAcquireResourceExclusiveLite(&(pFcb->MainResource), TRUE);
832 Status = VfatSetAllocationSizeInformation(FileObject,
833 pFcb,
834 DeviceExt,
835 &Irp->Overlay.AllocationSize);
836 ExReleaseResourceLite(&(pFcb->MainResource));
837 if (!NT_SUCCESS (Status))
838 {
839 VfatCloseFile(DeviceExt, FileObject);
840 return Status;
841 }
842 }
843
844 if (RequestedDisposition == FILE_SUPERSEDE)
845 {
846 Irp->IoStatus.Information = FILE_SUPERSEDED;
847 }
848 else if (RequestedDisposition == FILE_OVERWRITE ||
849 RequestedDisposition == FILE_OVERWRITE_IF)
850 {
851 Irp->IoStatus.Information = FILE_OVERWRITTEN;
852 }
853 else
854 {
855 Irp->IoStatus.Information = FILE_OPENED;
856 }
857 }
858
859 if (pFcb->OpenHandleCount == 0)
860 {
861 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
862 Stack->Parameters.Create.ShareAccess,
863 FileObject,
864 &pFcb->FCBShareAccess);
865 }
866 else
867 {
868 IoUpdateShareAccess(FileObject,
869 &pFcb->FCBShareAccess);
870 }
871
872 if (Irp->IoStatus.Information == FILE_CREATED)
873 {
874 FsRtlNotifyFullReportChange(DeviceExt->NotifySync,
875 &(DeviceExt->NotifyList),
876 (PSTRING)&pFcb->PathNameU,
877 pFcb->PathNameU.Length - pFcb->LongNameU.Length,
878 NULL,
879 NULL,
880 ((*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY) ?
881 FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME),
882 FILE_ACTION_ADDED,
883 NULL);
884 }
885
886 pFcb->OpenHandleCount++;
887
888 /* FIXME : test write access if requested */
889
890 return Status;
891 }
892
893 /*
894 * FUNCTION: Create or open a file
895 */
896 NTSTATUS
897 VfatCreate(
898 PVFAT_IRP_CONTEXT IrpContext)
899 {
900 NTSTATUS Status;
901
902 ASSERT(IrpContext);
903
904 if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
905 {
906 /* DeviceObject represents FileSystem instead of logical volume */
907 DPRINT ("FsdCreate called with file system\n");
908 IrpContext->Irp->IoStatus.Information = FILE_OPENED;
909 IrpContext->Irp->IoStatus.Status = STATUS_SUCCESS;
910 IoCompleteRequest(IrpContext->Irp, IO_DISK_INCREMENT);
911 VfatFreeIrpContext(IrpContext);
912 return STATUS_SUCCESS;
913 }
914
915 if (!(IrpContext->Flags & IRPCONTEXT_CANWAIT))
916 {
917 return(VfatQueueRequest(IrpContext));
918 }
919
920 IrpContext->Irp->IoStatus.Information = 0;
921 ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
922 Status = VfatCreateFile(IrpContext->DeviceObject, IrpContext->Irp);
923 ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
924
925 IrpContext->Irp->IoStatus.Status = Status;
926 IoCompleteRequest(IrpContext->Irp,
927 (CCHAR)(NT_SUCCESS(Status) ? IO_DISK_INCREMENT : IO_NO_INCREMENT));
928 VfatFreeIrpContext(IrpContext);
929 return Status;
930 }
931
932 /* EOF */