Created bugs in wait and timer code
[reactos.git] / reactos / ntoskrnl / io / create.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/io/create.c
5 * PURPOSE: Handling file create/open apis
6 * PROGRAMMER: David Welch (welch@cwcom.net)
7 * UPDATE HISTORY:
8 * 24/05/98: Created
9 */
10
11 /* INCLUDES ***************************************************************/
12
13 #include <windows.h>
14 #include <ddk/ntddk.h>
15 #include <internal/ob.h>
16 #include <internal/io.h>
17 #include <internal/string.h>
18 #include <wstring.h>
19
20 #define NDEBUG
21 #include <internal/debug.h>
22
23 /* FUNCTIONS *************************************************************/
24
25 NTSTATUS STDCALL NtDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes)
26 {
27 return(ZwDeleteFile(ObjectAttributes));
28 }
29
30 NTSTATUS STDCALL ZwDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes)
31 {
32 UNIMPLEMENTED;
33 }
34
35 NTSTATUS NtCreateFile(PHANDLE FileHandle,
36 ACCESS_MASK DesiredAccess,
37 POBJECT_ATTRIBUTES ObjectAttributes,
38 PIO_STATUS_BLOCK IoStatusBlock,
39 PLARGE_INTEGER AllocateSize,
40 ULONG FileAttributes,
41 ULONG ShareAccess,
42 ULONG CreateDisposition,
43 ULONG CreateOptions,
44 PVOID EaBuffer,
45 ULONG EaLength)
46 {
47 return(ZwCreateFile(FileHandle,
48 DesiredAccess,
49 ObjectAttributes,
50 IoStatusBlock,
51 AllocateSize,
52 FileAttributes,
53 ShareAccess,
54 CreateDisposition,
55 CreateOptions,
56 EaBuffer,
57 EaLength));
58 }
59
60 NTSTATUS ZwCreateFile(PHANDLE FileHandle,
61 ACCESS_MASK DesiredAccess,
62 POBJECT_ATTRIBUTES ObjectAttributes,
63 PIO_STATUS_BLOCK IoStatusBlock,
64 PLARGE_INTEGER AllocateSize,
65 ULONG FileAttributes,
66 ULONG ShareAccess,
67 ULONG CreateDisposition,
68 ULONG CreateOptions,
69 PVOID EaBuffer,
70 ULONG EaLength)
71 /*
72 * FUNCTION: Either causes a new file or directory to be created, or it opens
73 * an existing file, device, directory or volume, giving the caller a handle
74 * for the file object. This handle can be used by subsequent calls to
75 * manipulate data within the file or the file object's state of attributes.
76 * ARGUMENTS:
77 * FileHandle (OUT) = Points to a variable which receives the file
78 * handle on return
79 * DesiredAccess = Desired access to the file
80 * ObjectAttributes = Structure describing the file
81 * IoStatusBlock (OUT) = Receives information about the operation on
82 * return
83 * AllocationSize = Initial size of the file in bytes
84 * FileAttributes = Attributes to create the file with
85 * ShareAccess = Type of shared access the caller would like to the file
86 * CreateDisposition = Specifies what to do, depending on whether the
87 * file already exists
88 * CreateOptions = Options for creating a new file
89 * EaBuffer = Undocumented
90 * EaLength = Undocumented
91 * RETURNS: Status
92 */
93 {
94 PVOID Object;
95 NTSTATUS Status;
96 PIRP Irp;
97 KEVENT Event;
98 PDEVICE_OBJECT DeviceObject;
99 PFILE_OBJECT FileObject;
100 PIO_STACK_LOCATION StackLoc;
101 PWSTR Remainder;
102
103 DPRINT("ZwCreateFile(FileHandle %x, DesiredAccess %x, "
104 "ObjectAttributes %x ObjectAttributes->ObjectName->Buffer %w)\n",
105 FileHandle,DesiredAccess,ObjectAttributes,
106 ObjectAttributes->ObjectName->Buffer);
107
108 assert_irql(PASSIVE_LEVEL);
109
110 *FileHandle=0;
111
112 FileObject = ObGenericCreateObject(FileHandle,DesiredAccess,NULL,IoFileType);
113 memset(FileObject,0,sizeof(FILE_OBJECT));
114
115 Status = ObOpenObjectByName(ObjectAttributes,&Object,&Remainder);
116
117 if (Status != STATUS_SUCCESS && Status != STATUS_FS_QUERY_REQUIRED)
118 {
119 DPRINT("%s() = Failed to find object\n",__FUNCTION__);
120 ObDereferenceObject(FileObject);
121 ZwClose(*FileHandle);
122 *FileHandle=0;
123 return(STATUS_UNSUCCESSFUL);
124 }
125
126
127 DeviceObject = (PDEVICE_OBJECT)Object;
128 DPRINT("DeviceObject %x\n",DeviceObject);
129 DeviceObject = IoGetAttachedDevice(DeviceObject);
130 DPRINT("DeviceObject %x\n",DeviceObject);
131
132 if (Status == STATUS_SUCCESS)
133 {
134 CHECKPOINT;
135 FileObject->Flags = FileObject->Flags | FO_DIRECT_DEVICE_OPEN;
136 FileObject->FileName.Buffer = ExAllocatePool(NonPagedPool,
137 ObjectAttributes->ObjectName->Length);
138 FileObject->FileName.Length = ObjectAttributes->ObjectName->Length;
139 RtlCopyUnicodeString(&(FileObject->FileName),
140 ObjectAttributes->ObjectName);
141 }
142 else
143 {
144 CHECKPOINT;
145 DPRINT("DeviceObject %x\n",DeviceObject);
146 DPRINT("FileHandle %x\n",FileHandle);
147 if (DeviceObject->DeviceType != FILE_DEVICE_FILE_SYSTEM &&
148 DeviceObject->DeviceType != FILE_DEVICE_DISK)
149 {
150 ObDereferenceObject(FileObject);
151 ZwClose(*FileHandle);
152 *FileHandle=0;
153 return(STATUS_UNSUCCESSFUL);
154 }
155 DPRINT("DeviceObject->Vpb %x\n",DeviceObject->Vpb);
156 if (!(DeviceObject->Vpb->Flags & VPB_MOUNTED))
157 {
158 CHECKPOINT;
159 Status = IoTryToMountStorageDevice(DeviceObject);
160 if (Status!=STATUS_SUCCESS)
161 {
162 ObDereferenceObject(FileObject);
163 ZwClose(*FileHandle);
164 *FileHandle=0;
165 return(Status);
166 }
167 DeviceObject = IoGetAttachedDevice(DeviceObject);
168 }
169 DPRINT("Remainder %x\n",Remainder);
170 DPRINT("Remainder %w\n",Remainder);
171 DPRINT("FileObject->FileName.Buffer %w\n",FileObject->FileName.Buffer);
172 RtlInitUnicodeString(&(FileObject->FileName),wstrdup(Remainder));
173 DPRINT("FileObject->FileName.Buffer %x %w\n",
174 FileObject->FileName.Buffer,FileObject->FileName.Buffer);
175 }
176 CHECKPOINT;
177 DPRINT("FileObject->FileName.Buffer %x\n",FileObject->FileName.Buffer);
178
179 if (CreateOptions & FILE_SYNCHRONOUS_IO_ALERT)
180 {
181 FileObject->Flags = FileObject->Flags | FO_ALERTABLE_IO;
182 FileObject->Flags = FileObject->Flags | FO_SYNCHRONOUS_IO;
183 }
184 if (CreateOptions & FILE_SYNCHRONOUS_IO_NONALERT)
185 {
186 FileObject->Flags = FileObject->Flags | FO_SYNCHRONOUS_IO;
187 }
188
189 FileObject->DeviceObject=DeviceObject;
190 FileObject->Vpb=DeviceObject->Vpb;
191
192 KeInitializeEvent(&Event,NotificationEvent,FALSE);
193
194 DPRINT("DevObj StackSize %d\n", DeviceObject->StackSize);
195 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
196 if (Irp==NULL)
197 {
198 ExFreePool(FileObject->FileName.Buffer);
199 ObDereferenceObject(FileObject);
200 ZwClose(*FileHandle);
201 *FileHandle=0;
202 return(STATUS_UNSUCCESSFUL);
203 }
204
205 StackLoc = IoGetNextIrpStackLocation(Irp);
206 StackLoc->MajorFunction = IRP_MJ_CREATE;
207 StackLoc->MinorFunction = 0;
208 StackLoc->Flags = 0;
209 StackLoc->Control = 0;
210 StackLoc->DeviceObject = DeviceObject;
211 StackLoc->FileObject=FileObject;
212 Status = IoCallDriver(DeviceObject,Irp);
213 if (Status==STATUS_PENDING)
214 {
215 KeWaitForSingleObject(&Event,Executive,KernelMode,FALSE,NULL);
216 Status = IoStatusBlock->Status;
217 }
218
219 if (Status!=STATUS_SUCCESS)
220 {
221 DPRINT("FileObject->FileName.Buffer %x\n",FileObject->FileName.Buffer);
222 ExFreePool(FileObject->FileName.Buffer);
223 ObDereferenceObject(FileObject);
224 ZwClose(*FileHandle);
225 *FileHandle=0;
226 }
227
228 DPRINT("*FileHandle %x\n",*FileHandle);
229
230 return(Status);
231
232 }
233
234 NTSTATUS NtOpenFile(PHANDLE FileHandle,
235 ACCESS_MASK DesiredAccess,
236 POBJECT_ATTRIBUTES ObjectAttributes,
237 PIO_STATUS_BLOCK IoStatusBlock,
238 ULONG ShareAccess,
239 ULONG OpenOptions)
240 {
241 return(ZwOpenFile(FileHandle,
242 DesiredAccess,
243 ObjectAttributes,
244 IoStatusBlock,
245 ShareAccess,
246 OpenOptions));
247 }
248
249 NTSTATUS ZwOpenFile(PHANDLE FileHandle,
250 ACCESS_MASK DesiredAccess,
251 POBJECT_ATTRIBUTES ObjectAttributes,
252 PIO_STATUS_BLOCK IoStatusBlock,
253 ULONG ShareAccess,
254 ULONG OpenOptions)
255 /*
256 * FUNCTION: Opens a file (simpler than ZwCreateFile)
257 * ARGUMENTS:
258 * FileHandle (OUT) = Variable that receives the file handle on return
259 * DesiredAccess = Access desired by the caller to the file
260 * ObjectAttributes = Structue describing the file to be opened
261 * IoStatusBlock (OUT) = Receives details about the result of the
262 * operation
263 * ShareAccess = Type of shared access the caller requires
264 * OpenOptions = Options for the file open
265 * RETURNS: Status
266 * NOTE: Undocumented
267 */
268 {
269 return(ZwCreateFile(FileHandle,
270 DesiredAccess,
271 ObjectAttributes,
272 IoStatusBlock,
273 NULL,
274 0,
275 ShareAccess,
276 FILE_OPEN,
277 OpenOptions,
278 NULL,
279 0));
280 }
281
282