[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 CdfsDispatch(PCDFS_IRP_CONTEXT IrpContext)
59 {
60 PIRP Irp = IrpContext->Irp;
61 NTSTATUS Status = STATUS_UNSUCCESSFUL;
62
63 DPRINT("CdfsDispatch()\n");
64
65 FsRtlEnterFileSystem();
66
67 CdfsIsIrpTopLevel(Irp);
68
69 switch (IrpContext->MajorFunction)
70 {
71 case IRP_MJ_QUERY_VOLUME_INFORMATION:
72 Status = CdfsQueryVolumeInformation(IrpContext);
73 break;
74
75 case IRP_MJ_SET_VOLUME_INFORMATION:
76 Status = CdfsSetVolumeInformation(IrpContext);
77 break;
78
79 case IRP_MJ_QUERY_INFORMATION:
80 Status = CdfsQueryInformation(IrpContext);
81 break;
82
83 case IRP_MJ_SET_INFORMATION:
84 Status = CdfsSetInformation(IrpContext);
85 break;
86
87 case IRP_MJ_DIRECTORY_CONTROL:
88 Status = CdfsDirectoryControl(IrpContext);
89 break;
90
91 case IRP_MJ_READ:
92 Status = CdfsRead(IrpContext);
93 break;
94
95 case IRP_MJ_DEVICE_CONTROL:
96 Status = CdfsDeviceControl(IrpContext);
97 break;
98
99 case IRP_MJ_WRITE:
100 Status = CdfsWrite(IrpContext);
101 break;
102
103 case IRP_MJ_CLOSE:
104 Status = CdfsClose(IrpContext);
105 break;
106
107 case IRP_MJ_CREATE:
108 // Status = CdfsCreate(IrpContext);
109 break;
110
111 case IRP_MJ_CLEANUP:
112 Status = CdfsCleanup(IrpContext);
113 break;
114
115 case IRP_MJ_FILE_SYSTEM_CONTROL:
116 Status = CdfsFileSystemControl(IrpContext);
117 break;
118 }
119
120 ASSERT((!(IrpContext->Flags & IRPCONTEXT_COMPLETE) && !(IrpContext->Flags & IRPCONTEXT_QUEUE)) ||
121 ((IrpContext->Flags & IRPCONTEXT_COMPLETE) && !(IrpContext->Flags & IRPCONTEXT_QUEUE)) ||
122 (!(IrpContext->Flags & IRPCONTEXT_COMPLETE) && (IrpContext->Flags & IRPCONTEXT_QUEUE)));
123
124 if (IrpContext->Flags & IRPCONTEXT_COMPLETE)
125 {
126 Irp->IoStatus.Status = Status;
127 IoCompleteRequest(Irp, IrpContext->PriorityBoost);
128 }
129
130 if (IrpContext->Flags & IRPCONTEXT_QUEUE)
131 {
132 /* Reset our status flags before queueing the IRP */
133 IrpContext->Flags |= IRPCONTEXT_COMPLETE;
134 IrpContext->Flags &= ~IRPCONTEXT_QUEUE;
135 Status = CdfsQueueRequest(IrpContext);
136 }
137 else
138 {
139 ExFreeToNPagedLookasideList(&CdfsGlobalData->IrpContextLookasideList, IrpContext);
140 }
141
142 IoSetTopLevelIrp(NULL);
143 FsRtlExitFileSystem();
144
145 return Status;
146 }
147
148 static
149 VOID
150 NTAPI
151 CdfsDoRequest(PVOID IrpContext)
152 {
153 InterlockedDecrement(&QueueCount);
154 DPRINT("CdfsDoRequest(IrpContext %p), MajorFunction %x, %d\n",
155 IrpContext, ((PCDFS_IRP_CONTEXT)IrpContext)->MajorFunction, QueueCount);
156 CdfsDispatch((PCDFS_IRP_CONTEXT)IrpContext);
157 }
158
159 /*
160 * FUNCTION: This function manages IRP for various major functions
161 * ARGUMENTS:
162 * DriverObject = object describing this driver
163 * Irp = IRP to be passed to internal functions
164 * RETURNS: Status of I/O Request
165 */
166 NTSTATUS
167 NTAPI
168 CdfsFsdDispatch(
169 PDEVICE_OBJECT DeviceObject,
170 PIRP Irp)
171 {
172 PCDFS_IRP_CONTEXT IrpContext = NULL;
173 NTSTATUS Status;
174
175 DPRINT("CdfsFsdDispatch()\n");
176
177 IrpContext = CdfsAllocateIrpContext(DeviceObject, Irp);
178 if (IrpContext == NULL)
179 {
180 Status = STATUS_INSUFFICIENT_RESOURCES;
181 Irp->IoStatus.Status = Status;
182 IoCompleteRequest(Irp, IO_NO_INCREMENT);
183 }
184 else
185 {
186 Status = CdfsDispatch(IrpContext);
187 }
188
189 return Status;
190 }