There was a thread about this in Info-AFS about a year ago, when I
still believed locking was working OK for me. There was also rumor
about an existing patch. Meanwhile, I discovered some problems. I am
running AFS 3.3a on rs_aix32. Please don't tell me that I should not be
doing mail in AFS ...
Description of Problem:
I am trying to write a small C program that appends to an AFS file
while holding a write lock on the complete file. The idea is of course
that running multiple instances of that program on one or more AFS
clients should never cause loss of data or corruption of the file.
If I try this with 2 instances on the SAME client, it appears to work
fine. If instance 2 has opened the file in append mode, and then has to
wait for the lock while instance 1 is appending, it will automatically
figure out the new file size and append after the data from instance 1
once the lock has been granted.
However, if I try with 2 instances on DIFFERENT clients, the file gets
corrupted. In the same situation, instance 2 will append at the
position where the end-of-file happened to be when it opened the file,
thus overwriting data meanwhile appended by instance 1. I tried to
again seek the end-of-the file or explicitly obtain the new file size
after the lock was granted, but instance 2 keeps believing that the
file size hasn't changed after it was opened. I also played around with
lots of fflush and fsync without any success. The only thing that makes
instance 2 aware of the new end-of-file is to close and re-open the
file.
A possible solution to my problem is to indeed re-open the file once
the lock is granted, and obtain a new lock, or to do a non-blocking
lock and close/re-open the file if I cannot get a lock immediately.
While this would probably reduce the chance of file corruption
drastically, it is not a 'correct' solution, as the combination of
opening and locking is not atomic, and there is always a chance that
some other process can obtain a lock in between.
Can anyone enlighten me about how this is supposed to work? Is there a
way to open/lockfx atomically? Has anyone seen that problem? The whole
thing gets even more complicated by the fact that eventually there are
other programs - standard mail handlers like mail, mh, and Z-Mail -
involved which I can't change, and which don't bother to support AFS
explicitly.
--
Michael Niksch TEL: +41-1-7248-913
IBM Zurich Research Laboratory FAX: +41-1-7103608
Saeumerstrasse 4 SMTP: [EMAIL PROTECTED]
CH-8803 Rueschlikon / Switzerland RSCS: NIK at ZURICH
--- End of forwarded mail from <[EMAIL PROTECTED]> ("Michael Niksch")
--
Michael Niksch TEL: +41-1-7248-913
IBM Zurich Research Laboratory FAX: +41-1-7103608
Saeumerstrasse 4 SMTP: [EMAIL PROTECTED]
CH-8803 Rueschlikon / Switzerland RSCS: NIK at ZURICH