WDK Mini Filter Example
CtxInit.c
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) 1999 - 2003 Microsoft Corporation
4 
5 Module Name:
6 
7  ContextInit.c
8 
9 Abstract:
10 
11  This is the main module of the kernel mode filter driver implementing
12  the context sample.
13 
14 
15 Environment:
16 
17  Kernel mode
18 
19 
20 --*/
21 
22 #include "pch.h"
23 
24 //
25 // Global variables
26 //
27 
29 
30 
31 //
32 // Local function prototypes
33 //
34 
35 DRIVER_INITIALIZE DriverEntry;
36 NTSTATUS
38  _In_ PDRIVER_OBJECT DriverObject,
39  _In_ PUNICODE_STRING RegistryPath
40  );
41 
42 NTSTATUS
43 CtxUnload (
44  _In_ FLT_FILTER_UNLOAD_FLAGS Flags
45  );
46 
47 VOID
49  _In_ PFLT_CONTEXT Context,
50  _In_ FLT_CONTEXT_TYPE ContextType
51  );
52 
53 NTSTATUS
55  _In_ PCFLT_RELATED_OBJECTS FltObjects,
56  _In_ FLT_INSTANCE_SETUP_FLAGS Flags,
57  _In_ DEVICE_TYPE VolumeDeviceType,
58  _In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
59  );
60 
61 NTSTATUS
63  _In_ PCFLT_RELATED_OBJECTS FltObjects,
64  _In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
65  );
66 
67 VOID
69  _In_ PCFLT_RELATED_OBJECTS FltObjects,
70  _In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
71  );
72 
73 VOID
75  _In_ PCFLT_RELATED_OBJECTS FltObjects,
76  _In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
77  );
78 
79 #if DBG
80 
81 VOID
82 CtxInitializeDebugLevel (
83  _In_ PUNICODE_STRING RegistryPath
84  );
85 
86 #endif
87 
88 //
89 // Assign text sections for each routine.
90 //
91 
92 #ifdef ALLOC_PRAGMA
93 #pragma alloc_text(INIT, DriverEntry)
94 
95 #if DBG
96 #pragma alloc_text(INIT, CtxInitializeDebugLevel)
97 #endif
98 
99 #pragma alloc_text(PAGE, CtxUnload)
100 #pragma alloc_text(PAGE, CtxContextCleanup)
101 #pragma alloc_text(PAGE, CtxInstanceSetup)
102 #pragma alloc_text(PAGE, CtxInstanceQueryTeardown)
103 #pragma alloc_text(PAGE, CtxInstanceTeardownStart)
104 #pragma alloc_text(PAGE, CtxInstanceTeardownComplete)
105 #endif
106 
107 
108 //
109 // Filters callback routines
110 //
111 
112 FLT_OPERATION_REGISTRATION Callbacks[] = {
113 
114  { IRP_MJ_CREATE,
115  FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
116  CtxPreCreate,
117  CtxPostCreate },
118 
119  { IRP_MJ_CLEANUP,
120  FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
122  NULL },
123 
124  { IRP_MJ_CLOSE,
125  FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
126  CtxPreClose,
127  NULL },
128 
130  FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
132  CtxPostSetInfo },
133 
134  { IRP_MJ_OPERATION_END }
135 };
136 
137 const FLT_CONTEXT_REGISTRATION ContextRegistration[] = {
138 
139  { FLT_INSTANCE_CONTEXT,
140  0,
144 
145  { FLT_FILE_CONTEXT,
146  0,
150 
151  { FLT_STREAM_CONTEXT,
152  0,
156 
157  { FLT_STREAMHANDLE_CONTEXT,
158  0,
162 
163  { FLT_CONTEXT_END }
164 };
165 
166 //
167 // Filters registration data structure
168 //
169 
170 FLT_REGISTRATION FilterRegistration = {
171 
172  sizeof( FLT_REGISTRATION ), // Size
173  FLT_REGISTRATION_VERSION, // Version
174  0, // Flags
175  ContextRegistration, // Context
176  Callbacks, // Operation callbacks
177  CtxUnload, // Filters unload routine
178  CtxInstanceSetup, // InstanceSetup routine
179  CtxInstanceQueryTeardown, // InstanceQueryTeardown routine
180  CtxInstanceTeardownStart, // InstanceTeardownStart routine
181  CtxInstanceTeardownComplete, // InstanceTeardownComplete routine
182  NULL, NULL, NULL // Unused naming support callbacks
183 };
184 
185 //
186 // Filter driver initialization and unload routines
187 //
188 
189 NTSTATUS
191  _In_ PDRIVER_OBJECT DriverObject,
192  _In_ PUNICODE_STRING RegistryPath
193  )
194 /*++
195 
196 Routine Description:
197 
198  This is the initialization routine for this filter driver. It registers
199  itself with the filter manager and initializes all its global data structures.
200 
201 Arguments:
202 
203  DriverObject - Pointer to driver object created by the system to
204  represent this driver.
205 
206  RegistryPath - Unicode string identifying where the parameters for this
207  driver are located in the registry.
208 
209 Return Value:
210 
211  Returns STATUS_SUCCESS.
212 
213 --*/
214 {
215  NTSTATUS status;
216 
217  //
218  // Default to NonPagedPoolNx for non paged pool allocations where supported.
219  //
220 
221  ExInitializeDriverRuntime( DrvRtPoolNxOptIn );
222 
223  RtlZeroMemory( &Globals, sizeof( Globals ) );
224 
225 #if DBG
226 
227  //
228  // Initialize global debug level
229  //
230 
231  CtxInitializeDebugLevel( RegistryPath );
232 
233 #else
234 
235  UNREFERENCED_PARAMETER( RegistryPath );
236 
237 #endif
238 
239  DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
240  ("[Ctx]: Driver being loaded\n") );
241 
242 
243 
244  //
245  // Register with the filter manager
246  //
247 
248  status = FltRegisterFilter( DriverObject,
250  &Globals.Filter );
251 
252  if (!NT_SUCCESS( status )) {
253 
254  return status;
255  }
256 
257  //
258  // Start filtering I/O
259  //
260 
261  status = FltStartFiltering( Globals.Filter );
262 
263  if (!NT_SUCCESS( status )) {
264 
265  FltUnregisterFilter( Globals.Filter );
266  }
267 
268  DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
269  ("[Ctx]: Driver loaded complete (Status = 0x%08X)\n",
270  status) );
271 
272  return status;
273 }
274 
275 #if DBG
276 
277 VOID
278 CtxInitializeDebugLevel (
279  _In_ PUNICODE_STRING RegistryPath
280  )
281 /*++
282 
283 Routine Description:
284 
285  This routine tries to read the filter DebugLevel parameter from
286  the registry. This value will be found in the registry location
287  indicated by the RegistryPath passed in.
288 
289 Arguments:
290 
291  RegistryPath - The path key passed to the driver during DriverEntry.
292 
293 Return Value:
294 
295  None.
296 
297 --*/
298 {
299  OBJECT_ATTRIBUTES attributes;
300  HANDLE driverRegKey;
301  NTSTATUS status;
302  ULONG resultLength;
303  UNICODE_STRING valueName;
304  UCHAR buffer[sizeof( KEY_VALUE_PARTIAL_INFORMATION ) + sizeof( LONG )];
305 
306  Globals.DebugLevel = DEBUG_TRACE_ERROR;
307 
308  //
309  // Open the desired registry key
310  //
311 
312  InitializeObjectAttributes( &attributes,
313  RegistryPath,
314  OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
315  NULL,
316  NULL );
317 
318  status = ZwOpenKey( &driverRegKey,
319  KEY_READ,
320  &attributes );
321 
322  if (NT_SUCCESS( status )) {
323 
324  //
325  // Read the DebugFlags value from the registry.
326  //
327 
328  RtlInitUnicodeString( &valueName, L"DebugLevel" );
329 
330  status = ZwQueryValueKey( driverRegKey,
331  &valueName,
333  buffer,
334  sizeof(buffer),
335  &resultLength );
336 
337  if (NT_SUCCESS( status )) {
338 
339  Globals.DebugLevel = *((PULONG) &(((PKEY_VALUE_PARTIAL_INFORMATION) buffer)->Data));
340  }
341 
342  //
343  // Close the registry entry
344  //
345 
346  ZwClose( driverRegKey );
347 
348  }
349 
350 }
351 
352 #endif
353 
354 NTSTATUS
356  _In_ FLT_FILTER_UNLOAD_FLAGS Flags
357  )
358 /*++
359 
360 Routine Description:
361 
362  This is the unload routine for this filter driver. This is called
363  when the minifilter is about to be unloaded. We can fail this unload
364  request if this is not a mandatory unloaded indicated by the Flags
365  parameter.
366 
367 Arguments:
368 
369  Flags - Indicating if this is a mandatory unload.
370 
371 Return Value:
372 
373  Returns the final status of this operation.
374 
375 --*/
376 {
377  UNREFERENCED_PARAMETER( Flags );
378 
379  PAGED_CODE();
380 
381  DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
382  ("[Ctx]: Unloading driver\n") );
383 
384 
385  FltUnregisterFilter( Globals.Filter );
386  Globals.Filter = NULL;
387 
388  return STATUS_SUCCESS;
389 }
390 
391 VOID
393  _In_ PFLT_CONTEXT Context,
394  _In_ FLT_CONTEXT_TYPE ContextType
395  )
396 {
397  PCTX_INSTANCE_CONTEXT instanceContext;
398  PCTX_FILE_CONTEXT fileContext;
399  PCTX_STREAM_CONTEXT streamContext;
400  PCTX_STREAMHANDLE_CONTEXT streamHandleContext;
401 
402  PAGED_CODE();
403 
404  switch(ContextType) {
405 
406  case FLT_INSTANCE_CONTEXT:
407 
408  instanceContext = (PCTX_INSTANCE_CONTEXT) Context;
409 
410  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
411  ("[Ctx]: Cleaning up instance context for volume %wZ (Context = %p)\n",
412  &instanceContext->VolumeName,
413  Context) );
414 
415  //
416  // Here the filter should free memory or synchronization objects allocated to
417  // objects within the instance context. The instance context itself should NOT
418  // be freed. It will be freed by Filter Manager when the ref count on the
419  // context falls to zero.
420  //
421 
422  CtxFreeUnicodeString( &instanceContext->VolumeName );
423 
424  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
425  ("[Ctx]: Instance context cleanup complete.\n") );
426 
427  break;
428 
429 
430  case FLT_FILE_CONTEXT:
431 
432  fileContext = (PCTX_FILE_CONTEXT) Context;
433 
434  DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
435  ("[Ctx]: Cleaning up file context for file %wZ (FileContext = %p)\n",
436  &fileContext->FileName,
437  fileContext) );
438 
439 
440  //
441  // Free the file name
442  //
443 
444  if (fileContext->FileName.Buffer != NULL) {
445 
446  CtxFreeUnicodeString(&fileContext->FileName);
447  }
448 
449  DebugTrace( DEBUG_TRACE_FILE_CONTEXT_OPERATIONS,
450  ("[Ctx]: File context cleanup complete.\n") );
451 
452  break;
453 
454  case FLT_STREAM_CONTEXT:
455 
456  streamContext = (PCTX_STREAM_CONTEXT) Context;
457 
458  DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
459  ("[Ctx]: Cleaning up stream context for file %wZ (StreamContext = %p) \n\tCreateCount = %x \n\tCleanupCount = %x, \n\tCloseCount = %x\n",
460  &streamContext->FileName,
461  streamContext,
462  streamContext->CreateCount,
463  streamContext->CleanupCount,
464  streamContext->CloseCount) );
465 
466  //
467  // Delete the resource and memory the memory allocated for the resource
468  //
469 
470  if (streamContext->Resource != NULL) {
471 
472  ExDeleteResourceLite( streamContext->Resource );
473  CtxFreeResource( streamContext->Resource );
474  }
475 
476  //
477  // Free the file name
478  //
479 
480  if (streamContext->FileName.Buffer != NULL) {
481 
482  CtxFreeUnicodeString(&streamContext->FileName);
483  }
484 
485  DebugTrace( DEBUG_TRACE_STREAM_CONTEXT_OPERATIONS,
486  ("[Ctx]: Stream context cleanup complete.\n") );
487 
488  break;
489 
490  case FLT_STREAMHANDLE_CONTEXT:
491 
492  streamHandleContext = (PCTX_STREAMHANDLE_CONTEXT) Context;
493 
494  DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
495  ("[Ctx]: Cleaning up stream handle context for file %wZ (StreamContext = %p)\n",
496  &streamHandleContext->FileName,
497  streamHandleContext) );
498 
499  //
500  // Delete the resource and memory the memory allocated for the resource
501  //
502 
503  if (streamHandleContext->Resource != NULL) {
504 
505  ExDeleteResourceLite( streamHandleContext->Resource );
506  CtxFreeResource( streamHandleContext->Resource );
507  }
508 
509  //
510  // Free the file name
511  //
512 
513  if (streamHandleContext->FileName.Buffer != NULL) {
514 
515  CtxFreeUnicodeString(&streamHandleContext->FileName);
516  }
517 
518  DebugTrace( DEBUG_TRACE_STREAMHANDLE_CONTEXT_OPERATIONS,
519  ("[Ctx]: Stream handle context cleanup complete.\n") );
520 
521  break;
522 
523  }
524 
525 }
526 
527 //
528 // Instance setup/teardown routines.
529 //
530 
531 NTSTATUS
533  _In_ PCFLT_RELATED_OBJECTS FltObjects,
534  _In_ FLT_INSTANCE_SETUP_FLAGS Flags,
535  _In_ DEVICE_TYPE VolumeDeviceType,
536  _In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
537  )
538 /*++
539 
540 Routine Description:
541 
542  This routine is called whenever a new instance is created on a volume. This
543  gives us a chance to decide if we need to attach to this volume or not.
544 
545 Arguments:
546 
547  FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
548  opaque handles to this filter, instance and its associated volume.
549 
550  Flags - Flags describing the reason for this attach request.
551 
552 Return Value:
553 
554  STATUS_SUCCESS - attach
555  STATUS_FLT_DO_NOT_ATTACH - do not attach
556 
557 --*/
558 {
559  PCTX_INSTANCE_CONTEXT instanceContext = NULL;
560  NTSTATUS status = STATUS_SUCCESS;
561  ULONG volumeNameLength;
562 
563  UNREFERENCED_PARAMETER( Flags );
564  UNREFERENCED_PARAMETER( VolumeDeviceType );
565  UNREFERENCED_PARAMETER( VolumeFilesystemType );
566 
567  PAGED_CODE();
568 
569  DebugTrace( DEBUG_TRACE_INSTANCES,
570  ("[Ctx]: Instance setup started (Volume = %p, Instance = %p)\n",
571  FltObjects->Volume,
572  FltObjects->Instance) );
573 
574 
575  //
576  // Allocate and initialize the context for this volume
577  //
578 
579 
580  //
581  // Allocate the instance context
582  //
583 
584  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
585  ("[Ctx]: Allocating instance context (Volume = %p, Instance = %p)\n",
586  FltObjects->Volume,
587  FltObjects->Instance) );
588 
589  status = FltAllocateContext( FltObjects->Filter,
590  FLT_INSTANCE_CONTEXT,
592  NonPagedPool,
593  &instanceContext );
594 
595  if (!NT_SUCCESS( status )) {
596 
597  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
598  ("[Ctx]: Failed to allocate instance context (Volume = %p, Instance = %p, Status = 0x%x)\n",
599  FltObjects->Volume,
600  FltObjects->Instance,
601  status) );
602 
603  goto CtxInstanceSetupCleanup;
604  }
605 
606  //
607  // Get the NT volume name length
608  //
609 
610  status = FltGetVolumeName( FltObjects->Volume, NULL, &volumeNameLength );
611 
612  if( !NT_SUCCESS( status ) &&
613  (status != STATUS_BUFFER_TOO_SMALL) ) {
614 
615  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
616  ("[Ctx]: Unexpected failure in FltGetVolumeName. (Volume = %p, Instance = %p, Status = 0x%x)\n",
617  FltObjects->Volume,
618  FltObjects->Instance,
619  status) );
620 
621  goto CtxInstanceSetupCleanup;
622  }
623 
624  //
625  // Allocate a string big enough to take the volume name
626  //
627 
628  instanceContext->VolumeName.MaximumLength = (USHORT) volumeNameLength;
629  status = CtxAllocateUnicodeString( &instanceContext->VolumeName );
630 
631  if( !NT_SUCCESS( status )) {
632 
633  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
634  ("[Ctx]: Failed to allocate volume name string. (Volume = %p, Instance = %p, Status = 0x%x)\n",
635  FltObjects->Volume,
636  FltObjects->Instance,
637  status) );
638 
639  goto CtxInstanceSetupCleanup;
640  }
641 
642  //
643  // Get the NT volume name
644  //
645 
646  status = FltGetVolumeName( FltObjects->Volume, &instanceContext->VolumeName, &volumeNameLength );
647 
648  if( !NT_SUCCESS( status ) ) {
649 
650  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
651  ("[Ctx]: Unexpected failure in FltGetVolumeName. (Volume = %p, Instance = %p, Status = 0x%x)\n",
652  FltObjects->Volume,
653  FltObjects->Instance,
654  status) );
655 
656  goto CtxInstanceSetupCleanup;
657  }
658 
659 
660  instanceContext->Instance = FltObjects->Instance;
661  instanceContext->Volume = FltObjects->Volume;
662 
663  //
664  // Set the instance context.
665  //
666 
667  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
668  ("[Ctx]: Setting instance context %p for volume %wZ (Volume = %p, Instance = %p)\n",
669  instanceContext,
670  &instanceContext->VolumeName,
671  FltObjects->Volume,
672  FltObjects->Instance) );
673 
674  status = FltSetInstanceContext( FltObjects->Instance,
675  FLT_SET_CONTEXT_KEEP_IF_EXISTS,
676  instanceContext,
677  NULL );
678 
679  if( !NT_SUCCESS( status )) {
680 
681  DebugTrace( DEBUG_TRACE_INSTANCES | DEBUG_TRACE_ERROR,
682  ("[Ctx]: Failed to set instance context for volume %wZ (Volume = %p, Instance = %p, Status = 0x%08X)\n",
683  &instanceContext->VolumeName,
684  FltObjects->Volume,
685  FltObjects->Instance,
686  status) );
687  goto CtxInstanceSetupCleanup;
688  }
689 
690 
691 CtxInstanceSetupCleanup:
692 
693  //
694  // If FltAllocateContext suceeded then we MUST release the context,
695  // irrespective of whether FltSetInstanceContext suceeded or not.
696  //
697  // FltAllocateContext increments the ref count by one.
698  // A successful FltSetInstanceContext increments the ref count by one
699  // and also associates the context with the file system object
700  //
701  // FltReleaseContext decrements the ref count by one.
702  //
703  // When FltSetInstanceContext succeeds, calling FltReleaseContext will
704  // leave the context with a ref count of 1 corresponding to the internal
705  // reference to the context from the file system structures
706  //
707  // When FltSetInstanceContext fails, calling FltReleaseContext will
708  // leave the context with a ref count of 0 which is correct since
709  // there is no reference to the context from the file system structures
710  //
711 
712  if ( instanceContext != NULL ) {
713 
714  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
715  ("[Ctx]: Releasing instance context %p (Volume = %p, Instance = %p)\n",
716  instanceContext,
717  FltObjects->Volume,
718  FltObjects->Instance) );
719 
720  FltReleaseContext( instanceContext );
721  }
722 
723 
724  if (NT_SUCCESS( status )) {
725 
726  DebugTrace( DEBUG_TRACE_INSTANCES,
727  ("[Ctx]: Instance setup complete (Volume = %p, Instance = %p). Filter will attach to the volume.\n",
728  FltObjects->Volume,
729  FltObjects->Instance) );
730  } else {
731 
732  DebugTrace( DEBUG_TRACE_INSTANCES,
733  ("[Ctx]: Instance setup complete (Volume = %p, Instance = %p). Filter will not attach to the volume.\n",
734  FltObjects->Volume,
735  FltObjects->Instance) );
736  }
737 
738  return status;
739 }
740 
741 
742 NTSTATUS
744  _In_ PCFLT_RELATED_OBJECTS FltObjects,
745  _In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
746  )
747 /*++
748 
749 Routine Description:
750 
751  This is called when an instance is being manually deleted by a
752  call to FltDetachVolume or FilterDetach thereby giving us a
753  chance to fail that detach request.
754 
755 Arguments:
756 
757  FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
758  opaque handles to this filter, instance and its associated volume.
759 
760  Flags - Indicating where this detach request came from.
761 
762 Return Value:
763 
764  Returns the status of this operation.
765 
766 --*/
767 {
768  UNREFERENCED_PARAMETER( FltObjects );
769  UNREFERENCED_PARAMETER( Flags );
770 
771  PAGED_CODE();
772 
773  DebugTrace( DEBUG_TRACE_INSTANCES,
774  ("[Ctx]: Instance query teardown started (Instance = %p)\n",
775  FltObjects->Instance) );
776 
777 
778  DebugTrace( DEBUG_TRACE_INSTANCES,
779  ("[Ctx]: Instance query teadown ended (Instance = %p)\n",
780  FltObjects->Instance) );
781  return STATUS_SUCCESS;
782 }
783 
784 
785 VOID
787  _In_ PCFLT_RELATED_OBJECTS FltObjects,
788  _In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
789  )
790 /*++
791 
792 Routine Description:
793 
794  This routine is called at the start of instance teardown.
795 
796 Arguments:
797 
798  FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
799  opaque handles to this filter, instance and its associated volume.
800 
801  Flags - Reason why this instance is been deleted.
802 
803 Return Value:
804 
805  None.
806 
807 --*/
808 {
809  UNREFERENCED_PARAMETER( FltObjects );
810  UNREFERENCED_PARAMETER( Flags );
811 
812  PAGED_CODE();
813 
814  DebugTrace( DEBUG_TRACE_INSTANCES,
815  ("[Ctx]: Instance teardown start started (Instance = %p)\n",
816  FltObjects->Instance) );
817 
818 
819  DebugTrace( DEBUG_TRACE_INSTANCES,
820  ("[Ctx]: Instance teardown start ended (Instance = %p)\n",
821  FltObjects->Instance) );
822 }
823 
824 
825 VOID
827  _In_ PCFLT_RELATED_OBJECTS FltObjects,
828  _In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
829  )
830 /*++
831 
832 Routine Description:
833 
834  This routine is called at the end of instance teardown.
835 
836 Arguments:
837 
838  FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
839  opaque handles to this filter, instance and its associated volume.
840 
841  Flags - Reason why this instance is been deleted.
842 
843 Return Value:
844 
845  None.
846 
847 --*/
848 {
849  PCTX_INSTANCE_CONTEXT instanceContext;
850  NTSTATUS status;
851 
852  UNREFERENCED_PARAMETER( Flags );
853 
854  PAGED_CODE();
855 
856  DebugTrace( DEBUG_TRACE_INSTANCES,
857  ("[Ctx]: Instance teardown complete started (Instance = %p)\n",
858  FltObjects->Instance) );
859 
860  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
861  ("[Ctx]: Getting instance context (Volume = %p, Instance = %p)\n",
862  FltObjects->Volume,
863  FltObjects->Instance) );
864 
865  status = FltGetInstanceContext( FltObjects->Instance,
866  &instanceContext );
867 
868  if (NT_SUCCESS( status )) {
869 
870  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
871  ("[Ctx]: Instance teardown for volume %wZ (Volume = %p, Instance = %p, InstanceContext = %p)\n",
872  &instanceContext->VolumeName,
873  FltObjects->Volume,
874  FltObjects->Instance,
875  instanceContext) );
876 
877 
878  //
879  // Here the filter may perform any teardown of its own structures associated
880  // with this instance.
881  //
882  // The filter should not free memory or synchronization objects allocated to
883  // objects within the instance context. That should be performed in the
884  // cleanup callback for the instance context
885  //
886 
887  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS,
888  ("[Ctx]: Releasing instance context %p for volume %wZ (Volume = %p, Instance = %p)\n",
889  instanceContext,
890  &instanceContext->VolumeName,
891  FltObjects->Volume,
892  FltObjects->Instance) );
893 
894  FltReleaseContext( instanceContext );
895  } else {
896 
897  DebugTrace( DEBUG_TRACE_INSTANCE_CONTEXT_OPERATIONS | DEBUG_TRACE_ERROR,
898  ("[Ctx]: Failed to get instance context (Volume = %p, Instance = %p Status = 0x%x)\n",
899  FltObjects->Volume,
900  FltObjects->Instance,
901  status) );
902  }
903 
904  DebugTrace( DEBUG_TRACE_INSTANCES,
905  ("[Ctx]: Instance teardown complete ended (Instance = %p)\n",
906  FltObjects->Instance) );
907 }
908 
#define CTX_STREAMHANDLE_CONTEXT_SIZE
Definition: CtxStruc.h:178
UNICODE_STRING FileName
Definition: CtxStruc.h:168
#define CTX_FILE_CONTEXT_TAG
Definition: CtxStruc.h:29
struct _CTX_STREAMHANDLE_CONTEXT * PCTX_STREAMHANDLE_CONTEXT
_In_ PCWSTR valueName
Definition: ncinit.c:8
#define IRP_MJ_CLEANUP
Definition: mspyLog.h:302
struct _CTX_STREAM_CONTEXT * PCTX_STREAM_CONTEXT
NTSTATUS CtxInstanceSetup(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_SETUP_FLAGS Flags, _In_ DEVICE_TYPE VolumeDeviceType, _In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType)
Definition: CtxInit.c:532
VOID CtxContextCleanup(_In_ PFLT_CONTEXT Context, _In_ FLT_CONTEXT_TYPE ContextType)
Definition: CtxInit.c:392
VOID CtxInstanceTeardownStart(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags)
Definition: CtxInit.c:786
#define DebugTrace(Level, Data)
Definition: cancelSafe.c:36
NTSTATUS CtxUnload(_In_ FLT_FILTER_UNLOAD_FLAGS Flags)
Definition: CtxInit.c:355
#define CTX_INSTANCE_CONTEXT_SIZE
Definition: CtxStruc.h:88
#define CTX_STREAM_CONTEXT_TAG
Definition: CtxStruc.h:30
#define CTX_STREAMHANDLE_CONTEXT_TAG
Definition: CtxStruc.h:31
FLT_POSTOP_CALLBACK_STATUS CtxPostCreate(_Inout_ PFLT_CALLBACK_DATA Cbd, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Inout_opt_ PVOID CbdContext, _In_ FLT_POST_OPERATION_FLAGS Flags)
CTX_GLOBAL_DATA Globals
Definition: CtxInit.c:28
FLT_OPERATION_REGISTRATION Callbacks[]
Definition: CtxInit.c:112
NcLoadRegistryStringRetry KeyValuePartialInformation
Definition: ncinit.c:53
PFLT_VOLUME Volume
Definition: CtxStruc.h:78
#define CTX_INSTANCE_CONTEXT_TAG
Definition: CtxStruc.h:28
#define IRP_MJ_SET_INFORMATION
Definition: mspyLog.h:290
PERESOURCE Resource
Definition: CtxStruc.h:150
FLT_REGISTRATION FilterRegistration
Definition: CtxInit.c:170
UNREFERENCED_PARAMETER(FileObject)
NcLoadRegistryStringRetry NULL
Definition: ncinit.c:53
#define CTX_STREAM_CONTEXT_SIZE
Definition: CtxStruc.h:154
UNICODE_STRING FileName
Definition: CtxStruc.h:101
FLT_PREOP_CALLBACK_STATUS CtxPreClose(_Inout_ PFLT_CALLBACK_DATA Cbd, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Flt_CompletionContext_Outptr_ PVOID *CompletionContext)
#define IRP_MJ_CLOSE
Definition: mspyLog.h:286
UNICODE_STRING VolumeName
Definition: CtxStruc.h:84
PAGED_CODE()
FLT_POSTOP_CALLBACK_STATUS CtxPostSetInfo(_Inout_ PFLT_CALLBACK_DATA Cbd, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Inout_opt_ PVOID CbdContext, _In_ FLT_POST_OPERATION_FLAGS Flags)
PFLT_FILTER Filter
Definition: CtxStruc.h:44
struct _CTX_INSTANCE_CONTEXT * PCTX_INSTANCE_CONTEXT
PFLT_INSTANCE Instance
Definition: CtxStruc.h:72
UNICODE_STRING FileName
Definition: CtxStruc.h:126
DRIVER_INITIALIZE DriverEntry
Definition: CtxInit.c:35
FLT_PREOP_CALLBACK_STATUS CtxPreSetInfo(_Inout_ PFLT_CALLBACK_DATA Cbd, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Flt_CompletionContext_Outptr_ PVOID *CompletionContext)
const FLT_CONTEXT_REGISTRATION ContextRegistration[]
Definition: CtxInit.c:137
#define CTX_FILE_CONTEXT_SIZE
Definition: CtxStruc.h:112
FLT_PREOP_CALLBACK_STATUS CtxPreCreate(_Inout_ PFLT_CALLBACK_DATA Cbd, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Flt_CompletionContext_Outptr_ PVOID *CompletionContext)
VOID CtxInstanceTeardownComplete(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags)
Definition: CtxInit.c:826
NTSTATUS CtxInstanceQueryTeardown(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags)
Definition: CtxInit.c:743
struct _CTX_FILE_CONTEXT * PCTX_FILE_CONTEXT
FORCEINLINE VOID CtxFreeResource(_In_ PERESOURCE Resource)
Definition: CtxProc.h:171
FLT_PREOP_CALLBACK_STATUS CtxPreCleanup(_Inout_ PFLT_CALLBACK_DATA Cbd, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Flt_CompletionContext_Outptr_ PVOID *CompletionContext)
#define IRP_MJ_CREATE
Definition: mspyLog.h:284

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com