# HG changeset patch # User Timo Sirainen # Date 1101328922 -7200 # Node ID 51be21fc2c9d5e5b22f815206280ac3e59f06aa8 # Parent e7d6ae64fd68ea3d9785af3f98bbe836aca71836 Avoid re-reading index file with mmap_disable=yes. diff -r e7d6ae64fd68 -r 51be21fc2c9d src/lib-index/mail-index-sync.c --- a/src/lib-index/mail-index-sync.c Wed Nov 24 22:41:35 2004 +0200 +++ b/src/lib-index/mail-index-sync.c Wed Nov 24 22:42:02 2004 +0200 @@ -290,6 +290,10 @@ return 0; } +#define MAIL_INDEX_IS_SYNCS_SAME(index) \ + ((index)->sync_log_file_seq == (index)->hdr->log_file_seq && \ + (index)->sync_log_file_offset == (index)->hdr->log_file_ext_offset) + int mail_index_sync_begin(struct mail_index *index, struct mail_index_sync_ctx **ctx_r, struct mail_index_view **view_r, @@ -299,36 +303,25 @@ struct mail_index_sync_ctx *ctx; uint32_t seq; uoff_t offset; - unsigned int lock_id; + unsigned int lock_id = 0; int seen_external; if (mail_transaction_log_sync_lock(index->log, &seq, &offset) < 0) return -1; - if (mail_index_lock_shared(index, TRUE, &lock_id) < 0) { - mail_transaction_log_sync_unlock(index->log); - return -1; - } + if (!index->mmap_disable || !MAIL_INDEX_IS_SYNCS_SAME(index) || + index->sync_log_file_seq != seq || + index->sync_log_file_offset != offset) { + /* make sure we have the latest file mapped */ + if (mail_index_lock_shared(index, TRUE, &lock_id) < 0) { + mail_transaction_log_sync_unlock(index->log); + return -1; + } - if (index->mmap_disable) { - if (index->sync_log_file_seq != seq || - index->sync_log_file_offset != offset) { - /* we may have synced our internal view more than what - is synced in index. re-read the whole index if our - sync seq/offset doesn't match what is in index's - header. */ - if (mail_index_map(index, TRUE) <= 0) { - mail_transaction_log_sync_unlock(index->log); - mail_index_unlock(index, lock_id); - return -1; - } - } else { - /* the whole log file is synced already. */ - i_assert(index->map->hdr.log_file_seq == seq); - i_assert(index->map->hdr.log_file_ext_offset == offset); - } - } else { - if (mail_index_map(index, FALSE) <= 0) { + /* with mmap_disable the force parameter has somewhat special + meaning, it syncs exactly to the log seq/offset in index + file's header. */ + if (mail_index_map(index, index->mmap_disable) <= 0) { mail_transaction_log_sync_unlock(index->log); mail_index_unlock(index, lock_id); return -1; diff -r e7d6ae64fd68 -r 51be21fc2c9d src/lib-index/mail-index.c --- a/src/lib-index/mail-index.c Wed Nov 24 22:41:35 2004 +0200 +++ b/src/lib-index/mail-index.c Wed Nov 24 22:42:02 2004 +0200 @@ -696,6 +696,8 @@ struct mail_index_map *map; int ret; + i_assert(index->lock_type != F_UNLCK); + if (!force && index->map != NULL) { ret = mail_index_map_try_existing(index->map); if (ret != 0) @@ -972,6 +974,8 @@ uoff_t offset; int ret; + i_assert(index->lock_type == F_UNLCK); + /* log file lock protects index creation */ if (mail_transaction_log_sync_lock(index->log, &seq, &offset) < 0) return -1; @@ -990,7 +994,9 @@ mail_index_file_set_syscall_error(index, path, "write_full()"); ret = -1; } else { + index->lock_type = F_WRLCK; ret = mail_index_map(index, FALSE); + index->lock_type = F_UNLCK; } if (ret == 0) { @@ -1118,6 +1124,11 @@ (flags & MAIL_INDEX_OPEN_FLAG_MMAP_NO_WRITE) != 0; index->lock_method = lock_method; + /* don't even bother to handle dotlocking without mmap being + disabled. that combination simply doesn't make any sense */ + i_assert(lock_method != MAIL_INDEX_LOCK_DOTLOCK || + index->mmap_disable); + ret = mail_index_open_files(index, flags); if (ret <= 0) break; @@ -1183,9 +1194,7 @@ old_map = index->map; old_fd = index->fd; - - index->map = NULL; - index->hdr = NULL; + old_map->refcount++; /* new file, new locks. the old fd can keep it's locks, they don't matter anymore as no-one's going to modify the file. */ @@ -1213,7 +1222,10 @@ } if (ret == 0) { - if (mail_index_map(index, FALSE) <= 0) + /* read the new mapping. note that with mmap_disable we want + to keep the old mapping in index->map so we can update it + by reading transaction log. */ + if (mail_index_map(index, TRUE) <= 0) ret = -1; }