nfsv4 linux c 中的并发文件访问

Concurrent file access in nfsv4 linux c

使用 C,运行 在 Linux 下,我试图弄清楚如何让 2 个不同的进程访问位于 NFSv4 文件系统上的同一个文件。具体来说,我想让一个程序打开文件进行读取,并让第二个程序打开文件进行写入。我打算使用 AWS 的新 EFS(弹性文件系统),而 EFS 只是 NFSv4。

程序的性质使得编写者本质上只会附加到文件中。在编写器完成并调用 fflush 之前,reader 永远不会访问新附加的信息。

NFSv4 是否支持这种情况?

或者是否有更好的方法来实现这种并发 NFSv4 文件访问?

NFS v4 确实支持文件的字节范围锁定(如 RFC 3530 "NFS version 4 protocol" 的摘录中所述):

1.4.5. File locking

With the NFS version 4 protocol, the support for byte range file locking is part of the NFS protocol. The file locking support is structured so that an RPC callback mechanism is not required. This is a departure from the previous versions of the NFS file locking protocol, Network Lock Manager (NLM). The state associated with file locks is maintained at the server under a lease-based model. The server defines a single lease period for all state held by a NFS client. If the client does not renew its lease within the defined period, all state associated with the client's lease may be released by the server. The client may renew its lease with use of the RENEW operation or implicitly by use of other operations (primarily READ).

有关锁定和基于租用模型的更多详细信息,请参阅同一 RFC 的第 8 节。

您的程序将需要使用 fcntl 来管理锁定。我的建议是编写一个测试来检查 EFS 提供的 NFS v4 的实​​际实现是否支持预期的锁定(某些方面在规范中是可选的)。可以参考this lock testing suite的源码,支持多客户端测试