Using C, running under Linux, I am trying to figure out how to have 2 different processes access the same file located on an NFSv4 filesystem. Specifically, I want to have one program fopen a file for reading, and have a second program fopen the file for writing. I am intending use the new EFS (Elastic File System) from AWS, and EFS is only NFSv4.
The nature of the programs is such that the writer will, in essence, only be appending to the file. The reader will never access the newly-appended info until the writer has finished and called fflush.
Does NFSv4 support this scenario?
Or is there a better way altogether to achieve such concurrent NFSv4 file access?
NFS v4 does support byte range locking on files (as outlined in this excerpt from 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 (primarilyREAD
).
Refer to section 8 of the same RFC for greater detail on locking and the lease-based model.
Your program will need to use fcntl
to manage locking. My advice is to write a test to check that the actual implementation of NFS v4 provided by EFS supports locking as expected (some aspects are optional in the specification). You can refer to the source code of this lock testing suite, which supports multi-client testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With