2008-02-27 17:43:38

by Brian Smith

[permalink] [raw]
Subject: cache revalidation & locking

According to http://www.unixcoding.org/NFSCoding#attrcache, the Linux NFS client flushes the attribute cache when a fnctl lock is obtained ("fcntl() locking flushes attribute cache with Linux and Solaris, but not with FreeBSD.")

1. What is the first version of the client that does this (in terms of Linux kernel version)?

2. Is this a side-effect of the implementation, or is this a guarentee that the Linux client makes?

What I really want to know is "How can I get consistent read access, and perform safe appends, when multiple systems are appending to a file?"

My current locking strategy is:
Opening the file:
* fd = open(path, O_RDWR | O_APPEND | O_SYNC)
* verify file integrity via checksums embedded in it
* lock the while file (l_type=F_RDLCK, l_len=0, l_off=0)
* fstat(fd, &st);
* unlock everything after EOF (l_len=0, l_off=st.st_size)
* read up to (st.st_size)
* unlock everything before EOF (l_len=st.st_size, l_off=0)
* lock the whole file (l_type=F_WRLCK, l_len=0, l_off=0)
* fstat(fd, &st);
* read up to (st.st_size)
* replace write lock with read lock before EOF
(l_len=st.st_size, l_off=0)
* append the new data.
* unlock the whole file (l_len = 0, l_off = 0)
Protection against partial write failures:
* checksums are written within the file in every write()

Is this a sound strategy when using the Linux NFS client? Is there a better one (one that reduces lock contention?)

- Brian