NFS file locking?

Larry McVoy (lm@bitmover.com)
Sun, 14 Oct 2001 11:11:04 -0700


Hi, the open(2) man page says:

O_EXCL When used with O_CREAT, if the file already exists
it is an error and the open will fail. O_EXCL is
broken on NFS file systems, programs which rely on
it for performing locking tasks will contain a race
condition. The solution for performing atomic file
locking using a lockfile is to create a unique file
on the same fs (e.g., incorporating hostname and
pid), use link(2) to make a link to the lockfile.
If link() returns 0, the lock is successful. Oth­
erwise, use stat(2) on the unique file to check if
its link count has increased to 2, in which case
the lock is also successful.

I coded this up and tried it here on a cluster of different operating
systems (Linux 2.4.5 server, linux, freebsd, solaris, aix, hpux, irix
clients) and it doesn't work.

2 questions:

a) is it the belief of folks here that this should work?

b) if performance isn't a big issue, is there any portable way to do
locking over NFS with just files?

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/