[Samba] File Locking for dropbox

Dana Lacoste dana.lacoste at peregrine.com
Fri Jun 20 20:59:19 GMT 2003

OK, this may sound like a FAQ but all the FAQ sheets I see
don't really cover it : they come close, but not close enough.

The case :

Samba server (2.2.8a) on a stock 2.4.20 linux kernel (glibc 2.2.3)
Operating as a 'dropbox' : files are dropped into \\server\incoming
by various (windows) clients, then the server processes them.

The problem :

How to tell when a 'new' file detected in the dropbox is available
Currently we do this :
1 - See a new file
2 - Attempt a write lock on the file (in Delphi, but could be in C)
3 - Process file

This doesn't work, because samba doesn't seem to write files in a
way that blocks the write access of the file processor.
i.e. Step 2 _always_ succeeds, even if the file hasn't done writing yet.

So when a user drops 5000 files into the dropbox, the Delphi process
gets about 4900 'rejected : corrupt files' because it tried to access
the file while it was still being written.

Notes :
- When using 100M ethernet vs. 10M ethernet, the problem goes away.
  This makes sense : the faster you write the file (files average
  about 800K in size) the less likely the file processor will try
  to read the file as you're writing it.
- smb.conf file is below.  The 'strict' and 'level2' lines were
  added to try to fix the problem, but they didn't work well enough :
  the ratio of rejected files dropped, but it's still happening.

Is there a way to force samba, when writing a file, to lock it such
that any other UNIX/server side process can't access the file because
it's locked?  If not, do you have any other suggestions?
(can't use atime, FS is mounted noatime for performance reasons.  Can't
use atime/ctime, cuz they're the atime/ctime of the original file on
the windows side.  I'm kinda stumped myself :)

Dana Lacoste
Ottawa, Canada

More information about the samba mailing list