[CDFS]
[reactos.git] / reactos / drivers / filesystems / cdfs / dispatch.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2008 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/filesystems/cdfs/dispatch.c
22 * PURPOSE: CDROM (ISO 9660) filesystem driver
23 * PROGRAMMER: Pierre Schweitzer
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #include "cdfs.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 static LONG QueueCount = 0;
34
35 /* FUNCTIONS ****************************************************************/
36
37 static WORKER_THREAD_ROUTINE CdfsDoRequest;
38
39 static
40 NTSTATUS
41 CdfsQueueRequest(PCDFS_IRP_CONTEXT IrpContext)
42 {
43 InterlockedIncrement(&QueueCount);
44 DPRINT("CdfsQueueRequest(IrpContext %p), %d\n", IrpContext, QueueCount);
45
46 ASSERT(!(IrpContext->Flags & IRPCONTEXT_QUEUE) &&
47 (IrpContext->Flags & IRPCONTEXT_COMPLETE));
48 IrpContext->Flags |= IRPCONTEXT_CANWAIT;
49 IoMarkIrpPending(IrpContext->Irp);
50 ExInitializeWorkItem(&IrpContext->WorkQueueItem, CdfsDoRequest, IrpContext);
51 ExQueueWorkItem(&IrpContext->WorkQueueItem, CriticalWorkQueue);
52
53 return STATUS_PENDING;
54 }
55
56 static
57 NTSTATUS
58 CdfsLockControl(
59 IN PCDFS_IRP_CONTEXT IrpContext)
60 {
61 PFCB Fcb;
62 NTSTATUS Status;
63
64 DPRINT("CdfsLockControl(IrpContext %p)\n", IrpContext);
65
66 if (IrpContext->DeviceObject == CdfsGlobalData->CdFsDeviceObject || IrpContext->DeviceObject == CdfsGlobalData->HddFsDeviceObject)
67 {
68 return STATUS_INVALID_DEVICE_REQUEST;
69 }
70
71 Fcb = IrpContext->FileObject->FsContext;
72 if (CdfsFCBIsDirectory(Fcb))
73 {
74 return STATUS_INVALID_PARAMETER;
75 }
76
77 IrpContext->Flags &= ~IRPCONTEXT_COMPLETE;
78 Status = FsRtlProcessFileLock(&Fcb->FileLock,
79 IrpContext->Irp,
80 NULL);
81 return Status;
82 }
83
84 static
85 NTSTATUS
86 CdfsDispatch(PCDFS_IRP_CONTEXT IrpContext)
87 {
88 PIRP Irp = IrpContext->Irp;
89 NTSTATUS Status = STATUS_UNSUCCESSFUL;
90
91 DPRINT("CdfsDispatch()\n");
92
93 FsRtlEnterFileSystem();
94
95 CdfsIsIrpTopLevel(Irp);
96
97 switch (IrpContext->MajorFunction)
98 {
99 case IRP_MJ_QUERY_VOLUME_INFORMATION:
100 Status = CdfsQueryVolumeInformation(IrpContext);
101 break;
102
103 case IRP_MJ_SET_VOLUME_INFORMATION:
104 Status = CdfsSetVolumeInformation(IrpContext);
105 break;
106
107 case IRP_MJ_QUERY_INFORMATION:
108 Status = CdfsQueryInformation(IrpContext);
109 break;
110
111 case IRP_MJ_SET_INFORMATION:
112 Status = CdfsSetInformation(IrpContext);
113 break;
114
115 case IRP_MJ_DIRECTORY_CONTROL:
116 Status = CdfsDirectoryControl(IrpContext);
117 break;
118
119 case IRP_MJ_READ:
120 Status = CdfsRead(IrpContext);
121 break;
122
123 case IRP_MJ_DEVICE_CONTROL:
124 Status = CdfsDeviceControl(IrpContext);
125 break;
126
127 case IRP_MJ_WRITE:
128 Status = CdfsWrite(IrpContext);
129 break;
130
131 case IRP_MJ_CLOSE:
132 Status = CdfsClose(IrpContext);
133 break;
134
135 case IRP_MJ_CREATE:
136 Status = CdfsCreate(IrpContext);
137 break;
138
139 case IRP_MJ_CLEANUP:
140 Status = CdfsCleanup(IrpContext);
141 break;
142
143 case IRP_MJ_FILE_SYSTEM_CONTROL:
144 Status = CdfsFileSystemControl(IrpContext);
145 break;
146
147 case IRP_MJ_LOCK_CONTROL:
148 Status = CdfsLockControl(IrpContext);
149 break;
150 }
151
152 ASSERT((!(IrpContext->Flags & IRPCONTEXT_COMPLETE) && !(IrpContext->Flags & IRPCONTEXT_QUEUE)) ||
153 ((IrpContext->Flags & IRPCONTEXT_COMPLETE) && !(IrpContext->Flags & IRPCONTEXT_QUEUE)) ||
154 (!(IrpContext->Flags & IRPCONTEXT_COMPLETE) && (IrpContext->Flags & IRPCONTEXT_QUEUE)));
155
156 if (IrpContext->Flags & IRPCONTEXT_COMPLETE)
157 {
158 Irp->IoStatus.Status = Status;
159 IoCompleteRequest(Irp, IrpContext->PriorityBoost);
160 }
161
162 if (IrpContext->Flags & IRPCONTEXT_QUEUE)
163 {
164 /* Reset our status flags before queueing the IRP */
165 IrpContext->Flags |= IRPCONTEXT_COMPLETE;
166 IrpContext->Flags &= ~IRPCONTEXT_QUEUE;
167 Status = CdfsQueueRequest(IrpContext);
168 }
169 else
170 {
171 ExFreeToNPagedLookasideList(&CdfsGlobalData->IrpContextLookasideList, IrpContext);
172 }
173
174 IoSetTopLevelIrp(NULL);
175 FsRtlExitFileSystem();
176
177 return Status;
178 }
179
180 static
181 VOID
182 NTAPI
183 CdfsDoRequest(PVOID IrpContext)
184 {
185 InterlockedDecrement(&QueueCount);
186 DPRINT("CdfsDoRequest(IrpContext %p), MajorFunction %x, %d\n",
187 IrpContext, ((PCDFS_IRP_CONTEXT)IrpContext)->MajorFunction, QueueCount);
188 CdfsDispatch((PCDFS_IRP_CONTEXT)IrpContext);
189 }
190
191 /*
192 * FUNCTION: This function manages IRP for various major functions
193 * ARGUMENTS:
194 * DriverObject = object describing this driver
195 * Irp = IRP to be passed to internal functions
196 * RETURNS: Status of I/O Request
197 */
198 NTSTATUS
199 NTAPI
200 CdfsFsdDispatch(
201 PDEVICE_OBJECT DeviceObject,
202 PIRP Irp)
203 {
204 PCDFS_IRP_CONTEXT IrpContext = NULL;
205 NTSTATUS Status;
206
207 DPRINT("CdfsFsdDispatch()\n");
208
209 IrpContext = CdfsAllocateIrpContext(DeviceObject, Irp);
210 if (IrpContext == NULL)
211 {
212 Status = STATUS_INSUFFICIENT_RESOURCES;
213 Irp->IoStatus.Status = Status;
214 IoCompleteRequest(Irp, IO_NO_INCREMENT);
215 }
216 else
217 {
218 Status = CdfsDispatch(IrpContext);
219 }
220
221 return Status;
222 }