Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

filtrctx.c

Go to the documentation of this file.
00001 /*++ 00002 00003 Copyright (c) 1997 Microsoft Corporation 00004 00005 Module Name: 00006 00007 FiltrCtx.c 00008 00009 Abstract: 00010 00011 This module provides three routines that allow filesystem filter drivers 00012 to associate state with FILE_OBJECTs -- for filesystems which support 00013 an extended FSRTL_COMMON_HEADER with FsContext. 00014 00015 These routines depend on fields (FastMutext and FilterContexts) 00016 added at the end of FSRTL_COMMON_HEADER in NT 5.0. 00017 00018 Filesystems should set FSRTL_FLAG2_SUPPORTS_FILTER_CONTEXTS if 00019 these new fields are supported. They must also initialize the mutex 00020 and list head. 00021 00022 Filter drivers must use a common header for the context they wish to 00023 associate with a file object: 00024 00025 FSRTL_FILTER_CONTEXT: 00026 LIST_ENTRY Links; 00027 PVOID OwnerId; 00028 PVOID InstanceId; 00029 00030 The OwnerId is a bit pattern unique to each filter driver 00031 (e.g. the device object). 00032 00033 The InstanceId is used to specify a particular instance of the context 00034 data owned by a filter driver (e.g. the file object). 00035 00036 Author: 00037 00038 Dave Probert [DavePr] 30-May-1997 00039 00040 Revision History: 00041 00042 --*/ 00043 00044 #include "FsRtlP.h" 00045 00046 #define MySearchList(pHdr, Ptr) \ 00047 for ( Ptr = (pHdr)->Flink; Ptr != (pHdr); Ptr = Ptr->Flink ) 00048 00049 // 00050 // Trace level for the module 00051 // 00052 00053 #define Dbg (0x80000000) 00054 00055 00056 NTKERNELAPI 00057 NTSTATUS 00058 FsRtlInsertFilterContext ( 00059 IN PFILE_OBJECT FileObject, 00060 IN PFSRTL_FILTER_CONTEXT Ptr 00061 ) 00062 /*++ 00063 00064 Routine Description: 00065 00066 This routine associates filter driver context with a stream. 00067 00068 Arguments: 00069 00070 FileObject - Specifies the stream of interest. 00071 00072 Ptr - Pointer to the filter-specific context structure. 00073 The common header fields OwnerId and InstanceId should 00074 be filled in by the filter driver before calling. 00075 00076 Return Value: 00077 00078 STATUS_SUCCESS - operation succeeded. 00079 00080 STATUS_INVALID_DEVICE_REQUEST - underlying filesystem does not support 00081 filter contexts. 00082 00083 --*/ 00084 00085 { 00086 PFSRTL_COMMON_FCB_HEADER Header; 00087 00088 ASSERT(FileObject); 00089 ASSERT(FileObject->FsContext); 00090 00091 Header = FileObject->FsContext; 00092 00093 if (! (Header->Flags2 & FSRTL_FLAG2_SUPPORTS_FILTER_CONTEXTS) ) { 00094 return STATUS_INVALID_DEVICE_REQUEST; 00095 } 00096 00097 ExAcquireFastMutex (Header->FastMutex); 00098 00099 InsertHeadList (&Header->FilterContexts, &Ptr->Links); 00100 00101 ExReleaseFastMutex(Header->FastMutex); 00102 return STATUS_SUCCESS; 00103 } 00104 00105 00106 NTKERNELAPI 00107 PFSRTL_FILTER_CONTEXT 00108 FsRtlLookupFilterContextInternal ( 00109 IN PFILE_OBJECT FileObject, 00110 IN PVOID OwnerId OPTIONAL, 00111 IN PVOID InstanceId OPTIONAL 00112 ) 00113 /*++ 00114 00115 Routine Description: 00116 00117 This routine lookups filter driver context associated with a stream. 00118 00119 The macro FsRtlLookupFilterContext should be used instead of calling 00120 this routine directly. The macro optimizes for the common case 00121 of an empty list. 00122 00123 Arguments: 00124 00125 FileObject - Specifies the stream of interest. 00126 00127 OwnerId - Used to identify context information belonging to a particular 00128 filter driver. 00129 00130 InstanceId - Used to search for a particular instance of a filter driver 00131 context. If not provided, any of the contexts owned by the filter 00132 driver is returned. 00133 00134 If neither the OwnerId nor the InstanceId is provided, any associated 00135 filter context will be returned. 00136 00137 Return Value: 00138 00139 A pointer to the filter context, or NULL if no match found. 00140 00141 --*/ 00142 00143 { 00144 PFSRTL_COMMON_FCB_HEADER Header; 00145 PFSRTL_FILTER_CONTEXT Ctx, RtnCtx; 00146 PLIST_ENTRY List; 00147 00148 ASSERT(FileObject); 00149 ASSERT(FileObject->FsContext); 00150 00151 Header = FileObject->FsContext; 00152 00153 ASSERT (Header->Flags2 & FSRTL_FLAG2_SUPPORTS_FILTER_CONTEXTS); 00154 00155 ExAcquireFastMutex (Header->FastMutex); 00156 RtnCtx = NULL; 00157 00158 // Use different loops depending on whether we are comparing both Ids or not. 00159 if ( ARGUMENT_PRESENT(InstanceId) ) { 00160 00161 MySearchList (&Header->FilterContexts, List) { 00162 Ctx = CONTAINING_RECORD (List, FSRTL_FILTER_CONTEXT, Links); 00163 if (Ctx->OwnerId == OwnerId && Ctx->InstanceId == InstanceId) { 00164 RtnCtx = Ctx; 00165 break; 00166 } 00167 } 00168 } else if ( ARGUMENT_PRESENT(OwnerId) ) { 00169 00170 MySearchList (&Header->FilterContexts, List) { 00171 Ctx = CONTAINING_RECORD (List, FSRTL_FILTER_CONTEXT, Links); 00172 if (Ctx->OwnerId == OwnerId) { 00173 RtnCtx = Ctx; 00174 break; 00175 } 00176 } 00177 } else if (!IsListEmpty(&Header->FilterContexts)) { 00178 00179 RtnCtx = (PFSRTL_FILTER_CONTEXT) Header->FilterContexts.Flink; 00180 } 00181 00182 ExReleaseFastMutex(Header->FastMutex); 00183 return RtnCtx; 00184 } 00185 00186 00187 NTKERNELAPI 00188 PFSRTL_FILTER_CONTEXT 00189 FsRtlRemoveFilterContext ( 00190 IN PFILE_OBJECT FileObject, 00191 IN PVOID OwnerId OPTIONAL, 00192 IN PVOID InstanceId OPTIONAL 00193 ) 00194 /*++ 00195 00196 Routine Description: 00197 00198 This routine deletes filter driver context associated with a stream. 00199 00200 Filter drivers must explicitly remove all context they associate with 00201 a stream (otherwise the underlying filesystem will BugCheck at close). 00202 00203 FsRtlRemoveFilterContext functions identically to FsRtlLookupFilterContext, 00204 except that the returned context has been removed from the list. 00205 00206 Arguments: 00207 00208 FileObject - Specifies the stream of interest. 00209 00210 OwnerId - Used to identify context information belonging to a particular 00211 filter driver. 00212 00213 InstanceId - Used to search for a particular instance of a filter driver 00214 context. If not provided, any of the contexts owned by the filter 00215 driver is removed and returned. 00216 00217 If neither the OwnerId nor the InstanceId is provided, any associated 00218 filter context will be removed and returned. 00219 00220 Return Value: 00221 00222 A pointer to the filter context, or NULL if no match found. 00223 00224 --*/ 00225 00226 { 00227 PFSRTL_COMMON_FCB_HEADER Header; 00228 PFSRTL_FILTER_CONTEXT Ctx, RtnCtx; 00229 PLIST_ENTRY List; 00230 00231 ASSERT(FileObject); 00232 00233 Header = FileObject->FsContext; 00234 00235 if ( !Header || !(Header->Flags2 & FSRTL_FLAG2_SUPPORTS_FILTER_CONTEXTS) ) { 00236 return NULL; 00237 } 00238 00239 ExAcquireFastMutex (Header->FastMutex); 00240 RtnCtx = NULL; 00241 00242 // Use different loops depending on whether we are comparing both Ids or not. 00243 if ( ARGUMENT_PRESENT(InstanceId) ) { 00244 00245 MySearchList (&Header->FilterContexts, List) { 00246 Ctx = CONTAINING_RECORD (List, FSRTL_FILTER_CONTEXT, Links); 00247 if (Ctx->OwnerId == OwnerId && Ctx->InstanceId == InstanceId) { 00248 RtnCtx = Ctx; 00249 break; 00250 } 00251 } 00252 } else if ( ARGUMENT_PRESENT(OwnerId) ) { 00253 00254 MySearchList (&Header->FilterContexts, List) { 00255 Ctx = CONTAINING_RECORD (List, FSRTL_FILTER_CONTEXT, Links); 00256 if (Ctx->OwnerId == OwnerId) { 00257 RtnCtx = Ctx; 00258 break; 00259 } 00260 } 00261 } else if (!IsListEmpty(&Header->FilterContexts)) { 00262 00263 RtnCtx = (PFSRTL_FILTER_CONTEXT) Header->FilterContexts.Flink; 00264 } 00265 00266 if (RtnCtx) { 00267 RemoveEntryList(&RtnCtx->Links); // remove the matched entry 00268 } 00269 ExReleaseFastMutex(Header->FastMutex); 00270 return RtnCtx; 00271 } 00272 00273 00274 NTKERNELAPI 00275 VOID 00276 FsRtlTeardownFilterContexts ( 00277 IN PLIST_ENTRY FilterContexts 00278 ) 00279 /*++ 00280 00281 Routine Description: 00282 00283 This routine is called by filesystems to free the filter contexts 00284 associated with an FSRTL_COMMON_FCB_HEADER by calling the FreeCallback 00285 routine for each FilterContext. 00286 00287 It is assumed that we do not need to acquire the FastMutex. 00288 00289 Arguments: 00290 00291 FilterContexts - the address of the FilterContexts field within 00292 the FSRTL_COMMON_FCB_HEADER of the structure being torn down 00293 by the filesystem. 00294 00295 Return Value: 00296 00297 None. 00298 00299 --*/ 00300 00301 { 00302 PFSRTL_FILTER_CONTEXT ctx; 00303 PLIST_ENTRY ptr; 00304 00305 ptr = FilterContexts->Flink; 00306 00307 while ( ptr != FilterContexts ) { 00308 ctx = CONTAINING_RECORD (ptr, FSRTL_FILTER_CONTEXT, Links); 00309 ptr = ptr->Flink; 00310 (*ctx->FreeCallback)(ctx); 00311 } 00312 00313 InitializeListHead( FilterContexts ); 00314 return; 00315 } 00316

Generated on Sat May 15 19:40:01 2004 for test by doxygen 1.3.7