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
,
88 PWSTR AbsFileName
= NULL
;
90 DPRINT1("NtfsOpenFile(%p, %p, %S, %p)\n", DeviceExt
, FileObject
, FileName
, FoundFCB
);
94 if (FileObject
->RelatedFileObject
)
96 DPRINT("Converting relative filename to absolute filename\n");
98 Status
= NtfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
101 FileName
= AbsFileName
;
102 if (!NT_SUCCESS(Status
))
107 return STATUS_UNSUCCESSFUL
;
110 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
112 DPRINT("PathName to open: %S\n", FileName
);
114 /* try first to find an existing FCB in memory */
115 DPRINT("Checking for existing FCB in memory\n");
116 Fcb
= NtfsGrabFCBFromTable(DeviceExt
,
120 DPRINT("No existing FCB found, making a new one if file exists.\n");
121 Status
= NtfsGetFCBForFile(DeviceExt
,
125 if (ParentFcb
!= NULL
)
127 NtfsReleaseFCB(DeviceExt
,
131 if (!NT_SUCCESS (Status
))
133 DPRINT("Could not make a new FCB, status: %x\n", Status
);
136 ExFreePool(AbsFileName
);
142 DPRINT("Attaching FCB to fileObject\n");
143 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
148 ExFreePool(AbsFileName
);
157 * FUNCTION: Opens a file
161 NtfsCreateFile(PDEVICE_OBJECT DeviceObject
,
164 PDEVICE_EXTENSION DeviceExt
;
165 PIO_STACK_LOCATION Stack
;
166 PFILE_OBJECT FileObject
;
167 ULONG RequestedDisposition
;
168 ULONG RequestedOptions
;
173 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject
, Irp
);
175 DeviceExt
= DeviceObject
->DeviceExtension
;
177 Stack
= IoGetCurrentIrpStackLocation (Irp
);
180 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
181 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
182 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
183 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
184 RequestedDisposition
== FILE_SUPERSEDE
)
186 return STATUS_INVALID_PARAMETER
;
189 FileObject
= Stack
->FileObject
;
191 if (RequestedDisposition
== FILE_CREATE
||
192 RequestedDisposition
== FILE_OVERWRITE_IF
||
193 RequestedDisposition
== FILE_SUPERSEDE
)
195 return STATUS_ACCESS_DENIED
;
198 /* This a open operation for the volume itself */
199 if (FileObject
->FileName
.Length
== 0 &&
200 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
!= NULL
))
202 if (RequestedDisposition
!= FILE_OPEN
&&
203 RequestedDisposition
!= FILE_OPEN_IF
)
205 return STATUS_ACCESS_DENIED
;
208 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
210 return STATUS_NOT_A_DIRECTORY
;
213 NtfsAttachFCBToFileObject(DeviceExt
, DeviceExt
->VolumeFcb
, FileObject
);
214 DeviceExt
->VolumeFcb
->RefCount
++;
216 Irp
->IoStatus
.Information
= FILE_OPENED
;
217 return STATUS_SUCCESS
;
220 Status
= NtfsOpenFile(DeviceExt
,
222 FileObject
->FileName
.Buffer
,
225 if (NT_SUCCESS(Status
))
227 if (RequestedDisposition
== FILE_CREATE
)
229 Irp
->IoStatus
.Information
= FILE_EXISTS
;
230 NtfsCloseFile(DeviceExt
, FileObject
);
231 return STATUS_OBJECT_NAME_COLLISION
;
234 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
235 NtfsFCBIsDirectory(Fcb
))
237 NtfsCloseFile(DeviceExt
, FileObject
);
238 return STATUS_FILE_IS_A_DIRECTORY
;
241 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
242 !NtfsFCBIsDirectory(Fcb
))
244 NtfsCloseFile(DeviceExt
, FileObject
);
245 return STATUS_NOT_A_DIRECTORY
;
248 /* HUGLY HACK: remain RO so far... */
249 if (RequestedDisposition
== FILE_OVERWRITE
||
250 RequestedDisposition
== FILE_OVERWRITE_IF
||
251 RequestedDisposition
== FILE_SUPERSEDE
)
253 DPRINT1("Denying write request on NTFS volume\n");
254 NtfsCloseFile(DeviceExt
, FileObject
);
255 return STATUS_ACCESS_DENIED
;
260 /* HUGLY HACK: remain RO so far... */
261 if (RequestedDisposition
== FILE_CREATE
||
262 RequestedDisposition
== FILE_OPEN_IF
||
263 RequestedDisposition
== FILE_OVERWRITE_IF
||
264 RequestedDisposition
== FILE_SUPERSEDE
)
266 DPRINT1("Denying write request on NTFS volume\n");
267 return STATUS_ACCESS_DENIED
;
272 * If the directory containing the file to open doesn't exist then
275 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
276 Irp
->IoStatus
.Status
= Status
;
284 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject
,
287 PDEVICE_EXTENSION DeviceExt
;
290 if (DeviceObject
== NtfsGlobalData
->DeviceObject
)
292 /* DeviceObject represents FileSystem instead of logical volume */
293 DPRINT("Opening file system\n");
294 Irp
->IoStatus
.Information
= FILE_OPENED
;
295 Status
= STATUS_SUCCESS
;
299 DeviceExt
= DeviceObject
->DeviceExtension
;
301 FsRtlEnterFileSystem();
302 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
304 Status
= NtfsCreateFile(DeviceObject
,
306 ExReleaseResourceLite(&DeviceExt
->DirResource
);
307 FsRtlExitFileSystem();
310 Irp
->IoStatus
.Status
= Status
;
311 IoCompleteRequest(Irp
,
312 NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);