mail-index-lock.c revision fe4d93fb06c39c82802c5a5ab9ddb3251863cf6c
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* Copyright (C) 2003-2004 Timo Sirainen */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster Locking should never fail or timeout. Exclusive locks must be kept as short
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster time as possible. Shared locks can be long living, so if we can't get
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster exclusive lock directly within 2 seconds, we'll replace the index file with
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster a copy of it. That means the shared lock holders can keep using the old file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster while we're modifying the new file.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster lock_id is used to figure out if acquired lock is still valid. When index
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster file is reopened, the lock_id can become invalid. It doesn't matter however,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster as no-one's going to modify the old file anymore.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster lock_id also tells if we're referring to shared or exclusive lock. This
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster allows us to drop back to shared locking once all exclusive locks are
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster dropped. Shared locks have even numbers, exclusive locks have odd numbers.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster The number is increased by two every time the lock is dropped or index file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster is reopened.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterint mail_index_lock_fd(struct mail_index *index, const char *path, int fd,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster ret = fcntl(fd, timeout_secs ? F_SETLKW : F_SETLK, &fl);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* locked by another process */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* most likely alarm hit, meaning we timeouted.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster even if not, we probably want to be killed
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster so stop blocking. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster mail_index_file_set_syscall_error(index, path, "fcntl()");
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster "(see lock_method setting in config file)");
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster int operation = timeout_secs != 0 ? 0 : LOCK_NB;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* a) locked by another process,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster b) timeouted */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* Give a bit more helpful error message since this
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster is the default locking method and it doesn't work
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster with NFS. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster "flock() failed with file %s: %m "
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster "(see lock_method setting in config file)",
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster mail_index_file_set_syscall_error(index, path, "flock()");
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* we shouldn't get here */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterstatic int mail_index_lock(struct mail_index *index, int lock_type,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster unsigned int *lock_id_r)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster i_assert(lock_type == F_RDLCK || lock_type == F_WRLCK);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster if (lock_type == F_RDLCK && index->lock_type != F_UNLCK) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster } else if (lock_type == F_WRLCK && index->lock_type == F_WRLCK) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster if (update_index && index->excl_lock_count == 0) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster if ((ret2 = mail_index_reopen_if_needed(index)) < 0)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster if (index->lock_method == MAIL_INDEX_LOCK_DOTLOCK) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* FIXME: exclusive locking will rewrite the index file every
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster time. shouldn't really be needed.. reading doesn't require
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster locks then, though */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster if (update_index && index->lock_type == F_UNLCK) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster if (lock_type == F_RDLCK || !index->log_locked) {
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster ret = mail_index_lock_fd(index, index->filepath, index->fd,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* this is kind of kludgy. we wish to avoid deadlocks while
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster trying to lock transaction log, but it can happen if our
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster process is holding transaction log lock and waiting for
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster index write lock, while the other process is holding index
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster read lock and waiting for transaction log lock.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster we don't have a problem with grabbing read index lock
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster because the only way for it to block is if it's
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster write-locked, which isn't allowed unless transaction log
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster is also locked.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster so, the workaround for this problem is that we simply try
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster locking once. if it doesn't work, just rewrite the file.
lock_type, 0);
if (ret <= 0)
return ret;
unsigned int *lock_id_r)
int ret;
if (ret > 0)
if (ret < 0)
unsigned int base_size;
const char *path;
if (ret == 0) {
return fd;
/* copy the index to index.tmp and use it */
unsigned int *lock_id_r)
int ret;
if (ret > 0)
if (ret < 0)
unsigned int base_size;
int fd;
F_RDLCK, 0);
return TRUE;
return FALSE;