Opportunistic locks ,or oplock is  a client caching mechanism that allows SMB/SMB2 clients to dynamically decide the client-side buffering strategy so the network traffic can be minimized.   For example, a client can buffer  data for writing  locally to reduce network packets  if the client is notified there are no other processes  accessing data.   The same buffering can be done on reading , where data from a remote file is read ahead to reduce the round trips between the client and the server if the client knows  that there are no other processes writing data to the file.   

     In SMB2.1, which is the new SMB2 dialect implemented in Windows 7,  Lease is introduced as a new type of client caching mechanism.  It  offers more flexibility for controlling client caching and thus results in significant performance improvement in high latency network[2].

In this blog, we will analyze the network behavior of  oplock  in SMB/SMB2  and leasing  in SMB2.1.     We will compare lease and oplock and discuss the advantages of lease over oplock in certain scenarios.    

Oplocks  in SMB/SMB2

 The following types of oplocks are available in SMB/SMB2. 

·         Batch Oplock

This oplock allows a client to perform batch operations to repeatedly open, read/write and close the same file  without actually closing the file on the server.  This oplock  is exclusive and cannot be held by two clients at the same time.   It provides read, write and handle caching .

 

·         Level II Oplock

This oplock allows multiple clients for reading, but no writing.   This oplock can be shared by multiple clients at the same time.  It provides read caching only.

 

·         Exclusive Oplock   (only in SMB2)

This oplock allows a client to have exclusive access to do arbitrary buffering.  It  provides read caching and writes caching.

 

    After an oplock is granted to a client, if the server receives another operation that  conflicts with the current opLock held by the client, an oplock break notification will be sent to the client.  The client should send an  acknowledgement for the oplock break to the server before the server can  proceed.  For example, if a client holds a Level II oplock and another client opens the same file on the server for writing, the level II oplock must be broken and the client caching has to be disabled.

 

Oplock network traffic:  

 

·         A client requests  an oplock  from  a server in

o    Flag field of  SMB_COM_NT_CREATE_ANDX (SMB) 

                           or

o   RequestedOplockLevel field in SMB2_CREATE(SMB2)

 

·         The server grants  oplock to the client in

o   opLockLevel of  SMB_COM_NT_CREATE_ANDX response(SMB)

                         or

o   opLockLevel of  SMB2_CREATE  response(SMB2)

 

·         The server sends an opLock break  to the client, if the condition for oplock break is met.

There is a difference between SMB and SMB2 for how an oplock break is sent to a client

o   For SMB, the server sends an oplock break to the client using SMB_LOCK_ANDX.     LockType  field is the  level that  the oplock is broken into.   

o   For SMB2, there is a specific command (SMB2_OPLOCK_BREAK,0x12 ) used to send the oplock break notification to the client. 

 

·         The client sends the  acknowledgment  for the opLock break to the  server

 

 SMB and SMB2 clients use different commands to acknowledge oplock breaks.

 

o   For SMB, the client  sends an oplock break acknowledgement to the server  using SMB_LOCK_ANDX.    LockType is the type of oplock client holds.

o   For SMB2, the client sends SMB2_OPLOCK_BREAK_ACKNOWLEDGEMENT to the server.  OplockType is the type of oplock client holds.

  

Lease in SMB 2.1  (Windows 7)

         In Windows 7, a new caching mechanism named Lease is introduced.    It shares the same purpose as an oplock, which allows clients to adjust their buffering policy to increase performance and to reduce network use.   Actually, the newly added lease types correspond to the new oplock types introduced in Windows 7 file system [1].   SMB2.1 just gives it a different name to distinguish it from the existing oplock functionality.

         The primary types of leases available are:

·         Read-caching lease:  allows  caching  reads and can be shared by multiple clients.

·         Write-caching lease: allows caching writes and  is exclusive to only one client.

·         Handle-caching lease: allows caching  handles and can be shared by multiple clients.

         It is very important to remember that a lease can be a combination of one or more of the leases above.   The valid leases Windows 7 can grant are 

·         R (READ_CACHING)

·         RW(READ_CACHING|WRITE_CACHING)

·         RH(READ_CACHING|HANDLE_CACHING)

·         RWH(READ_CACHING|WRITE_CACHING|HANDLE_CACHING)

 

SMB2.1  lease network traffic:  

·         A client requests  a  lease  from a server

