SMB v2 Client Cache e ritardo in visualizzazione file nuovi

Quando i clinet utilizzano SMB v2 per accedere alle share può verificarsi il problema che creando un file nuovo questo non sia immediatamente visibile neppure dopo l’esecuzione di un refresh, ma occorrono circa 10 secondi affinché diventi visibile. Il motivo è dovuto al fatto che nell’implementazione dell’SMB (Server Message Block) 2.0 introdotto in Windows Vista e Windows Server 2008 il componente SMB Redirector che implemente la parte client della funzionalità file-and-print sharing utilizza un meccanismo di caching per aumentare l’efficienza come descritto in SMB2 Client Redirector Caches Explained:

“With the release of SMB 2.0 in Windows Vista® and Windows Server 2008, three file metadata caches were implemented to speed up the return of the most recently accessed file and directory information. These caches also reduce the number of interactions a client requires with a SMB server for common file browsing operations. This has value in a scenario such as a client browsing a network file directory while connected via a low bandwidth or high latency connection.”

E’ possibile gestire il comportamento del caching impostando, se necessario, tre time out mediante modifica al registro di sistema, sempre come riportato in SMB2 Client Redirector Caches Explained:

“For common network file browsing scenarios, the default values are sufficient and should not be altered. Changing these cache timeout values can have significant performance implications to many network file scenarios. As each of these caches is designed to reduce the number of SMB server requests, they are important not only in client response time evaluation, but also in overall SMB server scalability and performance.”

“If changes are deemed necessary to these default values, they may be altered by creating these client registry keys and changing to the appropriate setting. Each cache is independent and controls specific information. It is not necessary to change all of the caches if a change to any one of the caches is necessary, as they operate independently and control different redirector optimizations.The default values were selected for the broadest range of SMB file access scenarios. However these default settings may not be appropriate for all network application usage.”

Nel caso si verifichi il problema della mancata visualizzazione dei file appena creati è possibile provare a disabilitare (o ridurre) la cache relativa all’enumerazione delle directory gestita dalla chiave di registro DirectoryCacheLifetime: () per defaulttale cache è inmpostata a10 secondi

“This is a cache of recent directory enumerations performed by the client. Subsequent enumeration requests made by client applications as well as metadata queries for files in the directory can be satisfied from the cache. The client also uses the directory cache to determine the presence or absence of a file in the directory and uses that information to prevent clients from repeatedly attempting to open files which are known not to exist on the server. This cache is likely to affect distributed applications running on multiple computers accessing a set of files on a server – where the applications use an out of band mechanism to signal each other about modification/addition/deletion of files on the server.”

Per disabilitare tale cache creare, se non esistente, il valore DirectoryCacheLifetime di tipo DWORD e impostarlo al valore 0 nella chiave di registro HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Lanmanworkstation\Parameters quindi ravviare il computer.

L’impostazione a 0 del valore DirectoryCacheLifetime può essere necessario per risolvere un’issue in Windows 7 descritto nel post Mount Point Folder May Disappear in Share Folder in Windows 7 pubblicato sul TechNet Forum Windows 7 Support Team:

SYMPTOM
You have a share folder which contains volume mount points in Windows Server 2008 or Windows Server 2008 R2. Sometimes the mount point folders appear as “hidden” when you navigate through the content of the folders and clicks “back” in Windows Explorer in Windows 7.

CAUSE
This is a known issue of SMBv2.

Resolution
As a Workaround, you can create the following Registry key on the client machine:
Navigate to the following key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters
Create DWORD DirectoryCacheLifetime and set it to 0

Per ulteriori informazioni si veda anche Performance tuning for file servers in cui a riguardo di tale chiave di registro viene riportato quanto segue:

DirectoryCacheLifetime

HKLM\System\CurrentControlSet\Services\LanmanWorkstation\Parameters\DirectoryCacheLifetime

Applies to Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Vista, Windows Server 2016, Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2, and Windows Server 2008

The default is 10 seconds. This is the directory cache timeout.

Note

This parameter controls caching of directory metadata in the absence of directory leases.