* Sync up to trunk head (r65353).
[reactos.git] / 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 }
369 else
370 {
371 *ParentFcb = NULL;
372 }
373
374 if (!DeviceExt->FatInfo.FixedMedia)
375 {
376 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
377 IOCTL_DISK_CHECK_VERIFY,
378 NULL,
379 0,
380 NULL,
381 0,
382 FALSE);
383 if (!NT_SUCCESS(Status))
384 {
385 DPRINT("Status %lx\n", Status);
386 *ParentFcb = NULL;
387 return Status;
388 }
389 }
390
391 if (*ParentFcb)
392 {
393 vfatGrabFCB(DeviceExt, *ParentFcb);
394 }
395
396 /* try first to find an existing FCB in memory */
397 DPRINT("Checking for existing FCB in memory\n");
398
399 Status = vfatGetFCBForFile(DeviceExt, ParentFcb, &Fcb, PathNameU);
400 if (!NT_SUCCESS(Status))
401 {
402 DPRINT ("Could not make a new FCB, status: %x\n", Status);
403 return Status;
404 }
405
406 if (Fcb->Flags & FCB_DELETE_PENDING)
407 {
408 vfatReleaseFCB(DeviceExt, Fcb);
409 return STATUS_DELETE_PENDING;
410 }
411
412 /* Fail, if we try to overwrite a read-only file */
413 if ((*Fcb->Attributes & FILE_ATTRIBUTE_READONLY) &&
414 (RequestedDisposition == FILE_OVERWRITE))
415 {
416 vfatReleaseFCB(DeviceExt, Fcb);
417 return STATUS_ACCESS_DENIED;
418 }
419
420 DPRINT("Attaching FCB to fileObject\n");
421 Status = vfatAttachFCBToFileObject(DeviceExt, Fcb, FileObject);
422 if (!NT_SUCCESS(Status))
423 {
424 vfatReleaseFCB(DeviceExt, Fcb);
425 }
426 return Status;
427 }
428
429 /*
430 * FUNCTION: Create or open a file
431 */
432 static NTSTATUS
433 VfatCreateFile(
434 PDEVICE_OBJECT DeviceObject,
435 PIRP Irp)
436 {
437 PIO_STACK_LOCATION Stack;
438 PFILE_OBJECT FileObject;
439 NTSTATUS Status = STATUS_SUCCESS;
440 PDEVICE_EXTENSION DeviceExt;
441 ULONG RequestedDisposition, RequestedOptions;
442 PVFATFCB pFcb = NULL;
443 PVFATFCB ParentFcb = NULL;
444 PWCHAR c, last;
445 BOOLEAN PagingFileCreate;
446 BOOLEAN Dots;
447 BOOLEAN OpenTargetDir;
448 UNICODE_STRING FileNameU;
449 UNICODE_STRING PathNameU;
450 ULONG Attributes;
451
452 /* Unpack the various parameters. */
453 Stack = IoGetCurrentIrpStackLocation(Irp);
454 RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
455 RequestedOptions = Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
456 PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
457 OpenTargetDir = (Stack->Flags & SL_OPEN_TARGET_DIRECTORY) ? TRUE : FALSE;
458
459 FileObject = Stack->FileObject;
460 DeviceExt = DeviceObject->DeviceExtension;
461
462 /* Check their validity. */
463 if (RequestedOptions & FILE_DIRECTORY_FILE &&
464 RequestedDisposition == FILE_SUPERSEDE)
465 {
466 return STATUS_INVALID_PARAMETER;
467 }
468
469 if (RequestedOptions & FILE_DIRECTORY_FILE &&
470 RequestedOptions & FILE_NON_DIRECTORY_FILE)
471 {
472 return STATUS_INVALID_PARAMETER;
473 }
474
475 /* Deny create if the volume is locked */
476 if (DeviceExt->Flags & VCB_VOLUME_LOCKED)
477 {
478 return STATUS_ACCESS_DENIED;
479 }
480
481 /* This a open operation for the volume itself */
482 if (FileObject->FileName.Length == 0 &&
483 (FileObject->RelatedFileObject == NULL || FileObject->RelatedFileObject->FsContext2 != NULL))
484 {
485 DPRINT("Volume opening\n");
486
487 if (RequestedDisposition != FILE_OPEN &&
488 RequestedDisposition != FILE_OPEN_IF)
489 {
490 return STATUS_ACCESS_DENIED;
491 }
492 #if 0
493 /* In spite of what is shown in WDK, it seems that Windows FAT driver doesn't perform that test */
494 if (RequestedOptions & FILE_DIRECTORY_FILE)
495 {
496 return STATUS_NOT_A_DIRECTORY;
497 }
498 #endif
499
500 if (OpenTargetDir)
501 {
502 return STATUS_INVALID_PARAMETER;
503 }
504
505 pFcb = DeviceExt->VolumeFcb;
506 vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
507 DeviceExt->OpenHandleCount++;
508
509 Irp->IoStatus.Information = FILE_OPENED;
510 return STATUS_SUCCESS;
511 }
512
513 /* Check for illegal characters and illegale dot sequences in the file name */
514 PathNameU = FileObject->FileName;
515 c = PathNameU.Buffer + PathNameU.Length / sizeof(WCHAR);
516 last = c - 1;
517 Dots = TRUE;
518 while (c-- > PathNameU.Buffer)
519 {
520 if (*c == L'\\' || c == PathNameU.Buffer)
521 {
522 if (Dots && last > c)
523 {
524 return STATUS_OBJECT_NAME_INVALID;
525 }
526 last = c - 1;
527 Dots = TRUE;
528 }
529 else if (*c != L'.')
530 {
531 Dots = FALSE;
532 }
533
534 if (*c != '\\' && vfatIsLongIllegal(*c))
535 {
536 return STATUS_OBJECT_NAME_INVALID;
537 }
538 }
539
540 /* Check if we try to open target directory of root dir */
541 if (OpenTargetDir && FileObject->RelatedFileObject == NULL && PathNameU.Length == sizeof(WCHAR) &&
542 PathNameU.Buffer[0] == L'\\')
543 {
544 return STATUS_INVALID_PARAMETER;
545 }
546
547 if (FileObject->RelatedFileObject && PathNameU.Length >= sizeof(WCHAR) && PathNameU.Buffer[0] == L'\\')
548 {
549 return STATUS_OBJECT_NAME_INVALID;
550 }
551
552 if (PathNameU.Length > sizeof(WCHAR) && PathNameU.Buffer[PathNameU.Length/sizeof(WCHAR)-1] == L'\\')
553 {
554 PathNameU.Length -= sizeof(WCHAR);
555 }
556
557 /* Try opening the file. */
558 if (!OpenTargetDir)
559 {
560 Status = VfatOpenFile(DeviceExt, &PathNameU, FileObject, RequestedDisposition, &ParentFcb);
561 }
562 else
563 {
564 PVFATFCB TargetFcb;
565 LONG idx, FileNameLen;
566
567 ParentFcb = (FileObject->RelatedFileObject != NULL) ? FileObject->RelatedFileObject->FsContext : NULL;
568 if (ParentFcb)
569 {
570 vfatGrabFCB(DeviceExt, ParentFcb);
571 }
572 Status = vfatGetFCBForFile(DeviceExt, &ParentFcb, &TargetFcb, &PathNameU);
573
574 if (NT_SUCCESS(Status))
575 {
576 vfatReleaseFCB(DeviceExt, TargetFcb);
577 Irp->IoStatus.Information = FILE_EXISTS;
578 }
579 else
580 {
581 Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
582 }
583
584 idx = FileObject->FileName.Length / sizeof(WCHAR) - 1;
585
586 /* Skip trailing \ - if any */
587 if (PathNameU.Buffer[idx] == L'\\')
588 {
589 --idx;
590 PathNameU.Length -= sizeof(WCHAR);
591 }
592
593 /* Get file name */
594 while (idx >= 0 && PathNameU.Buffer[idx] != L'\\')
595 {
596 --idx;
597 }
598
599 if (idx > 0 || PathNameU.Buffer[0] == L'\\')
600 {
601 /* We don't want to include / in the name */
602 FileNameLen = PathNameU.Length - ((idx + 1) * sizeof(WCHAR));
603
604 /* Update FO just to keep file name */
605 /* Skip first slash */
606 ++idx;
607 FileObject->FileName.Length = FileNameLen;
608 RtlMoveMemory(&PathNameU.Buffer[0], &PathNameU.Buffer[idx], FileObject->FileName.Length);
609 }
610 else
611 {
612 /* This is a relative open and we have only the filename, so open the parent directory
613 * It is in RelatedFileObject
614 */
615 ASSERT(FileObject->RelatedFileObject != NULL);
616
617 /* No need to modify the FO, it already has the name */
618 }
619
620 /* We're done with opening! */
621 if (ParentFcb != NULL)
622 {
623 Status = vfatAttachFCBToFileObject(DeviceExt, ParentFcb, FileObject);
624 }
625
626 if (NT_SUCCESS(Status))
627 {
628 pFcb = FileObject->FsContext;
629 ASSERT(pFcb == ParentFcb);
630
631 if (pFcb->OpenHandleCount == 0)
632 {
633 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
634 Stack->Parameters.Create.ShareAccess,
635 FileObject,
636 &pFcb->FCBShareAccess);
637 }
638 else
639 {
640 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
641 Stack->Parameters.Create.ShareAccess,
642 FileObject,
643 &pFcb->FCBShareAccess,
644 FALSE);
645 if (!NT_SUCCESS(Status))
646 {
647 VfatCloseFile(DeviceExt, FileObject);
648 return Status;
649 }
650 }
651
652 pFcb->OpenHandleCount++;
653 DeviceExt->OpenHandleCount++;
654 }
655 else if (ParentFcb != NULL)
656 {
657 vfatReleaseFCB(DeviceExt, ParentFcb);
658 }
659
660 return Status;
661 }
662
663 /*
664 * If the directory containing the file to open doesn't exist then
665 * fail immediately
666 */
667 if (Status == STATUS_OBJECT_PATH_NOT_FOUND ||
668 Status == STATUS_INVALID_PARAMETER ||
669 Status == STATUS_DELETE_PENDING)
670 {
671 if (ParentFcb)
672 {
673 vfatReleaseFCB(DeviceExt, ParentFcb);
674 }
675 return Status;
676 }
677
678 if (!NT_SUCCESS(Status) && ParentFcb == NULL)
679 {
680 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU, Status);
681 return Status;
682 }
683
684 /* If the file open failed then create the required file */
685 if (!NT_SUCCESS (Status))
686 {
687 if (RequestedDisposition == FILE_CREATE ||
688 RequestedDisposition == FILE_OPEN_IF ||
689 RequestedDisposition == FILE_OVERWRITE_IF ||
690 RequestedDisposition == FILE_SUPERSEDE)
691 {
692 Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
693 if (!(RequestedOptions & FILE_DIRECTORY_FILE))
694 Attributes |= FILE_ATTRIBUTE_ARCHIVE;
695 vfatSplitPathName(&PathNameU, NULL, &FileNameU);
696 Status = VfatAddEntry(DeviceExt, &FileNameU, &pFcb, ParentFcb, RequestedOptions,
697 (UCHAR)(Attributes & FILE_ATTRIBUTE_VALID_FLAGS), NULL);
698 vfatReleaseFCB(DeviceExt, ParentFcb);
699 if (NT_SUCCESS(Status))
700 {
701 Status = vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
702 if (!NT_SUCCESS(Status))
703 {
704 vfatReleaseFCB(DeviceExt, pFcb);
705 return Status;
706 }
707
708 Irp->IoStatus.Information = FILE_CREATED;
709 VfatSetAllocationSizeInformation(FileObject,
710 pFcb,
711 DeviceExt,
712 &Irp->Overlay.AllocationSize);
713 VfatSetExtendedAttributes(FileObject,
714 Irp->AssociatedIrp.SystemBuffer,
715 Stack->Parameters.Create.EaLength);
716
717 if (PagingFileCreate)
718 {
719 pFcb->Flags |= FCB_IS_PAGE_FILE;
720 }
721 }
722 else
723 {
724 return Status;
725 }
726 }
727 else
728 {
729 if (ParentFcb)
730 {
731 vfatReleaseFCB(DeviceExt, ParentFcb);
732 }
733 return Status;
734 }
735 }
736 else
737 {
738 if (ParentFcb)
739 {
740 vfatReleaseFCB(DeviceExt, ParentFcb);
741 }
742
743 /* Otherwise fail if the caller wanted to create a new file */
744 if (RequestedDisposition == FILE_CREATE)
745 {
746 Irp->IoStatus.Information = FILE_EXISTS;
747 VfatCloseFile(DeviceExt, FileObject);
748 return STATUS_OBJECT_NAME_COLLISION;
749 }
750
751 pFcb = FileObject->FsContext;
752
753 if (pFcb->OpenHandleCount != 0)
754 {
755 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
756 Stack->Parameters.Create.ShareAccess,
757 FileObject,
758 &pFcb->FCBShareAccess,
759 FALSE);
760 if (!NT_SUCCESS(Status))
761 {
762 VfatCloseFile(DeviceExt, FileObject);
763 return Status;
764 }
765 }
766
767 /*
768 * Check the file has the requested attributes
769 */
770 if (RequestedOptions & FILE_NON_DIRECTORY_FILE &&
771 *pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY)
772 {
773 VfatCloseFile (DeviceExt, FileObject);
774 return STATUS_FILE_IS_A_DIRECTORY;
775 }
776 if (RequestedOptions & FILE_DIRECTORY_FILE &&
777 !(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
778 {
779 VfatCloseFile (DeviceExt, FileObject);
780 return STATUS_NOT_A_DIRECTORY;
781 }
782 #ifndef USE_ROS_CC_AND_FS
783 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
784 {
785 if (Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA ||
786 RequestedDisposition == FILE_OVERWRITE ||
787 RequestedDisposition == FILE_OVERWRITE_IF)
788 {
789 if (!MmFlushImageSection(&pFcb->SectionObjectPointers, MmFlushForWrite))
790 {
791 DPRINT1("%wZ\n", &pFcb->PathNameU);
792 DPRINT1("%d %d %d\n", Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA,
793 RequestedDisposition == FILE_OVERWRITE, RequestedDisposition == FILE_OVERWRITE_IF);
794 VfatCloseFile (DeviceExt, FileObject);
795 return STATUS_SHARING_VIOLATION;
796 }
797 }
798 }
799 #endif
800 if (PagingFileCreate)
801 {
802 /* FIXME:
803 * Do more checking for page files. It is possible,
804 * that the file was opened and closed previously
805 * as a normal cached file. In this case, the cache
806 * manager has referenced the fileobject and the fcb
807 * is held in memory. Try to remove the fileobject
808 * from cache manager and use the fcb.
809 */
810 if (pFcb->RefCount > 1)
811 {
812 if(!(pFcb->Flags & FCB_IS_PAGE_FILE))
813 {
814 VfatCloseFile(DeviceExt, FileObject);
815 return STATUS_INVALID_PARAMETER;
816 }
817 }
818 else
819 {
820 pFcb->Flags |= FCB_IS_PAGE_FILE;
821 }
822 }
823 else
824 {
825 if (pFcb->Flags & FCB_IS_PAGE_FILE)
826 {
827 VfatCloseFile(DeviceExt, FileObject);
828 return STATUS_INVALID_PARAMETER;
829 }
830 }
831
832 if (RequestedDisposition == FILE_OVERWRITE ||
833 RequestedDisposition == FILE_OVERWRITE_IF ||
834 RequestedDisposition == FILE_SUPERSEDE)
835 {
836 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
837 {
838 *pFcb->Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
839 *pFcb->Attributes |= FILE_ATTRIBUTE_ARCHIVE;
840 VfatUpdateEntry(pFcb);
841 }
842
843 ExAcquireResourceExclusiveLite(&(pFcb->MainResource), TRUE);
844 Status = VfatSetAllocationSizeInformation(FileObject,
845 pFcb,
846 DeviceExt,
847 &Irp->Overlay.AllocationSize);
848 ExReleaseResourceLite(&(pFcb->MainResource));
849 if (!NT_SUCCESS (Status))
850 {
851 VfatCloseFile(DeviceExt, FileObject);
852 return Status;
853 }
854 }
855
856 if (RequestedDisposition == FILE_SUPERSEDE)
857 {
858 Irp->IoStatus.Information = FILE_SUPERSEDED;
859 }
860 else if (RequestedDisposition == FILE_OVERWRITE ||
861 RequestedDisposition == FILE_OVERWRITE_IF)
862 {
863 Irp->IoStatus.Information = FILE_OVERWRITTEN;
864 }
865 else
866 {
867 Irp->IoStatus.Information = FILE_OPENED;
868 }
869 }
870
871 if (pFcb->OpenHandleCount == 0)
872 {
873 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
874 Stack->Parameters.Create.ShareAccess,
875 FileObject,
876 &pFcb->FCBShareAccess);
877 }
878 else
879 {
880 IoUpdateShareAccess(FileObject,
881 &pFcb->FCBShareAccess);
882 }
883
884 if (Irp->IoStatus.Information == FILE_CREATED)
885 {
886 FsRtlNotifyFullReportChange(DeviceExt->NotifySync,
887 &(DeviceExt->NotifyList),
888 (PSTRING)&pFcb->PathNameU,
889 pFcb->PathNameU.Length - pFcb->LongNameU.Length,
890 NULL,
891 NULL,
892 ((*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY) ?
893 FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME),
894 FILE_ACTION_ADDED,
895 NULL);
896 }
897
898 pFcb->OpenHandleCount++;
899 DeviceExt->OpenHandleCount++;
900
901 /* FIXME : test write access if requested */
902
903 return Status;
904 }
905
906 /*
907 * FUNCTION: Create or open a file
908 */
909 NTSTATUS
910 VfatCreate(
911 PVFAT_IRP_CONTEXT IrpContext)
912 {
913 NTSTATUS Status;
914
915 ASSERT(IrpContext);
916
917 if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
918 {
919 /* DeviceObject represents FileSystem instead of logical volume */
920 DPRINT ("FsdCreate called with file system\n");
921 IrpContext->Irp->IoStatus.Information = FILE_OPENED;
922 IrpContext->Irp->IoStatus.Status = STATUS_SUCCESS;
923 IoCompleteRequest(IrpContext->Irp, IO_DISK_INCREMENT);
924 VfatFreeIrpContext(IrpContext);
925 return STATUS_SUCCESS;
926 }
927
928 if (!(IrpContext->Flags & IRPCONTEXT_CANWAIT))
929 {
930 return(VfatQueueRequest(IrpContext));
931 }
932
933 IrpContext->Irp->IoStatus.Information = 0;
934 ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
935 Status = VfatCreateFile(IrpContext->DeviceObject, IrpContext->Irp);
936 ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
937
938 IrpContext->Irp->IoStatus.Status = Status;
939 IoCompleteRequest(IrpContext->Irp,
940 (CCHAR)(NT_SUCCESS(Status) ? IO_DISK_INCREMENT : IO_NO_INCREMENT));
941 VfatFreeIrpContext(IrpContext);
942 return Status;
943 }
944
945 /* EOF */