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 if (Fcb
->Flags
& FCB_IS_VOLUME
)
51 /* This is likely to be an opening by ID, return ourselves */
52 if (pRelativeFileName
[0] == L
'\\')
54 *pAbsoluteFilename
= NULL
;
55 return STATUS_SUCCESS
;
58 return STATUS_INVALID_PARAMETER
;
61 /* verify related object is a directory and target name
62 don't start with \. */
63 if (NtfsFCBIsDirectory(Fcb
) == FALSE
||
64 pRelativeFileName
[0] == L
'\\')
66 return STATUS_INVALID_PARAMETER
;
69 /* construct absolute path name */
70 ASSERT(wcslen (Fcb
->PathName
) + 1 + wcslen (pRelativeFileName
) + 1 <= MAX_PATH
);
71 rcName
= ExAllocatePoolWithTag(NonPagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_NTFS
);
74 return STATUS_INSUFFICIENT_RESOURCES
;
77 wcscpy(rcName
, Fcb
->PathName
);
78 if (!NtfsFCBIsRoot(Fcb
))
79 wcscat (rcName
, L
"\\");
80 wcscat (rcName
, pRelativeFileName
);
81 *pAbsoluteFilename
= rcName
;
83 return STATUS_SUCCESS
;
89 NtfsMoonWalkID(PDEVICE_EXTENSION DeviceExt
,
91 PUNICODE_STRING OutPath
)
94 PFILE_RECORD_HEADER MftRecord
;
95 PFILENAME_ATTRIBUTE FileName
;
96 WCHAR FullPath
[MAX_PATH
];
97 ULONG WritePosition
= MAX_PATH
- 1;
99 DPRINT1("NtfsMoonWalkID(%p, %I64x, %p)\n", DeviceExt
, Id
, OutPath
);
101 Id
= Id
& NTFS_MFT_MASK
;
103 RtlZeroMemory(FullPath
, sizeof(FullPath
));
104 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
105 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
107 if (MftRecord
== NULL
)
109 return STATUS_INSUFFICIENT_RESOURCES
;
114 Status
= ReadFileRecord(DeviceExt
, Id
, MftRecord
);
115 if (!NT_SUCCESS(Status
))
118 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
119 if (!(MftRecord
->Flags
& FRH_IN_USE
))
121 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
125 FileName
= GetBestFileNameFromRecord(MftRecord
);
126 WritePosition
-= FileName
->NameLength
;
127 ASSERT(WritePosition
< MAX_PATH
);
128 RtlCopyMemory(FullPath
+ WritePosition
, FileName
->Name
, FileName
->NameLength
* sizeof(WCHAR
));
130 ASSERT(WritePosition
< MAX_PATH
);
131 FullPath
[WritePosition
] = L
'\\';
133 Id
= FileName
->DirectoryFileReferenceNumber
& NTFS_MFT_MASK
;
134 if (Id
== NTFS_FILE_ROOT
)
138 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
140 if (!NT_SUCCESS(Status
))
143 OutPath
->Length
= (MAX_PATH
- WritePosition
- 1) * sizeof(WCHAR
);
144 OutPath
->MaximumLength
= (MAX_PATH
- WritePosition
) * sizeof(WCHAR
);
145 OutPath
->Buffer
= ExAllocatePoolWithTag(NonPagedPool
, OutPath
->MaximumLength
, TAG_NTFS
);
146 if (OutPath
->Buffer
== NULL
)
148 return STATUS_INSUFFICIENT_RESOURCES
;
150 RtlCopyMemory(OutPath
->Buffer
, FullPath
+ WritePosition
, OutPath
->MaximumLength
);
156 * FUNCTION: Opens a file
160 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt
,
161 PFILE_OBJECT FileObject
,
163 PNTFS_FCB
* FoundFCB
)
168 PWSTR AbsFileName
= NULL
;
170 DPRINT1("NtfsOpenFile(%p, %p, %S, %p)\n", DeviceExt
, FileObject
, FileName
, FoundFCB
);
174 if (FileObject
->RelatedFileObject
)
176 DPRINT("Converting relative filename to absolute filename\n");
178 Status
= NtfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
181 if (AbsFileName
) FileName
= AbsFileName
;
182 if (!NT_SUCCESS(Status
))
188 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
190 DPRINT("PathName to open: %S\n", FileName
);
192 /* try first to find an existing FCB in memory */
193 DPRINT("Checking for existing FCB in memory\n");
194 Fcb
= NtfsGrabFCBFromTable(DeviceExt
,
198 DPRINT("No existing FCB found, making a new one if file exists.\n");
199 Status
= NtfsGetFCBForFile(DeviceExt
,
203 if (ParentFcb
!= NULL
)
205 NtfsReleaseFCB(DeviceExt
,
209 if (!NT_SUCCESS (Status
))
211 DPRINT("Could not make a new FCB, status: %x\n", Status
);
214 ExFreePool(AbsFileName
);
220 DPRINT("Attaching FCB to fileObject\n");
221 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
226 ExFreePool(AbsFileName
);
235 * FUNCTION: Opens a file
239 NtfsCreateFile(PDEVICE_OBJECT DeviceObject
,
242 PDEVICE_EXTENSION DeviceExt
;
243 PIO_STACK_LOCATION Stack
;
244 PFILE_OBJECT FileObject
;
245 ULONG RequestedDisposition
;
246 ULONG RequestedOptions
;
250 UNICODE_STRING FullPath
;
252 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject
, Irp
);
254 DeviceExt
= DeviceObject
->DeviceExtension
;
256 Stack
= IoGetCurrentIrpStackLocation (Irp
);
259 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
260 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
261 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
262 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
263 RequestedDisposition
== FILE_SUPERSEDE
)
265 return STATUS_INVALID_PARAMETER
;
268 FileObject
= Stack
->FileObject
;
270 if (RequestedDisposition
== FILE_CREATE
||
271 RequestedDisposition
== FILE_OVERWRITE_IF
||
272 RequestedDisposition
== FILE_SUPERSEDE
)
274 return STATUS_ACCESS_DENIED
;
277 if ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) == FILE_OPEN_BY_FILE_ID
)
279 if (FileObject
->FileName
.Length
!= sizeof(ULONGLONG
))
280 return STATUS_INVALID_PARAMETER
;
282 Status
= NtfsMoonWalkID(DeviceExt
, (*(PULONGLONG
)FileObject
->FileName
.Buffer
), &FullPath
);
283 if (!NT_SUCCESS(Status
))
288 DPRINT1("Open by ID: %I64x -> %wZ\n", (*(PULONGLONG
)FileObject
->FileName
.Buffer
) & NTFS_MFT_MASK
, &FullPath
);
291 /* This a open operation for the volume itself */
292 if (FileObject
->FileName
.Length
== 0 &&
293 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
!= NULL
))
295 if (RequestedDisposition
!= FILE_OPEN
&&
296 RequestedDisposition
!= FILE_OPEN_IF
)
298 return STATUS_ACCESS_DENIED
;
301 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
303 return STATUS_NOT_A_DIRECTORY
;
306 NtfsAttachFCBToFileObject(DeviceExt
, DeviceExt
->VolumeFcb
, FileObject
);
307 DeviceExt
->VolumeFcb
->RefCount
++;
309 Irp
->IoStatus
.Information
= FILE_OPENED
;
310 return STATUS_SUCCESS
;
313 Status
= NtfsOpenFile(DeviceExt
,
315 ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) ? FullPath
.Buffer
: FileObject
->FileName
.Buffer
),
318 if (RequestedOptions
& FILE_OPEN_BY_FILE_ID
)
320 ExFreePoolWithTag(FullPath
.Buffer
, TAG_NTFS
);
323 if (NT_SUCCESS(Status
))
325 if (RequestedDisposition
== FILE_CREATE
)
327 Irp
->IoStatus
.Information
= FILE_EXISTS
;
328 NtfsCloseFile(DeviceExt
, FileObject
);
329 return STATUS_OBJECT_NAME_COLLISION
;
332 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
333 NtfsFCBIsDirectory(Fcb
))
335 NtfsCloseFile(DeviceExt
, FileObject
);
336 return STATUS_FILE_IS_A_DIRECTORY
;
339 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
340 !NtfsFCBIsDirectory(Fcb
))
342 NtfsCloseFile(DeviceExt
, FileObject
);
343 return STATUS_NOT_A_DIRECTORY
;
347 * If it is a reparse point & FILE_OPEN_REPARSE_POINT, then allow opening it
349 * Otherwise, attempt to read reparse data and hand them to the Io manager
350 * with status reparse to force a reparse.
352 if (NtfsFCBIsReparsePoint(Fcb
) &&
353 ((RequestedOptions
& FILE_OPEN_REPARSE_POINT
) != FILE_OPEN_REPARSE_POINT
))
355 PREPARSE_DATA_BUFFER ReparseData
= NULL
;
357 Status
= NtfsReadFCBAttribute(DeviceExt
, Fcb
,
358 AttributeReparsePoint
, L
"", 0,
359 (PVOID
*)&Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
360 if (NT_SUCCESS(Status
))
362 ReparseData
= (PREPARSE_DATA_BUFFER
)Irp
->Tail
.Overlay
.AuxiliaryBuffer
;
363 if (ReparseData
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
365 Status
= STATUS_REPARSE
;
369 Status
= STATUS_NOT_IMPLEMENTED
;
370 ExFreePoolWithTag(ReparseData
, TAG_NTFS
);
374 Irp
->IoStatus
.Information
= ((Status
== STATUS_REPARSE
) ? ReparseData
->ReparseTag
: 0);
376 NtfsCloseFile(DeviceExt
, FileObject
);
380 /* HUGLY HACK: remain RO so far... */
381 if (RequestedDisposition
== FILE_OVERWRITE
||
382 RequestedDisposition
== FILE_OVERWRITE_IF
||
383 RequestedDisposition
== FILE_SUPERSEDE
)
385 DPRINT1("Denying write request on NTFS volume\n");
386 NtfsCloseFile(DeviceExt
, FileObject
);
387 return STATUS_ACCESS_DENIED
;
392 /* HUGLY HACK: remain RO so far... */
393 if (RequestedDisposition
== FILE_CREATE
||
394 RequestedDisposition
== FILE_OPEN_IF
||
395 RequestedDisposition
== FILE_OVERWRITE_IF
||
396 RequestedDisposition
== FILE_SUPERSEDE
)
398 DPRINT1("Denying write request on NTFS volume\n");
399 return STATUS_ACCESS_DENIED
;
404 * If the directory containing the file to open doesn't exist then
407 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
408 Irp
->IoStatus
.Status
= Status
;
416 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject
,
419 PDEVICE_EXTENSION DeviceExt
;
422 if (DeviceObject
== NtfsGlobalData
->DeviceObject
)
424 /* DeviceObject represents FileSystem instead of logical volume */
425 DPRINT("Opening file system\n");
426 Irp
->IoStatus
.Information
= FILE_OPENED
;
427 Status
= STATUS_SUCCESS
;
431 DeviceExt
= DeviceObject
->DeviceExtension
;
433 FsRtlEnterFileSystem();
434 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
436 Status
= NtfsCreateFile(DeviceObject
,
438 ExReleaseResourceLite(&DeviceExt
->DirResource
);
439 FsRtlExitFileSystem();
442 Irp
->IoStatus
.Status
= Status
;
443 IoCompleteRequest(Irp
,
444 NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);