ReactOS  0.4.14-dev-98-gb0d4763
pnp.c
Go to the documentation of this file.
1 /*
2  * FFS File System Driver for Windows
3  *
4  * misc.c
5  *
6  * 2004.5.6 ~
7  *
8  * Lee Jae-Hong, http://www.pyrasis.com
9  *
10  * See License.txt
11  *
12  */
13 
14 #include "ntifs.h"
15 #include "ffsdrv.h"
16 
17 
18 extern PFFS_GLOBAL FFSGlobal;
19 
20 #if (_WIN32_WINNT >= 0x0500)
21 
22 /* Globals */
23 
24 extern PFFS_GLOBAL FFSGlobal;
25 
26 /* Definitions */
27 
28 #define DBG_PNP DBG_USER
29 
30 #ifdef _PREFAST_
31 IO_COMPLETION_ROUTINE FFSPnpCompletionRoutine;
32 #endif // _PREFAST_
33 
35 FFSPnpCompletionRoutine(
37  IN PIRP Irp,
38  IN PVOID Contxt);
39 
40 
41 #ifdef ALLOC_PRAGMA
42 #pragma alloc_text(PAGE, FFSPnp)
43 #pragma alloc_text(PAGE, FFSPnpQueryRemove)
44 #pragma alloc_text(PAGE, FFSPnpRemove)
45 #pragma alloc_text(PAGE, FFSPnpCancelRemove)
46 #pragma alloc_text(PAGE, FFSPnpSurpriseRemove)
47 #endif
48 
49 
50 
52 FFSPnpCompletionRoutine(
54  IN PIRP Irp,
55  IN PVOID Contxt)
56 {
57  PKEVENT Event = (PKEVENT) Contxt;
58 
59  KeSetEvent(Event, 0, FALSE);
60 
62 
64  UNREFERENCED_PARAMETER(Contxt);
65 }
66 
67 
70 FFSPnp(
71  IN PFFS_IRP_CONTEXT IrpContext)
72 {
74 
75  PIRP Irp;
77  PFFS_VCB Vcb = 0;
79 
80  PAGED_CODE();
81 
82  _SEH2_TRY
83  {
84  ASSERT(IrpContext);
85 
86  ASSERT((IrpContext->Identifier.Type == FFSICX) &&
87  (IrpContext->Identifier.Size == sizeof(FFS_IRP_CONTEXT)));
88 
89  DeviceObject = IrpContext->DeviceObject;
90 
92 
93  ASSERT(Vcb != NULL);
94 
95  if (!((Vcb->Identifier.Type == FFSVCB) &&
96  (Vcb->Identifier.Size == sizeof(FFS_VCB))))
97  {
98  _SEH2_LEAVE; // Status = STATUS_INVALID_PARAMETER
99  }
100 
101  Irp = IrpContext->Irp;
103 
104  SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT);
105 
106  switch (IrpSp->MinorFunction)
107  {
108 
110 
111  FFSPrint((DBG_PNP, "FFSPnp: FFSPnpQueryRemove...\n"));
112  Status = FFSPnpQueryRemove(IrpContext, Vcb);
113 
114  break;
115 
117 
118  FFSPrint((DBG_PNP, "FFSPnp: FFSPnpRemove...\n"));
119  Status = FFSPnpRemove(IrpContext, Vcb);
120  break;
121 
123 
124  FFSPrint((DBG_PNP, "FFSPnp: FFSPnpCancelRemove...\n"));
125  Status = FFSPnpCancelRemove(IrpContext, Vcb);
126  break;
127 
129 
130  FFSPrint((DBG_PNP, "FFSPnp: FFSPnpSupriseRemove...\n"));
131  Status = FFSPnpSurpriseRemove(IrpContext, Vcb);
132  break;
133 
134  default:
135  break;
136  }
137  }
138 
140  {
141  if (!IrpContext->ExceptionInProgress)
142  {
143  Irp = IrpContext->Irp;
144 
145  if (Irp)
146  {
147  //
148  // Here we need pass the IRP to the disk driver.
149  //
150 
152 
153  Status = IoCallDriver(Vcb->TargetDeviceObject, Irp);
154 
155  IrpContext->Irp = NULL;
156  }
157 
158  FFSCompleteIrpContext(IrpContext, Status);
159  }
160  } _SEH2_END;
161 
162  return Status;
163 }
164 
165 
167 NTSTATUS
169  PFFS_IRP_CONTEXT IrpContext,
170  PFFS_VCB Vcb)
171 {
173  KEVENT Event;
174  BOOLEAN bDeleted = FALSE;
175  BOOLEAN VcbAcquired = FALSE;
176 
177  PAGED_CODE();
178 
179  _SEH2_TRY {
180 
181  FFSPrint((DBG_PNP, "FFSPnpQueryRemove by FFSPnp ...\n"));
182 
183  FFSPrint((DBG_PNP, "FFSPnpQueryRemove: FFSFlushVolume ...\n"));
184 
185 #if (_WIN32_WINNT >= 0x0500)
187 #endif
188 
190  &Vcb->MainResource, TRUE);
191  VcbAcquired = TRUE;
192 
194 
196 
197  FFSPrint((DBG_PNP, "FFSPnpQueryRemove: FFSLockVcb: Vcb=%xh FileObject=%xh ...\n", Vcb, IrpContext->FileObject));
198  Status = FFSLockVcb(Vcb, IrpContext->FileObject);
199 
200  FFSPrint((DBG_PNP, "FFSPnpQueryRemove: FFSPurgeVolume ...\n"));
202 
203  if (!NT_SUCCESS(Status))
204  {
205  _SEH2_LEAVE;
206  }
207 
209  &Vcb->MainResource,
211  VcbAcquired = FALSE;
212 
214 
216  IoSetCompletionRoutine(IrpContext->Irp,
217  FFSPnpCompletionRoutine,
218  &Event,
219  TRUE,
220  TRUE,
221  TRUE);
222 
223  FFSPrint((DBG_PNP, "FFSPnpQueryRemove: Call lower level driver...\n"));
224  Status = IoCallDriver(Vcb->TargetDeviceObject,
225  IrpContext->Irp);
226 
227  if (Status == STATUS_PENDING)
228  {
230  Executive,
231  KernelMode,
232  FALSE,
233  NULL);
234 
235  Status = IrpContext->Irp->IoStatus.Status;
236  }
237 
238  if (NT_SUCCESS(Status))
239  {
240  FFSPrint((DBG_PNP, "FFSPnpQueryRemove: FFSCheckDismount ...\n"));
241  bDeleted = FFSCheckDismount(IrpContext, Vcb, TRUE);
242  FFSPrint((DBG_PNP, "FFSPnpQueryRemove: FFSFlushVolume bDelted=%xh ...\n", bDeleted));
243  }
244 
245  ASSERT(!(NT_SUCCESS(Status) && !bDeleted));
246  }
247 
249  {
250  if (VcbAcquired)
251  {
253  &Vcb->MainResource,
255  }
256 
258  IrpContext->Irp, FALSE, (CCHAR)(NT_SUCCESS(Status) ?
260 
261  IrpContext->Irp = NULL;
262  } _SEH2_END;
263 
264  return Status;
265 }
266 
267 
269 NTSTATUS
271  PFFS_IRP_CONTEXT IrpContext,
272  PFFS_VCB Vcb)
273 {
275  KEVENT Event;
276  BOOLEAN bDeleted;
277 
278  PAGED_CODE();
279 
280  _SEH2_TRY
281  {
282 
283  FFSPrint((DBG_PNP, "FFSPnpRemove by FFSPnp ...\n"));
284 #if (_WIN32_WINNT >= 0x0500)
286 #endif
288  &Vcb->MainResource, TRUE);
289 
290  Status = FFSLockVcb(Vcb, IrpContext->FileObject);
291 
293  &Vcb->MainResource,
295 
296  //
297  // Setup the Irp. We'll send it to the lower disk driver.
298  //
299 
301 
303  IoSetCompletionRoutine(IrpContext->Irp,
304  FFSPnpCompletionRoutine,
305  &Event,
306  TRUE,
307  TRUE,
308  TRUE);
309 
310  Status = IoCallDriver(Vcb->TargetDeviceObject,
311  IrpContext->Irp);
312 
313  if (Status == STATUS_PENDING)
314  {
315 
317  Executive,
318  KernelMode,
319  FALSE,
320  NULL);
321 
322  Status = IrpContext->Irp->IoStatus.Status;
323  }
324 
326  &Vcb->MainResource, TRUE);
327 
329 
331  &Vcb->MainResource,
333 
334  bDeleted = FFSCheckDismount(IrpContext, Vcb, TRUE);
335  }
336 
338  {
340  IrpContext->Irp, FALSE, (CCHAR)(NT_SUCCESS(Status)?
342 
343  IrpContext->Irp = NULL;
344  } _SEH2_END;
345 
346  return Status;
347 }
348 
349 
351 NTSTATUS
353  PFFS_IRP_CONTEXT IrpContext,
354  PFFS_VCB Vcb)
355 {
357  KEVENT Event;
358  BOOLEAN bDeleted;
359 
360  PAGED_CODE();
361 
362  _SEH2_TRY
363  {
364 
365  FFSPrint((DBG_PNP, "FFSPnpSupriseRemove by FFSPnp ...\n"));
366 #if (_WIN32_WINNT >= 0x0500)
368 #endif
370  &Vcb->MainResource, TRUE);
371 
372  Status = FFSLockVcb(Vcb, IrpContext->FileObject);
373 
375  &Vcb->MainResource,
377 
378  //
379  // Setup the Irp. We'll send it to the lower disk driver.
380  //
381 
383 
385  IoSetCompletionRoutine(IrpContext->Irp,
386  FFSPnpCompletionRoutine,
387  &Event,
388  TRUE,
389  TRUE,
390  TRUE);
391 
392  Status = IoCallDriver(Vcb->TargetDeviceObject,
393  IrpContext->Irp);
394 
395  if (Status == STATUS_PENDING)
396  {
397 
399  Executive,
400  KernelMode,
401  FALSE,
402  NULL);
403 
404  Status = IrpContext->Irp->IoStatus.Status;
405  }
406 
408  &Vcb->MainResource, TRUE);
409 
411 
413  &Vcb->MainResource,
415 
416  bDeleted = FFSCheckDismount(IrpContext, Vcb, TRUE);
417  }
418 
420  {
422  IrpContext->Irp, FALSE, (CCHAR)(NT_SUCCESS(Status)?
424 
425  IrpContext->Irp = NULL;
426  } _SEH2_END;
427 
428  return Status;
429 }
430 
431 
433 NTSTATUS
435  PFFS_IRP_CONTEXT IrpContext,
436  PFFS_VCB Vcb)
437 {
439 
440  PAGED_CODE();
441 
442  FFSPrint((DBG_PNP, "FFSPnpCancelRemove by FFSPnp ...\n"));
443 
445  &Vcb->MainResource, TRUE);
446 
447  Status = FFSUnlockVcb(Vcb, IrpContext->FileObject);
448 
450  &Vcb->MainResource,
452 
453  IoSkipCurrentIrpStackLocation(IrpContext->Irp);
454 
455  Status = IoCallDriver(Vcb->TargetDeviceObject, IrpContext->Irp);
456 
457  IrpContext->Irp = NULL;
458 
459  return Status;
460 }
461 
462 
463 #endif //(_WIN32_WINNT >= 0x0500)
#define IRP_MN_CANCEL_REMOVE_DEVICE
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
#define IN
Definition: typedefs.h:38
NTSTATUS FFSUnlockVcb(IN PFFS_VCB Vcb, IN PFILE_OBJECT FileObject)
Definition: fsctl.c:491
#define TRUE
Definition: types.h:120
#define IRP_CONTEXT_FLAG_WAIT
Definition: cdstruc.h:1221
struct _KEVENT * PKEVENT
#define IRP_MN_REMOVE_DEVICE
#define __drv_mustHoldCriticalRegion
Definition: ffsdrv.h:34
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:63
_In_ PIRP Irp
Definition: csq.h:116
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
__drv_mustHoldCriticalRegion NTSTATUS FFSPnpRemove(PFFS_IRP_CONTEXT IrpContext, PFFS_VCB Vcb)
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS FFSLockVcb(IN PFFS_VCB Vcb, IN PFILE_OBJECT FileObject)
Definition: fsctl.c:358
PFFS_GLOBAL FFSGlobal
Definition: init.c:22
#define FFSCompleteRequest(Irp, bPrint, PriorityBoost)
Definition: ffsdrv.h:1049
Definition: ffsdrv.h:283
__drv_mustHoldCriticalRegion BOOLEAN FFSCheckDismount(IN PFFS_IRP_CONTEXT IrpContext, IN PFFS_VCB Vcb, IN BOOLEAN bForce)
Definition: fsctl.c:1332
__drv_mustHoldCriticalRegion NTSTATUS FFSPnpSurpriseRemove(PFFS_IRP_CONTEXT IrpContext, PFFS_VCB Vcb)
PFILE_OBJECT FileObject
Definition: ffsdrv.h:741
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define PAGED_CODE()
Definition: video.h:57
__drv_mustHoldCriticalRegion NTSTATUS FFSFlushVolume(IN PFFS_VCB Vcb, BOOLEAN bShutDown)
Definition: flush.c:107
__drv_mustHoldCriticalRegion NTSTATUS FFSPnp(IN PFFS_IRP_CONTEXT IrpContext)
_SEH2_TRY
Definition: create.c:4250
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:515
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
FORCEINLINE VOID IoCopyCurrentIrpStackLocationToNext(_Inout_ PIRP Irp)
Definition: iofuncs.h:2820
#define IO_DISK_INCREMENT
Definition: iotypes.h:568
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define IRP_MN_QUERY_REMOVE_DEVICE
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:435
PVOID DeviceExtension
Definition: env_spec_w32.h:418
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
__drv_mustHoldCriticalRegion NTSTATUS FFSPnpQueryRemove(PFFS_IRP_CONTEXT IrpContext, PFFS_VCB Vcb)
struct _FFS_VCB * PFFS_VCB
__drv_mustHoldCriticalRegion NTSTATUS FFSFlushFiles(IN PFFS_VCB Vcb, BOOLEAN bShutDown)
Definition: flush.c:54
#define DBG_PNP
Definition: pnp.c:21
NTSTATUS NTAPI CcWaitForCurrentLazyWriterActivity(VOID)
Definition: lazyrite.c:30
#define IRP_MN_SURPRISE_REMOVAL
Definition: ntifs_ex.h:408
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
char CCHAR
Definition: typedefs.h:50
#define Vcb
Definition: cdprocs.h:1425
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
__drv_mustHoldCriticalRegion NTSTATUS FFSCompleteIrpContext(IN PFFS_IRP_CONTEXT IrpContext, IN NTSTATUS Status)
Definition: read.c:53
Definition: ffsdrv.h:280
Status
Definition: gdiplustypes.h:24
#define FFSPrint(arg)
Definition: ffsdrv.h:1047
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define SetFlag(_F, _SF)
Definition: ext2fs.h:187
_SEH2_END
Definition: create.c:4424
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
__drv_mustHoldCriticalRegion NTSTATUS FFSPnpCancelRemove(PFFS_IRP_CONTEXT IrpContext, PFFS_VCB Vcb)
_In_ PIO_STACK_LOCATION IrpSp
Definition: create.c:4157
_SEH2_FINALLY
Definition: create.c:4395
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define IoSkipCurrentIrpStackLocation(Irp)
Definition: ntifs_ex.h:421
#define IO_NO_INCREMENT
Definition: iotypes.h:566
VOID NTAPI ExReleaseResourceForThreadLite(IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread)
Definition: resource.c:1844
#define _SEH2_LEAVE
Definition: filesup.c:20
__drv_mustHoldCriticalRegion NTSTATUS FFSPurgeVolume(IN PFFS_VCB Vcb, IN BOOLEAN FlushBeforePurge)
Definition: fsctl.c:1445