Skip to contentSkip to conversation

flock.2: describe flock on NFS filesystems

PATCH In review Petr Uzel 2 participants 2 messages Sep 15, 2011 – Oct 3, 2017 1 message not in the archive Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore

Petr Uzel

author
#0 lore raw

PATCH 1/1 flock.2: describe flock on NFS filesystems

NFSv4 need file to be opened in RW-mode to place exclusive lock:
http://www.spinics.net/lists/linux-nfs/msg18502.html

flock works on NFS since 2.6.12:
http://nfs.sourceforge.net/#section_d ,section D10

---
 man2/flock.2 |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
man2/flock.2 +4 -2
index f0d85af..c904f6b 100644
@@ -107,7 +107,8 @@ are preserved across an
 .BR execve (2).
 
 A shared or exclusive lock can be placed on a file regardless of the
-mode in which the file was opened.
+mode in which the file was opened. The exception is NFSv4, where
+the file has to be opened for writing to place exclusive lock.
 .SH "RETURN VALUE"
 On success, zero is returned.
 On error, \-1 is returned, and
@@ -146,7 +147,8 @@ possibly implemented in terms of
 appears on most UNIX systems.
 .SH NOTES
 .BR flock ()
-does not lock files over NFS.
+does not lock files over NFS before kernel 2.6.12. Since
+2.6.12, kernel emulates flock () using fcntl(2).
 Use
 .BR fcntl (2)
 instead: that does work over NFS, given a sufficiently recent version of
Signature · 8 lines
1.7.3.4


Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

Hello Peter,

Quoted 5 lines · Petr Uzel

NFSv4 need file to be opened in RW-mode to place exclusive lock:
http://www.spinics.net/lists/linux-nfs/msg18502.html

flock works on NFS since 2.6.12:
http://nfs.sourceforge.net/#section_d ,section D10

Long ago, this patch fell on the floor. Sorry about that. Someone did later fix most of this, but the detail that NFS requires the file to be opened for writing in order to place a LOCK_EX lock was missing. I've added it now.

Thanks,

Michael

Quoted 30 lines
---
 man2/flock.2 |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
man2/flock.2 +4 -2
index f0d85af..c904f6b 100644
@@ -107,7 +107,8 @@ are preserved across an
 .BR execve (2).
 
 A shared or exclusive lock can be placed on a file regardless of the
-mode in which the file was opened.
+mode in which the file was opened. The exception is NFSv4, where
+the file has to be opened for writing to place exclusive lock.
 .SH "RETURN VALUE"
 On success, zero is returned.
 On error, \-1 is returned, and
@@ -146,7 +147,8 @@ possibly implemented in terms of
 appears on most UNIX systems.
 .SH NOTES
 .BR flock ()
-does not lock files over NFS.
+does not lock files over NFS before kernel 2.6.12. Since
+2.6.12, kernel emulates flock () using fcntl(2).
 Use
 .BR fcntl (2)
 instead: that does work over NFS, given a sufficiently recent version of
Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html