Opportunistic locks, or oplocks, are mechanisms designed to allow clients to dynamically alter their buffering strategy for a given file or stream in a consistent manner to increase performance and reduce network use. The network performance for remote file operations may be increased if a client can locally buffer file data, which reduces or eliminates the need to send and receive network packets. For example, a client may not have to write information into a file on a remote server if the client knows that no other process is accessing the data. Likewise, the client may buffer read-ahead data from the remote file if the client knows that no other process is writing data to the remote file. Oplocks can also be used by applications to transparently access files in the background.
File systems like NTFS support multiple data streams per file. Oplocks are “stream” handle centric, this means the operations apply to the given open stream of a file and in general operations on one stream do not affect oplocks on a different stream. There are exceptions to this which will be explicitly pointed out. For file systems that don’t support alternate data streams think of “file” when this document refers to “stream”.
There are four different types of oplocks:
- A Level 2 (or shared) oplock indicates that there are multiple readers of a stream and no writers. This supports read caching.
- A Level 1 (or exclusive) oplock allows a client to open a stream for exclusive access and allows the client to perform arbitrary buffering. This supports read and write caching
- A Batch oplock (also exclusive) allows a client to keep a stream open on the server even though the local accessor on the client machine has closed the stream. This supports read, write and handle caching.
- A Filter Oplock (also exclusive) allows applications and file system filters which open and read stream data a way to “back out” when other applications/clients try to access the same stream. Filter oplock support was added in Windows 2000. This supports read and write caching.
The core functionality of the oplock package is implemented in the kernel. File systems call into this package at key points to implement the oplock functionality in their file system. This document describes how NTFS interoperates with the kernel oplock package. Other file systems function basically the same though there might be minor differences.
Oplocks are stream handle centric. This means an oplock is granted for a given open of a stream. If an operation is received that is not compatible with the currently granted oplock then that oplock is broken even if it is the same process or thread doing the incompatible operation. For example if a process opens a stream and is granted an exclusive oplock and that same process then opens the same stream again, the exclusive oplock will immediately be broken.