POSIX vs flock on Network File Systems in Linux

The Linux operating system manages file locking across networked file systems by translating local kernel primitives—specifically POSIX record locks (fcntl/lockf) and BSD-style locks (flock)—into network protocol operations. While POSIX locks natively map to standard network lock protocols like NFS and SMB to provide cross-client synchronization, flock historically functioned only on local filesystems. Modern Linux kernels reconcile these differences by emulating flock as whole-file POSIX locks over network mounts, a mechanism that unifies distributed locking but introduces distinct semantic caveats and configuration dependencies.

Fundamental Divergence in Linux Locking Semantics

Locally, Linux handles POSIX and flock locks using fundamentally different structures in the Virtual Filesystem (VFS) layer:

Because network protocols like NFS were designed around POSIX-like, byte-range semantics, native support for BSD-style whole-file descriptor locks does not exist at the wire-protocol level.

POSIX Locks Over the Network

When an application calls fcntl() on a network-mounted file, the VFS invokes the filesystem driver’s .lock() hook rather than using the local VFS lock list:

The Network Evolution of flock

Historically, flock calls on network mounts were purely client-local; a lock taken on Client A was invisible to Client B. Modern Linux kernels (since version 2.6.12) resolve this by delegating flock to the network file system:

  1. Protocol Emulation: The network filesystem driver implements a .flock() hook that translates an incoming flock() call into a whole-file POSIX byte-range lock (spanning byte 0 to the maximum possible offset).
  2. Transmission: The lock is transmitted over the wire as an ordinary POSIX lock using NLM, NFSv4, or SMB.
  3. Semantic Blurring: Locally, Linux treats flock and POSIX locks as independent namespaces that do not conflict with one another. However, on a network mount where flock is emulated as a POSIX lock, a local flock will conflict directly with a POSIX lock on the same node or across the cluster, altering local application behavior.

Mount Options and Behavioral Control

Because translating flock to POSIX network locks can cause performance overhead or break legacy applications, Linux provides mount options to alter locking behavior:

State Recovery and Partition Handling

Distributed locking requires strict synchronization between the client VFS and remote state handlers. On network partitions or server crashes: