a71c5072796bd6ec6a6a42dd8aab0902ab42467e
[reactos.git] / reactos / drivers / filesystems / ntfs / create.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002, 2014 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
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.
18 *
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)
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "ntfs.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* FUNCTIONS ****************************************************************/
35
36 static
37 NTSTATUS
38 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject,
39 PWSTR pRelativeFileName,
40 PWSTR *pAbsoluteFilename)
41 {
42 PWSTR rcName;
43 PNTFS_FCB Fcb;
44
45 DPRINT("try related for %S\n", pRelativeFileName);
46 Fcb = pFileObject->FsContext;
47 ASSERT(Fcb);
48
49 if (Fcb->Flags & FCB_IS_VOLUME)
50 {
51 /* This is likely to be an opening by ID, return ourselves */
52 if (pRelativeFileName[0] == L'\\')
53 {
54 *pAbsoluteFilename = NULL;
55 return STATUS_SUCCESS;
56 }
57
58 return STATUS_INVALID_PARAMETER;
59 }
60
61 /* verify related object is a directory and target name
62 don't start with \. */
63 if (NtfsFCBIsDirectory(Fcb) == FALSE ||
64 pRelativeFileName[0] == L'\\')
65 {
66 return STATUS_INVALID_PARAMETER;
67 }
68
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);
72 if (!rcName)
73 {
74 return STATUS_INSUFFICIENT_RESOURCES;
75 }
76
77 wcscpy(rcName, Fcb->PathName);
78 if (!NtfsFCBIsRoot(Fcb))
79 wcscat (rcName, L"\\");
80 wcscat (rcName, pRelativeFileName);
81 *pAbsoluteFilename = rcName;
82
83 return STATUS_SUCCESS;
84 }
85
86
87 static
88 NTSTATUS
89 NtfsMoonWalkID(PDEVICE_EXTENSION DeviceExt,
90 ULONGLONG Id,
91 PUNICODE_STRING OutPath)
92 {
93 NTSTATUS Status;
94 PFILE_RECORD_HEADER MftRecord;
95 PFILENAME_ATTRIBUTE FileName;
96 WCHAR FullPath[MAX_PATH];
97 ULONG WritePosition = MAX_PATH - 1;
98
99 DPRINT1("NtfsMoonWalkID(%p, %I64x, %p)\n", DeviceExt, Id, OutPath);
100
101 RtlZeroMemory(FullPath, sizeof(FullPath));
102 MftRecord = ExAllocatePoolWithTag(NonPagedPool,
103 DeviceExt->NtfsInfo.BytesPerFileRecord,
104 TAG_NTFS);
105 if (MftRecord == NULL)
106 {
107 return STATUS_INSUFFICIENT_RESOURCES;
108 }
109
110 while (TRUE)
111 {
112 Status = ReadFileRecord(DeviceExt, Id, MftRecord);
113 if (!NT_SUCCESS(Status))
114 break;
115
116 ASSERT(MftRecord->Ntfs.Type == NRH_FILE_TYPE);
117 if (!(MftRecord->Flags & FRH_IN_USE))
118 {
119 Status = STATUS_OBJECT_PATH_NOT_FOUND;
120 break;
121 }
122
123 FileName = GetBestFileNameFromRecord(MftRecord);
124 if (FileName == NULL)
125 {
126 DPRINT1("$FILE_NAME attribute not found for %I64x\n", Id);
127 Status = STATUS_OBJECT_PATH_NOT_FOUND;
128 break;
129 }
130
131 WritePosition -= FileName->NameLength;
132 ASSERT(WritePosition < MAX_PATH);
133 RtlCopyMemory(FullPath + WritePosition, FileName->Name, FileName->NameLength * sizeof(WCHAR));
134 WritePosition -= 1;
135 ASSERT(WritePosition < MAX_PATH);
136 FullPath[WritePosition] = L'\\';
137
138 Id = FileName->DirectoryFileReferenceNumber & NTFS_MFT_MASK;
139 if (Id == NTFS_FILE_ROOT)
140 break;
141 }
142
143 ExFreePoolWithTag(MftRecord, TAG_NTFS);
144
145 if (!NT_SUCCESS(Status))
146 return Status;
147
148 OutPath->Length = (MAX_PATH - WritePosition - 1) * sizeof(WCHAR);
149 OutPath->MaximumLength = (MAX_PATH - WritePosition) * sizeof(WCHAR);
150 OutPath->Buffer = ExAllocatePoolWithTag(NonPagedPool, OutPath->MaximumLength, TAG_NTFS);
151 if (OutPath->Buffer == NULL)
152 {
153 return STATUS_INSUFFICIENT_RESOURCES;
154 }
155 RtlCopyMemory(OutPath->Buffer, FullPath + WritePosition, OutPath->MaximumLength);
156
157 return Status;
158 }
159
160 /*
161 * FUNCTION: Opens a file
162 */
163 static
164 NTSTATUS
165 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt,
166 PFILE_OBJECT FileObject,
167 PWSTR FileName,
168 PNTFS_FCB * FoundFCB)
169 {
170 PNTFS_FCB ParentFcb;
171 PNTFS_FCB Fcb;
172 NTSTATUS Status;
173 PWSTR AbsFileName = NULL;
174
175 DPRINT1("NtfsOpenFile(%p, %p, %S, %p)\n", DeviceExt, FileObject, FileName, FoundFCB);
176
177 *FoundFCB = NULL;
178
179 if (FileObject->RelatedFileObject)
180 {
181 DPRINT("Converting relative filename to absolute filename\n");
182
183 Status = NtfsMakeAbsoluteFilename(FileObject->RelatedFileObject,
184 FileName,
185 &AbsFileName);
186 if (AbsFileName) FileName = AbsFileName;
187 if (!NT_SUCCESS(Status))
188 {
189 return Status;
190 }
191 }
192
193 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
194
195 DPRINT("PathName to open: %S\n", FileName);
196
197 /* try first to find an existing FCB in memory */
198 DPRINT("Checking for existing FCB in memory\n");
199 Fcb = NtfsGrabFCBFromTable(DeviceExt,
200 FileName);
201 if (Fcb == NULL)
202 {
203 DPRINT("No existing FCB found, making a new one if file exists.\n");
204 Status = NtfsGetFCBForFile(DeviceExt,
205 &ParentFcb,
206 &Fcb,
207 FileName);
208 if (ParentFcb != NULL)
209 {
210 NtfsReleaseFCB(DeviceExt,
211 ParentFcb);
212 }
213
214 if (!NT_SUCCESS (Status))
215 {
216 DPRINT("Could not make a new FCB, status: %x\n", Status);
217
218 if (AbsFileName)
219 ExFreePool(AbsFileName);
220
221 return Status;
222 }
223 }
224
225 DPRINT("Attaching FCB to fileObject\n");
226 Status = NtfsAttachFCBToFileObject(DeviceExt,
227 Fcb,
228 FileObject);
229
230 if (AbsFileName)
231 ExFreePool(AbsFileName);
232
233 *FoundFCB = Fcb;
234
235 return Status;
236 }
237
238
239 /*
240 * FUNCTION: Opens a file
241 */
242 static
243 NTSTATUS
244 NtfsCreateFile(PDEVICE_OBJECT DeviceObject,
245 PIRP Irp)
246 {
247 PDEVICE_EXTENSION DeviceExt;
248 PIO_STACK_LOCATION Stack;
249 PFILE_OBJECT FileObject;
250 ULONG RequestedDisposition;
251 ULONG RequestedOptions;
252 PNTFS_FCB Fcb;
253 // PWSTR FileName;
254 NTSTATUS Status;
255 UNICODE_STRING FullPath;
256
257 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject, Irp);
258
259 DeviceExt = DeviceObject->DeviceExtension;
260 ASSERT(DeviceExt);
261 Stack = IoGetCurrentIrpStackLocation (Irp);
262 ASSERT(Stack);
263
264 RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
265 RequestedOptions = Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
266 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
267 if (RequestedOptions & FILE_DIRECTORY_FILE &&
268 RequestedDisposition == FILE_SUPERSEDE)
269 {
270 return STATUS_INVALID_PARAMETER;
271 }
272
273 FileObject = Stack->FileObject;
274
275 if (RequestedDisposition == FILE_CREATE ||
276 RequestedDisposition == FILE_OVERWRITE_IF ||
277 RequestedDisposition == FILE_SUPERSEDE)
278 {
279 return STATUS_ACCESS_DENIED;
280 }
281
282 if ((RequestedOptions & FILE_OPEN_BY_FILE_ID) == FILE_OPEN_BY_FILE_ID)
283 {
284 ULONGLONG MFTId;
285
286 if (FileObject->FileName.Length != sizeof(ULONGLONG))
287 return STATUS_INVALID_PARAMETER;
288
289 MFTId = (*(PULONGLONG)FileObject->FileName.Buffer) & NTFS_MFT_MASK;
290 if (MFTId < 0xf)
291 {
292 UNIMPLEMENTED;
293 return STATUS_NOT_IMPLEMENTED;
294 }
295
296 Status = NtfsMoonWalkID(DeviceExt, MFTId, &FullPath);
297 if (!NT_SUCCESS(Status))
298 {
299 return Status;
300 }
301
302 DPRINT1("Open by ID: %I64x -> %wZ\n", (*(PULONGLONG)FileObject->FileName.Buffer) & NTFS_MFT_MASK, &FullPath);
303 }
304
305 /* This a open operation for the volume itself */
306 if (FileObject->FileName.Length == 0 &&
307 (FileObject->RelatedFileObject == NULL || FileObject->RelatedFileObject->FsContext2 != NULL))
308 {
309 if (RequestedDisposition != FILE_OPEN &&
310 RequestedDisposition != FILE_OPEN_IF)
311 {
312 return STATUS_ACCESS_DENIED;
313 }
314
315 if (RequestedOptions & FILE_DIRECTORY_FILE)
316 {
317 return STATUS_NOT_A_DIRECTORY;
318 }
319
320 NtfsAttachFCBToFileObject(DeviceExt, DeviceExt->VolumeFcb, FileObject);
321 DeviceExt->VolumeFcb->RefCount++;
322
323 Irp->IoStatus.Information = FILE_OPENED;
324 return STATUS_SUCCESS;
325 }
326
327 Status = NtfsOpenFile(DeviceExt,
328 FileObject,
329 ((RequestedOptions & FILE_OPEN_BY_FILE_ID) ? FullPath.Buffer : FileObject->FileName.Buffer),
330 &Fcb);
331
332 if (RequestedOptions & FILE_OPEN_BY_FILE_ID)
333 {
334 ExFreePoolWithTag(FullPath.Buffer, TAG_NTFS);
335 }
336
337 if (NT_SUCCESS(Status))
338 {
339 if (RequestedDisposition == FILE_CREATE)
340 {
341 Irp->IoStatus.Information = FILE_EXISTS;
342 NtfsCloseFile(DeviceExt, FileObject);
343 return STATUS_OBJECT_NAME_COLLISION;
344 }
345
346 if (RequestedOptions & FILE_NON_DIRECTORY_FILE &&
347 NtfsFCBIsDirectory(Fcb))
348 {
349 NtfsCloseFile(DeviceExt, FileObject);
350 return STATUS_FILE_IS_A_DIRECTORY;
351 }
352
353 if (RequestedOptions & FILE_DIRECTORY_FILE &&
354 !NtfsFCBIsDirectory(Fcb))
355 {
356 NtfsCloseFile(DeviceExt, FileObject);
357 return STATUS_NOT_A_DIRECTORY;
358 }
359
360 /*
361 * If it is a reparse point & FILE_OPEN_REPARSE_POINT, then allow opening it
362 * as a normal file.
363 * Otherwise, attempt to read reparse data and hand them to the Io manager
364 * with status reparse to force a reparse.
365 */
366 if (NtfsFCBIsReparsePoint(Fcb) &&
367 ((RequestedOptions & FILE_OPEN_REPARSE_POINT) != FILE_OPEN_REPARSE_POINT))
368 {
369 PREPARSE_DATA_BUFFER ReparseData = NULL;
370
371 Status = NtfsReadFCBAttribute(DeviceExt, Fcb,
372 AttributeReparsePoint, L"", 0,
373 (PVOID *)&Irp->Tail.Overlay.AuxiliaryBuffer);
374 if (NT_SUCCESS(Status))
375 {
376 ReparseData = (PREPARSE_DATA_BUFFER)Irp->Tail.Overlay.AuxiliaryBuffer;
377 if (ReparseData->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
378 {
379 Status = STATUS_REPARSE;
380 }
381 else
382 {
383 Status = STATUS_NOT_IMPLEMENTED;
384 ExFreePoolWithTag(ReparseData, TAG_NTFS);
385 }
386 }
387
388 Irp->IoStatus.Information = ((Status == STATUS_REPARSE) ? ReparseData->ReparseTag : 0);
389
390 NtfsCloseFile(DeviceExt, FileObject);
391 return Status;
392 }
393
394 /* HUGLY HACK: remain RO so far... */
395 if (RequestedDisposition == FILE_OVERWRITE ||
396 RequestedDisposition == FILE_OVERWRITE_IF ||
397 RequestedDisposition == FILE_SUPERSEDE)
398 {
399 DPRINT1("Denying write request on NTFS volume\n");
400 NtfsCloseFile(DeviceExt, FileObject);
401 return STATUS_ACCESS_DENIED;
402 }
403 }
404 else
405 {
406 /* HUGLY HACK: remain RO so far... */
407 if (RequestedDisposition == FILE_CREATE ||
408 RequestedDisposition == FILE_OPEN_IF ||
409 RequestedDisposition == FILE_OVERWRITE_IF ||
410 RequestedDisposition == FILE_SUPERSEDE)
411 {
412 DPRINT1("Denying write request on NTFS volume\n");
413 return STATUS_ACCESS_DENIED;
414 }
415 }
416
417 /*
418 * If the directory containing the file to open doesn't exist then
419 * fail immediately
420 */
421 Irp->IoStatus.Information = (NT_SUCCESS(Status)) ? FILE_OPENED : 0;
422
423 return Status;
424 }
425
426
427 NTSTATUS
428 NtfsCreate(PNTFS_IRP_CONTEXT IrpContext)
429 {
430 PDEVICE_EXTENSION DeviceExt;
431 NTSTATUS Status;
432 PDEVICE_OBJECT DeviceObject;
433
434 DeviceObject = IrpContext->DeviceObject;
435 if (DeviceObject == NtfsGlobalData->DeviceObject)
436 {
437 /* DeviceObject represents FileSystem instead of logical volume */
438 DPRINT("Opening file system\n");
439 IrpContext->Irp->IoStatus.Information = FILE_OPENED;
440 return STATUS_SUCCESS;
441 }
442
443 DeviceExt = DeviceObject->DeviceExtension;
444
445 if (!(IrpContext->Flags & IRPCONTEXT_CANWAIT))
446 {
447 return NtfsMarkIrpContextForQueue(IrpContext);
448 }
449
450 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource,
451 TRUE);
452 Status = NtfsCreateFile(DeviceObject,
453 IrpContext->Irp);
454 ExReleaseResourceLite(&DeviceExt->DirResource);
455
456 return Status;
457 }
458
459 /* EOF */