NFS4 Lock and LockU

Parent Previous Next

NFS4: Lock and LockU


1. Background: What “file locking” means

When multiple processes (or NFS clients) access the same file, a file lock is used to coordinate access — to prevent simultaneous writes or to implement safe sharing.

But how a system enforces those locks can vary. That’s where mandatory vs. advisory locking come in.


2. Advisory vs. Mandatory Locks — Core Difference

| Type               | Who enforces it?             | Effect                                                                | Default in NFS                  |

| ------------------ | ---------------------------- | --------------------------------------------------------------------- | ------------------------------- |

| **Advisory lock**  | User space / applications    | Cooperative — only programs that *check* the lock will obey it        | ✅ **Yes (default)**             |

| **Mandatory lock** | Kernel / OS filesystem layer | Enforced — the OS will block read/write syscalls from other processes | ❌ **No (not supported by NFS)** |



3. Advisory Lock — (Used by NFSv4)

In advisory locking, the kernel doesn’t force other processes or clients to obey the lock — it simply records the lock state.

Applications use system calls like fcntl() or lockf() to request or check locks.


If another process or client also respects the locking mechanism, they will wait or get a “lock denied” error.

If an application ignores locks, the OS won’t stop it — it can still read/write the file.


✅ NFSv4 LOCK / LOCKU operations are purely advisory.


That means:

The NFS server tracks locks and prevents other locking-aware NFS clients from acquiring overlapping locks,

but it does not block direct file access (e.g., local I/O on the server or clients that ignore locks).


4. Mandatory Lock


Mandatory locks are enforced by the kernel — other processes cannot read/write the locked region until it’s unlocked.

This is a local filesystem feature, not part of NFSv4.

On Linux, to make a file use mandatory locking, you need to:

Mount the filesystem with the -o mand option.

Set the file’s permission bits:

chmod g+s,g-x filename


(group-ID bit set and group execute bit cleared)

Then the kernel enforces it:

any read/write that conflicts with an active mandatory lock will block or fail with EAGAIN.


� NFSv4 does not implement mandatory locks — the server doesn’t block file I/O like that.


5. Why NFSv4 Is Advisory


NFSv4 locking is protocol-level state management between distributed clients and server.

Mandatory semantics would require the server to understand every I/O operation and block them — which is impractical in a networked system with stateless transport and caching.

So NFSv4 chose to make locking advisory for interoperability and simplicity.


6. Example Illustration

| Case                     | Process A               | Process B                     | Result                               |

| ------------------------ | ----------------------- | ----------------------------- | ------------------------------------ |

| **Advisory (NFSv4)**     | Locks file region 0–100 | Opens same file, ignores lock | B can still read/write (not blocked) |

| **Mandatory (Local FS)** | Locks file region 0–100 | Tries to write same region    | B’s write syscall blocks or fails    |


7. Protocol-Level (NFSv4) Lock Enforcement


Within NFSv4 protocol:

The server enforces locking rules only among NFSv4 clients using the LOCK state database.

If Client A locks a region, Client B’s LOCK request for the same range returns:

NFS4ERR_DENIED

But if Client B ignores locking and just sends a WRITE RPC — the NFS server will still allow it (it’s advisory).


8. Summary Table

| Feature                   | Advisory Lock                      | Mandatory Lock                   |

| ------------------------- | ---------------------------------- | -------------------------------- |

| Enforced by               | Applications & server lock manager | Kernel                           |

| Other process can ignore? | Yes                                | No                               |

| Supported in NFSv4?       | ✅ Yes                              | ❌ No                             |

| Used by                   | `fcntl()`, `lockf()`, `NFSv4 LOCK` | Filesystems with `-o mand` mount |

| Effect on I/O             | Cooperative                        | Blocking or denied               |


NFSv4 locks are advisory — they coordinate access among NFS-aware clients but do not prevent non-locking I/O.

Mandatory locks are enforced by the kernel (local filesystems only), but NFS does not support them.








www.traceinside.com