Synchronization on setting Context in IRP_MJ_CREATE

Hi

I am developing minifilter which will use shadow file object. In pre operation callback for IRP_MJ_CREATE I want to open shadow file object and set stream context for it if context is not set already. After successful call of FltCreateFileObjectEx I am going to get context by FltGetStreamContext(). If context is not set then it will be allocated and then set by FltSetStreamContext. There can be a few create requests at same time for the same file.

Can there be situation that more than one request will create and set context? Do I have to synchronize this?

Thanks

In the call to FltSetStreamContext() you can set a flag to either keep
or replace the context if it already exists, see the documentation for
this call. Therefore you don’t need to synchronize this, FM will do it
for you.

Pete

------ Original Message ------
From: xxxxx@gmail.com
To: “Windows File Systems Devs Interest List”
Sent: 4/7/2015 9:50:23 AM
Subject: [ntfsd] Synchronization on setting Context in IRP_MJ_CREATE

>Hi
>
>I am developing minifilter which will use shadow file object. In pre
>operation callback for IRP_MJ_CREATE I want to open shadow file object
>and set stream context for it if context is not set already. After
>successful call of FltCreateFileObjectEx I am going to get context by
>FltGetStreamContext(). If context is not set then it will be allocated
>and then set by FltSetStreamContext. There can be a few create requests
>at same time for the same file.
>
>Can there be situation that more than one request will create and set
>context? Do I have to synchronize this?
>
>Thanks
>
>—
>NTFSD is sponsored by OSR
>
>OSR is hiring!! Info at http://www.osr.com/careers
>
>For our schedule of debugging and file system seminars visit:
>http://www.osr.com/seminars
>
>To unsubscribe, visit the List Server section of OSR Online at
>http://www.osronline.com/page.cfm?name=ListServer

> Can there be situation that more than one request will create and set

context?

yes. You can tell Filter Manager what you want it to do in such a
circumstance (either replace or fail to add).

Do I have to synchronize this?

No more than is involved in behaving correctly with respect to filter
manager’s result from the FltSetStreamContext call.

/Rod

The file object isn’t even visible outside the current call context for IRP_MJ_CREATE, so no synchronization is required.

Note that there will *never* be a context set in pre-create. You should check in post-create (though you should allocate in pre-create as it makes dealing with out of memory errors much easier).

Tony
OSR

Thanks, but

  1. Most probably, if I use the single call of FltSetStreamContext with corresponding flag then synchronization is not needed. But I do not want to allocate context in advance and then free it if it is set already. I want at first to make try to get context and if it is not set then allocate and set it. Therefore there may be call of FltGetStreamContext and then FltAllocateContext and FltSetStreamContext. And I do not know whether in this situation synchronization is needed or not.

  2. I am not going to set context on file object which IRP_MJ_CREATE has to create. In pre operation callback I want to create additional file object with another name by FltCreateFileEx and set stream context on it. As file object is created then it is possible to set context. Of course, it will not be visible outside of current call, but there can be more than one request at same time which will create corresponding additional file objects and will try to allocate and set contexts.

So if I understand you correctly, you’d like to set the stream context on a
FILE_OBJECT that your filter opens on the FS. In this case you’re basically
the same as any other filter that opens a file and sets a stream context
(as in, there’s nothing specific to SFO that I can think of) so the
strategy you’ve outlined is great( call FltGetStreamContext, if none is set
call FltAllocateContext and then call FltSetStreamContext). However, yes,
it is possible you’ll race with other opens (unless you do something like
generating a GUID and use that as a file name or some other trick that
prevents two opens to the same file). In that case your call to
FltSetStreamContext will do whatever you tell it with the flag (either keep
the context it finds or replace it). You do not need additional
synchronization here, FltMgr handles everything. This is that the Ctx
sample does, see CtxFindOrCreateStreamContext() if you’d like to see an
example.

Please note that you might ending up allocating the context and then
discarding it if there’s a race, but that should be a rather rare event.
You could add synchronization to avoid this but it will likely be very
heavy (like, serialize all CREATEs because there’s not much else you can
use… the file path won’t be enough).

Hope this helps.

Thanks,
Alex.

On Tue, Apr 7, 2015 at 12:09 PM, wrote:

> Thanks, but
> 1. Most probably, if I use the single call of FltSetStreamContext with
> corresponding flag then synchronization is not needed. But I do not want to
> allocate context in advance and then free it if it is set already. I want
> at first to make try to get context and if it is not set then allocate and
> set it. Therefore there may be call of FltGetStreamContext and then
> FltAllocateContext and FltSetStreamContext. And I do not know whether in
> this situation synchronization is needed or not.
>
> 2. I am not going to set context on file object which IRP_MJ_CREATE has to
> create. In pre operation callback I want to create additional file object
> with another name by FltCreateFileEx and set stream context on it. As file
> object is created then it is possible to set context. Of course, it will
> not be visible outside of current call, but there can be more than one
> request at same time which will create corresponding additional file
> objects and will try to allocate and set contexts.
>
> —
> NTFSD is sponsored by OSR
>
> OSR is hiring!! Info at http://www.osr.com/careers
>
> For our schedule of debugging and file system seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>