3 * Copyright (C) 2002, 2014 ReactOS Team
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.
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.
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.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/create.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
24 * Pierre Schweitzer (pierre@reactos.org)
27 /* INCLUDES *****************************************************************/
34 /* FUNCTIONS ****************************************************************/
38 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject
,
39 PWSTR pRelativeFileName
,
40 PWSTR
*pAbsoluteFilename
)
45 DPRINT("try related for %S\n", pRelativeFileName
);
46 Fcb
= pFileObject
->FsContext
;
49 /* verify related object is a directory and target name
50 don't start with \. */
51 if (NtfsFCBIsDirectory(Fcb
) == FALSE
||
52 pRelativeFileName
[0] == L
'\\')
54 return STATUS_INVALID_PARAMETER
;
57 /* construct absolute path name */
58 ASSERT(wcslen (Fcb
->PathName
) + 1 + wcslen (pRelativeFileName
) + 1 <= MAX_PATH
);
59 rcName
= ExAllocatePoolWithTag(NonPagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_NTFS
);
62 return STATUS_INSUFFICIENT_RESOURCES
;
65 wcscpy(rcName
, Fcb
->PathName
);
66 if (!NtfsFCBIsRoot(Fcb
))
67 wcscat (rcName
, L
"\\");
68 wcscat (rcName
, pRelativeFileName
);
69 *pAbsoluteFilename
= rcName
;
71 return STATUS_SUCCESS
;
76 * FUNCTION: Opens a file
80 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt
,
81 PFILE_OBJECT FileObject
,
89 PWSTR AbsFileName
= NULL
;
91 DPRINT1("NtfsOpenFile(%p, %p, %S, %u, %p)\n", DeviceExt
, FileObject
, (!OpenById
? FileName
: NULL
), OpenById
, FoundFCB
);
97 ULONGLONG Id
= (*(PULONGLONG
)FileName
) & NTFS_MFT_MASK
;
99 DPRINT1("Will attempt to open by id: %I64x\n", Id
);
101 Fcb
= NtfsGrabFCBFromTableById(DeviceExt
,
105 Status
= NtfsGetFCBForFileById(DeviceExt
,
108 if (!NT_SUCCESS (Status
))
110 DPRINT("Could not make a new FCB, status: %x\n", Status
);
115 Fcb
->Flags
|= FCB_IS_OPEN_BY_ID
;
120 if (FileObject
->RelatedFileObject
)
122 DPRINT("Converting relative filename to absolute filename\n");
124 Status
= NtfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
127 FileName
= AbsFileName
;
128 if (!NT_SUCCESS(Status
))
133 return STATUS_UNSUCCESSFUL
;
136 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
138 DPRINT("PathName to open: %S\n", FileName
);
140 /* try first to find an existing FCB in memory */
141 DPRINT("Checking for existing FCB in memory\n");
142 Fcb
= NtfsGrabFCBFromTable(DeviceExt
,
146 DPRINT("No existing FCB found, making a new one if file exists.\n");
147 Status
= NtfsGetFCBForFile(DeviceExt
,
151 if (ParentFcb
!= NULL
)
153 NtfsReleaseFCB(DeviceExt
,
157 if (!NT_SUCCESS (Status
))
159 DPRINT("Could not make a new FCB, status: %x\n", Status
);
162 ExFreePool(AbsFileName
);
169 DPRINT("Attaching FCB to fileObject\n");
170 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
175 ExFreePool(AbsFileName
);
184 * FUNCTION: Opens a file
188 NtfsCreateFile(PDEVICE_OBJECT DeviceObject
,
191 PDEVICE_EXTENSION DeviceExt
;
192 PIO_STACK_LOCATION Stack
;
193 PFILE_OBJECT FileObject
;
194 ULONG RequestedDisposition
;
195 ULONG RequestedOptions
;
200 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject
, Irp
);
202 DeviceExt
= DeviceObject
->DeviceExtension
;
204 Stack
= IoGetCurrentIrpStackLocation (Irp
);
207 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
208 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
209 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
210 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
211 RequestedDisposition
== FILE_SUPERSEDE
)
213 return STATUS_INVALID_PARAMETER
;
216 if (RequestedOptions
& FILE_OPEN_BY_FILE_ID
)
219 return STATUS_NOT_IMPLEMENTED
;
222 FileObject
= Stack
->FileObject
;
224 if (RequestedDisposition
== FILE_CREATE
||
225 RequestedDisposition
== FILE_OVERWRITE_IF
||
226 RequestedDisposition
== FILE_SUPERSEDE
)
228 return STATUS_ACCESS_DENIED
;
231 if ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) == FILE_OPEN_BY_FILE_ID
&&
232 FileObject
->FileName
.Length
!= sizeof(ULONGLONG
))
234 return STATUS_INVALID_PARAMETER
;
237 /* This a open operation for the volume itself */
238 if (FileObject
->FileName
.Length
== 0 &&
239 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
!= NULL
))
241 if (RequestedDisposition
!= FILE_OPEN
&&
242 RequestedDisposition
!= FILE_OPEN_IF
)
244 return STATUS_ACCESS_DENIED
;
247 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
249 return STATUS_NOT_A_DIRECTORY
;
252 NtfsAttachFCBToFileObject(DeviceExt
, DeviceExt
->VolumeFcb
, FileObject
);
253 DeviceExt
->VolumeFcb
->RefCount
++;
255 Irp
->IoStatus
.Information
= FILE_OPENED
;
256 return STATUS_SUCCESS
;
259 Status
= NtfsOpenFile(DeviceExt
,
261 FileObject
->FileName
.Buffer
,
262 ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) == FILE_OPEN_BY_FILE_ID
),
265 if (NT_SUCCESS(Status
))
267 if (RequestedDisposition
== FILE_CREATE
)
269 Irp
->IoStatus
.Information
= FILE_EXISTS
;
270 NtfsCloseFile(DeviceExt
, FileObject
);
271 return STATUS_OBJECT_NAME_COLLISION
;
274 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
275 NtfsFCBIsDirectory(Fcb
))
277 NtfsCloseFile(DeviceExt
, FileObject
);
278 return STATUS_FILE_IS_A_DIRECTORY
;
281 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
282 !NtfsFCBIsDirectory(Fcb
))
284 NtfsCloseFile(DeviceExt
, FileObject
);
285 return STATUS_NOT_A_DIRECTORY
;
289 * If it is a reparse point & FILE_OPEN_REPARSE_POINT, then allow opening it
291 * Otherwise, attempt to read reparse data and hand them to the Io manager
292 * with status reparse to force a reparse.
294 if (NtfsFCBIsReparsePoint(Fcb
) &&
295 ((RequestedOptions
& FILE_OPEN_REPARSE_POINT
) != FILE_OPEN_REPARSE_POINT
))
297 PREPARSE_DATA_BUFFER ReparseData
= NULL
;
299 Status
= NtfsReadFCBAttribute(DeviceExt
, Fcb
,
300 AttributeReparsePoint
, L
"", 0,
301 (PVOID
*)&Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
302 if (NT_SUCCESS(Status
))
304 ReparseData
= (PREPARSE_DATA_BUFFER
)Irp
->Tail
.Overlay
.AuxiliaryBuffer
;
305 if (ReparseData
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
307 Status
= STATUS_REPARSE
;
311 Status
= STATUS_NOT_IMPLEMENTED
;
312 ExFreePoolWithTag(ReparseData
, TAG_NTFS
);
316 Irp
->IoStatus
.Information
= ((Status
== STATUS_REPARSE
) ? ReparseData
->ReparseTag
: 0);
318 NtfsCloseFile(DeviceExt
, FileObject
);
322 /* HUGLY HACK: remain RO so far... */
323 if (RequestedDisposition
== FILE_OVERWRITE
||
324 RequestedDisposition
== FILE_OVERWRITE_IF
||
325 RequestedDisposition
== FILE_SUPERSEDE
)
327 DPRINT1("Denying write request on NTFS volume\n");
328 NtfsCloseFile(DeviceExt
, FileObject
);
329 return STATUS_ACCESS_DENIED
;
334 /* HUGLY HACK: remain RO so far... */
335 if (RequestedDisposition
== FILE_CREATE
||
336 RequestedDisposition
== FILE_OPEN_IF
||
337 RequestedDisposition
== FILE_OVERWRITE_IF
||
338 RequestedDisposition
== FILE_SUPERSEDE
)
340 DPRINT1("Denying write request on NTFS volume\n");
341 return STATUS_ACCESS_DENIED
;
346 * If the directory containing the file to open doesn't exist then
349 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
350 Irp
->IoStatus
.Status
= Status
;
358 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject
,
361 PDEVICE_EXTENSION DeviceExt
;
364 if (DeviceObject
== NtfsGlobalData
->DeviceObject
)
366 /* DeviceObject represents FileSystem instead of logical volume */
367 DPRINT("Opening file system\n");
368 Irp
->IoStatus
.Information
= FILE_OPENED
;
369 Status
= STATUS_SUCCESS
;
373 DeviceExt
= DeviceObject
->DeviceExtension
;
375 FsRtlEnterFileSystem();
376 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
378 Status
= NtfsCreateFile(DeviceObject
,
380 ExReleaseResourceLite(&DeviceExt
->DirResource
);
381 FsRtlExitFileSystem();
384 Irp
->IoStatus
.Status
= Status
;
385 IoCompleteRequest(Irp
,
386 NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);