title: NFS - How to prevent deleting when the file is locked
body: <p>I have a program that writes some state info to a file and deletes it after it is useless. To solve the process level conflict, I use the <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/channels/FileChannel.html#lock()" rel="nofollow noreferrer">FileChannel.lock</a> to lock the file.</p>
  <p>It works well in most cases, but when using the NFS, I can see the file can be deleted without error even if the file is locked. The local file and SMB doesn't do this, they report an error when deleting. I tested both NFSv3 and NFSv4 both have the same behavior. The mount options are &quot;sync,mand&quot;. Anyone can help?</p>
  <p>I noticed the <a href="https://linux.die.net/man/2/fcntl" rel="nofollow noreferrer">fcntl document</a> says the &quot;Mandatory locking is enabled on a file by disabling group execute permission on the file and enabling the set-group-ID permission bit.&quot;. But when I did that, the JDK reports the error &quot;java.io.IOException: No locks available&quot;. Maybe I use the NFS locking in the wrong way?</p>