Search code examples
c#linuxfilecachingsamba

Reading file from linux samba share and caching


There are 2 similar setups:

  1. Ubuntu 16.04 server with a shared folder \\linux\data and file test.txt in it (Samba 4.3.9-Ubuntu)
  2. Windows Server 2012 server with a shared folder \\win\data and file test.txt in it

There is another PC (Win 7) and a C# test program that reads the file content from either of setups described above in a loop like so:

  1. read file, remember data size
  2. wait 500 ms
  3. read file again, if size changed - print out the size along with few bytes of content from the end of that file, update stored size
  4. goto 2.

Now when I run the test with setup #1 (win share) - if i open the file on the server, modify the content and save - I can immediately see the reaction in test program - first loop iteration after file modification brings me new file size and new data.

But when I run same test with setup #2 (ubuntu share) - I don't see the modified size or content in the test program until some time passes (and that time varies depending on what method I use to read file content. With FileStream it is around 10 seconds, with File.ReadAllBytes it is anywhere from 30 seconds to 10 minutes).

One interesting note with setup #2 - if I modify the file in Ubuntu, then go to test PC (where the test program is now looping, trying to get updated file content) and open the shared file with notepad - test program immediately gets the updated content.

To me it looks like client-side caching on the test machine. Only thing I can think of is that Win Server 2012 uses SMB3 and Ubuntu - SMB2_10 and this somehow forces the client (test reader part) to behave in different way.

I'm no linux expert and as far as I understand there is no way to enable SMB3 protocol in Ubuntu (it can work as SMB3 client but not server)

So the question is how to force client (Win 7) to read with no cache regardless of the server type and protocol version, or, if it is not possible - how to mimic from C# code opening file with notepad, that obviously gives it a kick and forces all readers to get updated content.


Solution

  • Figures out disabling opportunistic locks (oplocks) in linux samba configuration (smb.conf) helps.

    With OpLocks disabled test program on client machine gets updated content with no caching.