Configure opportunistic locking for files on network shares

When accessing a file on the network, then the file operations are converted to network requests and replies. To minimize the amount of network request and replies it is best to read/write in large blocks (64K), but many applications only read/write a single byte at the time, thus generating a large amount of network traffic.

Opportunistic locking (OpsLocks) is a way to help such applications, by implementing a read ahead and a lazy write cache. The client requests a read or write lock on the file, when they are granted the lock, then the client caches the file locally so sequent read/write operations from the application only affects the cache. If another client requests a read or write lock on the file, and it conflicts with an existing opportunistic lock, then the opportunistic lock is broken (Caches are flushed), and instead the access for all clients will revert to network requests/replies.

Note if working with files over a non-reliable connection or non-reliable fileserver, then opportunistic locking will most likely cause the client application to loose any changes made to the file without closing the file.

To configure the use of opportunistic locking for a Windows NT4 client:

[HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet \Services \LanmanWorkstation \Parameters]
UseOpportunisticLocking = 1 (Default = 1)

To configure the use of opportunistic locking for a Windows 2000/XP client:

[HKEY_LOCAL_MACHINE \System \CurrentControlSet \Services \MrXSmb \Parameters]
OplocksDisabled = 0 (Default = 0)

To configure the use of opportunistic locking for the server:

[HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet \Services \LanmanServer \Parameters]
EnableOplocks = 1 (Default = 1)

To configure the use of byte-range locking that allows a client to only lock portions of a read-only file, but at the cost of updating the lock continuously (WinXP/Win2k3):

[HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet \Services \LanmanWorkstation \Parameters]
DisableByteRangeLockingOnReadOnlyFiles = 1 (Default = 0)

Note if using Windows NT 4.0 or Windows 2000 SP2 (Or previous), then one should consider disabling opportunistic locking as it doesn’t work properly.

Note Windows Vista can only disable opportunistic locking for traditional SMB (SMB1), but not for SMB2. If disabling opportunistic locking, then the offline files feature will fail.

More Info MS KB129202
More Info MS KB224992
More Info MS KB296264
More Info MS KB306981
More Info MS KB818396
More Info MSDN: Opportunistic Locks