o    Set RequestedOplockLevel  field  to SMB2_OPLOCK_LEVEL_LEASE(0xFF)  in SMB2_CREATE request .

o   Use SMB2_CREATE_REQUEST_LEASE create context in SMB2_CREATE request to indicate the type of lease requested.

 

·          The server grants  a lease  to the client 

o   Set opLockLevel   field of  SMB2_CREATE  response to  OPLOCK_LEVEL_LEASE to indicate that a lease is granted.

o   Use SMB2_CREATE_RESPONSE_LEASE create context  in SMB2_CREATE response to indicate the type of lease granted.  

 

·         The  server sends  lease breaks to the client, if a lease break condition is met.

o   SMB2_LEASE_BREAK_NOTIFICATION is sent  to client. It contains the current lease state and the new lease state the client should change to.    This is a different structure than opLock.

 

·         The client sends the  acknowledgement  for the lease break to the  server

o   SMB2_LEASE_BREAK_ACKNOWLEDGEMENT is sent to server.  It indicates the lease state of client currently holds.    This is a different structure than opLock.

 

Comparison between Lease and Oplock

·         Lease in SMB2.1 is based on the new types of oplocks introduced in Window 7 or later file systems.    The purposes for Lease and Oplock are the same.  But Lease provides greater flexibility for clients to request caching mechanisms.    Some new caching levels are  added in leasing featuer in Windows 7.        

 

·          One of the  levels added is RH lease, which allows read caching and handle caching so multiple clients can  hold on to cached data even after an application closes the handle.   Compared with Level II oplocks, which have to be broken between closing and opening handles, this new lease level offers a big improvement for complex I/O intensive applications. 

 

·         Another enhancement of lease over oplock is that SMB2.1 in Windows 7 allows full caching when multiple handles are opened by the same client.   This is also a significant improvement over oplock semantics when any additional handle opened by even the same client will break the existing batch oplock and thus disable the client caching.   The following is the comparison between oplock and lease for the case of multiple handles opened. 

                                      Oplock with multiple handles from the same client

                      Client                                                Network                     Server       

CreateFile with READ and WRITE                            -->                 First handle on the file                                                                                  

                                                                                 <--                  Batch oplock granted            

    ReadFile                                                                  - ->                 

                                                                                    <--                  read data from file on server

    ReadFile….  (From cached data)               No network packets

    WriteFile…  (From cached data)               No network packets

    CreatFile with READ                                             -->                   Second handle on the file           

     No more data caching allowed                           <--                   Batch oplock broken      

    ReadFile                                                                 -->                  File I/O on the server                 

   WriteFile                                                                -->                   File I/O on the server

 

                                      Lease  with multiple handles from the same client

                      Client                                                Network                     Server       

    CreateFile with READ and WRITE                            -->                  First handle on the file 

                                                                                     <--                   Lease  granted            

    ReadFile                                                                   -->                 

                                                                                     <--                   read data from file on server

    ReadFile….  (From cached data)              No network packets  

    WriteFile…  (From cached data)              No network packets

    CreatFile with READ                                                 -->                   Lease is not broken on the second handle          

    ReadFile       (From cached data)             No network packets                                                  

   WriteFile        (From cached data)            No network packets                                               

      From two scenarios above, we can see that oplock doesn’t allow data caching if there are multiple handles opened by the same client; on the other hand, the leasing feature in Windows 7 allows  full data caching on multiple handles as long as they are opened by the same client.    This client caching enhancement can provide a further performance boost, especially on high latency network.

For more information about client caching improvements in Windows 7, you can review the excellent PDC presentation on the following link [2]  and the MS-SMB and MS-SMB2 documents[3][4]. 

   

Reference Link:

1.       [MSDN-FSBO] Microsoft Corporation, "File System Behavior in the Microsoft Windows Environment", June 2008, http://download.microsoft.com/download/4/3/8/43889780-8d45-4b2e-9d3a-c696a890309f/File%20System%20Behavior%20Overview.pdf .

2.       Windows 7: Optimizing Applications for Remote File Services over the WAN http://channel9.msdn.com/pdc2008/ES23/

3.       [MS-SMB2]: Server Message Block (SMB) Version 2 Protocol Specification http://msdn.microsoft.com/en-us/library/cc246482(PROT.13).aspx

4.       [MS-SMB]: Server Message Block (SMB) Protocol Specification http://msdn.microsoft.com/en-us/library/cc246231(PROT.13).aspx