flock possibly missing in SMB3 POSIX extensions?

Jeremy Allison jra at samba.org
Fri Feb 26 18:40:37 UTC 2021

On Fri, Feb 26, 2021 at 10:17:02AM -0800, Jeremy Allison via samba-technical wrote:
>On Fri, Feb 26, 2021 at 05:17:51PM +0100, Aurélien Aptel via samba-technical wrote:
>>While debugging cifs.ko customer issues we found that flock(2) seems to
>>be impossible to properly emulate using SMB.
>>On first sight it looks like they could be emulated with SMB locks but
>>the main difference is that FLOCKs are completely advisory, the application
>>is free to ignore them and sucessfully do read/write, whereas with SMB
>>locks WRITE/READ will fail on locked areas.
>>The flock syscall is avaible on most unixes so it would make sense to
>>support it I think. Maybe via a special flag for LOCK.
>>Given the number of options to control locking behaviour in Samba and
>>given how NFS doesn't support it properly either maybe there are
>>problems I'm not seeing.
>Yes, flock has historically not been part of UNIX
>extensions. I think you *could* emulate it by converting
>them into UNIX extension fcntl locks over the whole
>Indeed, from man 2 flock:
>       Since  Linux 2.6.12, NFS clients support flock() locks by emulating them as fcntl(2) byte-range locks
>       on the entire file.  This means that fcntl(2) and flock() locks do interact  with  one  another  over
>       NFS.  It also means that in order to place an exclusive lock, the file must be opened for writing.

So the plan would be once a file is opened with a POSIX
context, emulate flock(2) by getting a byte-range lock
on the whole file. For a POSIX context this is an
advisory lock.

This matches what NFS does, and applications using
flock are usually not also using fcntl locks.

I'd rather not add "special" extra flags to the
protocol that need extra logic to implement. The
locking part is complex enough in SMB1/2/3, and
as NFS shows it's possible to emulate using advisory
byte-range locks anyway.

Of course this doesn't help SMB2+ without UNIX
extensions, but there are many POSIX things that
won't work there anyway :-).

More information about the samba-technical mailing list