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