atomic locking over NFS with link(2),stat(2)
Posted on 1998-09-07
Because of the potential for the loss of RPC reply packets on NFS, the O_EXCL option of open does not work. For this reason, one can/should open (O_CREAT|O_EXCL) a [temporary] file, link(2) this to the name of the file we really wanted to open [discarding the return value of the link(2) call], and then stat the newer name (the name we really wanted to create). If the hard link count is 2, the process has worked. Otherwise the process has failed.
What I'm not so clear on it what the backout strategy for failure is that allows us to retry later. If the strategy fails and the link count is >2, presumably one should unlink() the new name. Is this right? But what if the link count is only 1? Do I unlink anything then? What actions do I take if the stat(2) fails? What are the pros/cons of using lstat/fstat here instead of stat? Which of these is correct?
(This lockfile strategy doesn't have to be compatible with any MTA or anything; I just need to atomically create a lockfile whose name is fixed).