Hi,
In order to keep coherency in my filter w/ writes, I am utilizing a shadow file. When there are no longer any references to the file (including from the cache manager), meaning that all outstanding writes have been flushed and redirected to my shadow file, I would like to replace the original file with the shadow file to put things back in a consistent state.
I have been reading over the MSDN documentation for IRP_MJ_CLEANUP, IRP_MJ_CLOSE, etc. and I am trying to understand how to best accomplish this. I have a few questions:
-
From my understanding, IRP_MJ_CLOSE tracks when a particular FILE_OBJECT no longer has any references and is being destroyed by the kernel. However, this does NOT mean that there is no longer any reference to the underlying data, since multiple FILE_OBJECTs can point to the same cache coherency structures. Correct? (This is what I have seen when debugging IRP_MJ_CLOSE).
-
Assuming 1) is correct, and assuming that the kernel might potentially keep a referenced FILE_OBJECT to data in cache for a very long time, what is the best way to accomplish what I am trying to do?
Here is what I am thinking:
Using a stream context, I can track how many open handles there on a particular stream via IRP_MJ_CREATE. When the number of open user handles reaches zero (tracked via IRP_MJ_CLEANUP) for a particular stream, I can force the cache manager to flush the file data (there by redirecting it to my shadow file). At this point I can proceed with my post-processing.
I’m assuming there are lots of gotchas w/ lock handling which I will figure out, but does this sound about right? Any other piece of advice?
Thanks,
Matt