mail-index.c revision 12e1f3cc0cf713ed131df801c36cdf8fe33d174f
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "lib.h"
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen#include "ioloop.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "file-lock.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "file-set-size.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mmap-util.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mail-index.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mail-index-data.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mail-index-util.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mail-tree.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "mail-modifylog.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-custom-flags.h"
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#include <unistd.h>
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include <fcntl.h>
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#include <utime.h>
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic int mmap_verify(MailIndex *index)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen MailIndexHeader *hdr;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen unsigned int extra;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_used_length = 0;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen if (index->mmap_full_length < sizeof(MailIndexHeader)) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_corrupted(index, "File too small");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen extra = (index->mmap_full_length - sizeof(MailIndexHeader)) %
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen sizeof(MailIndexRecord);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (extra != 0) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* partial write or corrupted -
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen truncate the file to valid length */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen i_assert(!index->anon_mmap);
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_full_length -= extra;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen (void)ftruncate(index->fd, (off_t)index->mmap_full_length);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen /* keep the header set even if we fail, so we can update the flags */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen hdr = index->mmap_base;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index->header = hdr;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (hdr->used_file_size > index->mmap_full_length) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_corrupted(index,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen "used_file_size larger than real file size "
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen "(%"PRIuUOFF_T" vs %"PRIuSIZE_T")",
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen hdr->used_file_size,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_full_length);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if ((hdr->used_file_size - sizeof(MailIndexHeader)) %
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen sizeof(MailIndexRecord) != 0) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_corrupted(index, "Invalid used_file_size in header "
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen "(%"PRIuUOFF_T")",
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen hdr->used_file_size);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->messages_count < hdr->seen_messages_count) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_corrupted(index, "Invalid seen messages count "
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen "(%u < %u)", hdr->messages_count,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen hdr->seen_messages_count);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (hdr->messages_count < hdr->deleted_messages_count) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_corrupted(index, "Invalid deleted messages count "
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen "(%u < %u)", hdr->messages_count,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen hdr->deleted_messages_count);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->sync_id = hdr->sync_id;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_used_length = hdr->used_file_size;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenint mail_index_mmap_update(MailIndex *index)
6389aeec8c26b585e583c364b48ad12adf741898Timo Sirainen{
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen if (index->anon_mmap)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return mmap_verify(index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->mmap_base != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->header = (MailIndexHeader *) index->mmap_base;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* make sure file size hasn't changed */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->header->sync_id == index->sync_id) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_used_length = index->header->used_file_size;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->mmap_used_length > index->mmap_full_length) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen i_panic("Index file size was grown without "
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen "updating sync_id");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (msync(index->mmap_base,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_used_length, MS_SYNC) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index_set_syscall_error(index, "msync()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (munmap(index->mmap_base, index->mmap_full_length) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index_set_syscall_error(index, "munmap()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_base = mmap_rw_file(index->fd, &index->mmap_full_length);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->mmap_base == MAP_FAILED) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_base = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_used_length = 0;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_syscall_error(index, "mmap()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mmap_verify(index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenvoid mail_index_close(MailIndex *index)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->set_flags = 0;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index->set_cache_fields = 0;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->opened = FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->inconsistent = FALSE;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->lock_type = MAIL_LOCK_UNLOCK;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->header = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->fd != -1) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (close(index->fd) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_syscall_error(index, "close()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->fd = -1;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->filepath != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen i_free(index->filepath);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index->filepath = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->anon_mmap) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (munmap_anon(index->mmap_base, index->mmap_full_length) < 0)
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index_set_syscall_error(index, "munmap_anon()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->anon_mmap = FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen } else if (index->mmap_base != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (munmap(index->mmap_base, index->mmap_full_length) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_syscall_error(index, "munmap()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->mmap_base = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->data != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_index_data_free(index->data);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->data = NULL;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen }
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->tree != NULL) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen mail_tree_free(index->tree);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->tree = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->modifylog != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_modifylog_free(index->modifylog);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->modifylog = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->custom_flags != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_custom_flags_free(index->custom_flags);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index->custom_flags = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->error != NULL) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen i_free(index->error);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->error = NULL;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic int mail_index_sync_file(MailIndex *index)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen unsigned int i;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen int failed, fsync_fds[3];
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->anon_mmap)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen for (i = 0; i < sizeof(fsync_fds)/sizeof(fsync_fds[0]); i++)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen fsync_fds[i] = -1;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_data_sync_file(index->data, &fsync_fds[0]))
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (msync(index->mmap_base, index->mmap_used_length, MS_SYNC) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index_set_syscall_error(index, "msync()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen failed = FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->tree != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_tree_sync_file(index->tree, &fsync_fds[1]))
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen failed = TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->modifylog != NULL) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_modifylog_sync_file(index->modifylog, &fsync_fds[2]))
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen failed = TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen for (i = 0; i < sizeof(fsync_fds)/sizeof(fsync_fds[0]); i++) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (fsync_fds[i] != -1 && fdatasync(fsync_fds[i]) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_error(index, "fdatasync(%u) failed: %m", i);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (fdatasync(index->fd) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index_set_syscall_error(index, "fdatasync()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return !failed;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic void mail_index_update_timestamp(MailIndex *index)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct utimbuf ut;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* keep index's modify stamp same as the sync file's stamp */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen ut.actime = ioloop_time;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen ut.modtime = index->file_sync_stamp;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (utime(index->filepath, &ut) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_syscall_error(index, "utime()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenint mail_index_fmdatasync(MailIndex *index, size_t size)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!index->anon_mmap) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (msync(index->mmap_base, size, MS_SYNC) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index_set_syscall_error(index, "msync()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (fdatasync(index->fd) < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index_set_syscall_error(index, "fdatasync()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic void mail_index_update_header_changes(MailIndex *index)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->set_flags != 0) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->header->flags |= index->set_flags;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->set_flags = 0;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen if (index->set_cache_fields != 0) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->header->cache_fields = index->set_cache_fields;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->set_cache_fields = 0;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic int mail_index_write_header_changes(MailIndex *index)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen int failed;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* use our own locking here so we don't mess up with any other
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index states, like inconsistency. */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_wait_lock(index, F_WRLCK))
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#ifdef DEBUG
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mprotect(index->mmap_base, index->mmap_used_length,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen PROT_READ|PROT_WRITE);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#endif
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_index_update_header_changes(index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen failed = msync(index->mmap_base, sizeof(MailIndexHeader), MS_SYNC) < 0;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (failed)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_syscall_error(index, "msync()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#ifdef DEBUG
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mprotect(index->mmap_base, index->mmap_used_length, PROT_NONE);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#endif
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen if (!mail_index_wait_lock(index, F_UNLCK))
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen return FALSE;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen return !failed;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenstatic int mail_index_lock_remove(MailIndex *index)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen MailLockType old_lock_type;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_wait_lock(index, F_UNLCK))
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen old_lock_type = index->lock_type;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->lock_type = MAIL_LOCK_UNLOCK;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (old_lock_type == MAIL_LOCK_SHARED) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* releasing shared lock. we may need to update some
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen flags in header. */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen unsigned int old_flags;
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen MailDataField old_cache;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen old_flags = index->header->flags;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen old_cache = index->header->cache_fields;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if ((old_flags | index->set_flags) != old_flags ||
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen (old_cache | index->set_cache_fields) != old_cache)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return mail_index_write_header_changes(index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen debug_mprotect(index->mmap_base, index->mmap_full_length, index);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen return TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainenstatic int mail_index_lock_change(MailIndex *index, MailLockType lock_type,
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen int try_lock)
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen{
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen int ret, fd_lock_type;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen /* shared -> exclusive isn't allowed without try_lock */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen i_assert(try_lock || lock_type != MAIL_LOCK_EXCLUSIVE ||
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen index->lock_type != MAIL_LOCK_SHARED);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->inconsistent) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* index is in inconsistent state and nothing else than
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen free() is allowed for it. */
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen fd_lock_type = MAIL_LOCK_TO_FLOCK(lock_type);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (try_lock) {
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen ret = file_try_lock(index->fd, fd_lock_type);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (ret < 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_syscall_error(index, "file_try_lock()");
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (ret <= 0)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen } else {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_wait_lock(index, fd_lock_type))
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index->lock_type = lock_type;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen debug_mprotect(index->mmap_base, index->mmap_full_length, index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_mmap_update(index)) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen (void)index->set_lock(index, MAIL_LOCK_UNLOCK);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen return FALSE;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen }
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen if (index->indexid != index->header->indexid) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* index was rebuilt, there's no way we can maintain
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen consistency */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_error(index, "Warning: Inconsistency - Index "
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen "%s was rebuilt while we had it open",
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->filepath);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->inconsistent = TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen }
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->header->flags & MAIL_INDEX_FLAG_FSCK) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* someone just partially updated the index, need to fsck it */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (lock_type == MAIL_LOCK_SHARED) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* we need exclusive lock so fsck()'s set_lock() won't
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen get us back here */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_lock_remove(index))
0b1f70057d59ed3fe7a163bd4fde0c75353910f3Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_wait_lock(index, F_WRLCK))
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->lock_type = MAIL_LOCK_EXCLUSIVE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen debug_mprotect(index->mmap_base,
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index->mmap_full_length, index);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* check again, in case it was already fscked while we had
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen it unlocked for a while */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->header->flags & MAIL_INDEX_FLAG_FSCK) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!index->fsck(index))
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (lock_type == MAIL_LOCK_SHARED) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen /* drop exclusive lock */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return index->set_lock(index, lock_type);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (lock_type == MAIL_LOCK_EXCLUSIVE) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen /* while holding exclusive lock, keep the FSCK flag on.
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen when the lock is released, the FSCK flag will also be
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen removed. */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->header->flags |= MAIL_INDEX_FLAG_FSCK;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (!mail_index_fmdatasync(index, sizeof(MailIndexHeader))) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen (void)index->set_lock(index, MAIL_LOCK_UNLOCK);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen return FALSE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return TRUE;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen}
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic int mail_index_lock_full(MailIndex *index, MailLockType lock_type,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen int try_lock)
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen int keep_fsck;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->lock_type == lock_type)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return TRUE;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen if (index->anon_mmap) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* anonymous mmaps are private and don't need any locking */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_index_update_header_changes(index);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen index->lock_type = lock_type;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen debug_mprotect(index->mmap_base, index->mmap_full_length,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return TRUE;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
73d29cffe84aa9742353c40516a09e18385ab341Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->lock_type == MAIL_LOCK_EXCLUSIVE) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (index->modifylog != NULL)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_modifylog_notify_lock_drop(index->modifylog);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen /* dropping exclusive lock (either unlock or to shared) */
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen keep_fsck = (index->set_flags & MAIL_INDEX_FLAG_FSCK) != 0;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_index_update_header_changes(index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen /* remove the FSCK flag only after successful fsync() */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (mail_index_sync_file(index) && !keep_fsck) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index->header->flags &= ~MAIL_INDEX_FLAG_FSCK;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (msync(index->mmap_base, sizeof(MailIndexHeader),
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen MS_SYNC) < 0) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen /* we only failed to remove the fsck flag,
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen so this isn't fatal. */
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen index_set_syscall_error(index, "msync()");
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_index_update_timestamp(index);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (lock_type == MAIL_LOCK_UNLOCK)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return mail_index_lock_remove(index);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen else
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return mail_index_lock_change(index, lock_type, try_lock);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenint mail_index_set_lock(MailIndex *index, MailLockType lock_type)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return mail_index_lock_full(index, lock_type, FALSE);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenint mail_index_try_lock(MailIndex *index, MailLockType lock_type)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return mail_index_lock_full(index, lock_type, TRUE);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
void mail_index_set_lock_notify_callback(MailIndex *index,
MailLockNotifyFunc func,
void *context)
{
index->lock_notify_func = func;
index->lock_notify_context = context;
}
int mail_index_verify_hole_range(MailIndex *index)
{
MailIndexHeader *hdr;
unsigned int max_records;
hdr = index->header;
if (hdr->first_hole_records == 0)
return TRUE;
max_records = MAIL_INDEX_RECORD_COUNT(index);
if (hdr->first_hole_index >= max_records) {
index_set_corrupted(index,
"first_hole_index points outside file");
return FALSE;
}
/* check that first_hole_records is in valid range */
if (max_records - hdr->first_hole_index < hdr->first_hole_records) {
index_set_corrupted(index,
"first_hole_records points outside file");
return FALSE;
}
return TRUE;
}
MailIndexHeader *mail_index_get_header(MailIndex *index)
{
i_assert(index->lock_type != MAIL_LOCK_UNLOCK);
return index->header;
}
MailIndexRecord *mail_index_lookup(MailIndex *index, unsigned int seq)
{
MailIndexHeader *hdr;
MailIndexRecord *rec;
const char *error;
unsigned int idx;
i_assert(seq > 0);
i_assert(index->lock_type != MAIL_LOCK_UNLOCK);
hdr = index->header;
if (seq > hdr->messages_count) {
/* out of range */
return NULL;
}
if (!mail_index_verify_hole_range(index))
return NULL;
idx = seq-1;
if (hdr->first_hole_records == 0 || hdr->first_hole_index > idx) {
/* easy, it's just at the expected index */
error = t_strdup_printf("Invalid first_hole_index in header: "
"%u", idx);
} else if (hdr->first_hole_records ==
MAIL_INDEX_RECORD_COUNT(index) - hdr->messages_count) {
/* only one hole in file, skip it and we're at
correct position */
idx += hdr->first_hole_records;
error = t_strdup_printf("Invalid hole locations in header: %u",
idx);
} else {
/* find from binary tree */
idx = mail_tree_lookup_sequence(index->tree, seq);
if (idx == (unsigned int)-1) {
index_set_corrupted(index,
"Sequence %u not found from binary tree "
"(%u msgs says header)",
seq, hdr->messages_count);
return NULL;
}
error = t_strdup_printf("Invalid offset returned by "
"binary tree: %u", idx);
}
if (idx >= MAIL_INDEX_RECORD_COUNT(index)) {
index_set_corrupted(index, "%s", error);
return NULL;
}
rec = (MailIndexRecord *) ((char *) index->mmap_base +
sizeof(MailIndexHeader)) + idx;
if (rec->uid == 0) {
index_set_corrupted(index, "%s", error);
return NULL;
}
return rec;
}
MailIndexRecord *mail_index_next(MailIndex *index, MailIndexRecord *rec)
{
MailIndexRecord *end_rec;
i_assert(index->lock_type != MAIL_LOCK_UNLOCK);
i_assert(rec >= (MailIndexRecord *) index->mmap_base);
if (rec == NULL)
return NULL;
/* go to the next non-deleted record */
end_rec = (MailIndexRecord *) ((char *) index->mmap_base +
index->mmap_used_length);
while (++rec < end_rec) {
if (rec->uid != 0)
return rec;
}
return NULL;
}
MailIndexRecord *mail_index_lookup_uid_range(MailIndex *index,
unsigned int first_uid,
unsigned int last_uid,
unsigned int *seq_r)
{
MailIndexRecord *rec;
unsigned int idx;
i_assert(index->lock_type != MAIL_LOCK_UNLOCK);
i_assert(first_uid > 0 && last_uid > 0);
i_assert(first_uid <= last_uid);
idx = mail_tree_lookup_uid_range(index->tree, seq_r,
first_uid, last_uid);
if (idx == (unsigned int)-1)
return NULL;
if (idx >= MAIL_INDEX_RECORD_COUNT(index)) {
index_set_error(index, "Corrupted binary tree for index %s: "
"lookup returned index outside range "
"(%u >= %"PRIuSIZE_T")", index->filepath, idx,
MAIL_INDEX_RECORD_COUNT(index));
index->set_flags |= MAIL_INDEX_FLAG_REBUILD_TREE;
return NULL;
}
rec = (MailIndexRecord *) ((char *) index->mmap_base +
sizeof(MailIndexHeader)) + idx;
if (rec->uid < first_uid || rec->uid > last_uid) {
index_set_error(index, "Corrupted binary tree for index %s: "
"lookup returned offset to wrong UID "
"(%u vs %u..%u)", index->filepath,
rec->uid, first_uid, last_uid);
index->set_flags |= MAIL_INDEX_FLAG_REBUILD_TREE;
return NULL;
}
return rec;
}
const char *mail_index_lookup_field(MailIndex *index, MailIndexRecord *rec,
MailDataField field)
{
MailIndexDataRecord *datarec;
datarec = (rec->data_fields & field) == 0 ? NULL :
mail_index_data_lookup(index->data, rec, field);
if (datarec == NULL)
return NULL;
if (!mail_index_data_record_verify(index->data, datarec)) {
/* index is corrupted, it will be rebuilt */
return NULL;
}
return datarec->data;
}
const void *mail_index_lookup_field_raw(MailIndex *index, MailIndexRecord *rec,
MailDataField field, size_t *size)
{
MailIndexDataRecordHeader *datahdr;
MailIndexDataRecord *datarec;
if ((rec->data_fields & field) == 0) {
*size = 0;
return NULL;
}
if (field < DATA_FIELD_LAST) {
/* read data field */
datarec = mail_index_data_lookup(index->data, rec, field);
if (datarec == NULL) {
*size = 0;
return NULL;
}
*size = datarec->full_field_size;
return datarec->data;
}
/* read header field */
datahdr = mail_index_data_lookup_header(index->data, rec);
if (datahdr == NULL) {
*size = 0;
return NULL;
}
switch (field) {
case DATA_HDR_INTERNAL_DATE:
*size = sizeof(datahdr->internal_date);
return &datahdr->internal_date;
case DATA_HDR_VIRTUAL_SIZE:
*size = sizeof(datahdr->virtual_size);
return &datahdr->virtual_size;
case DATA_HDR_HEADER_SIZE:
*size = sizeof(datahdr->header_size);
return &datahdr->header_size;
case DATA_HDR_BODY_SIZE:
*size = sizeof(datahdr->body_size);
return &datahdr->body_size;
default:
*size = 0;
return NULL;
}
}
void mail_index_cache_fields_later(MailIndex *index, MailDataField field)
{
i_assert(index->lock_type != MAIL_LOCK_UNLOCK);
field &= ~index->never_cache_fields;
/* first check if the field even could be in the file */
if ((index->set_cache_fields & field) != field) {
if ((index->header->cache_fields & field) == 0) {
/* no, but make sure the future records will have it.
we don't immediately mark the index to cache this
field for old messages as some clients never ask
the info again */
index->set_cache_fields |= field;
} else {
/* this is at least the second time it's being asked,
make sure it'll be cached soon. */
index->set_flags |= MAIL_INDEX_FLAG_CACHE_FIELDS;
}
}
}
time_t mail_get_internal_date(MailIndex *index, MailIndexRecord *rec)
{
const time_t *date;
size_t size;
date = index->lookup_field_raw(index, rec,
DATA_HDR_INTERNAL_DATE, &size);
if (date == NULL)
return (time_t)-1;
else {
i_assert(size == sizeof(*date));
return *date;
}
}
void mail_index_mark_flag_changes(MailIndex *index, MailIndexRecord *rec,
MailFlags old_flags, MailFlags new_flags)
{
if ((old_flags & MAIL_SEEN) == 0 && (new_flags & MAIL_SEEN)) {
/* unseen -> seen */
index->header->seen_messages_count++;
} else if ((old_flags & MAIL_SEEN) && (new_flags & MAIL_SEEN) == 0) {
/* seen -> unseen */
if (index->header->seen_messages_count ==
index->header->messages_count) {
/* this is the first unseen message */
index->header->first_unseen_uid_lowwater = rec->uid;
} else if (rec->uid < index->header->first_unseen_uid_lowwater)
index->header->first_unseen_uid_lowwater = rec->uid;
if (index->header->seen_messages_count == 0) {
index_set_corrupted(index,
"seen_messages_count in header is invalid");
} else {
index->header->seen_messages_count--;
}
}
if ((old_flags & MAIL_DELETED) == 0 && (new_flags & MAIL_DELETED)) {
/* undeleted -> deleted */
index->header->deleted_messages_count++;
if (index->header->deleted_messages_count == 1) {
/* this is the first deleted message */
index->header->first_deleted_uid_lowwater = rec->uid;
} else if (rec->uid < index->header->first_deleted_uid_lowwater)
index->header->first_deleted_uid_lowwater = rec->uid;
} else if ((old_flags & MAIL_DELETED) &&
(new_flags & MAIL_DELETED) == 0) {
/* deleted -> undeleted */
if (index->header->deleted_messages_count == 0) {
index_set_corrupted(index,
"deleted_messages_count in header is invalid");
} else {
index->header->deleted_messages_count--;
}
}
}
static void update_first_hole_records(MailIndex *index)
{
MailIndexRecord *rec, *end_rec;
/* see if first_hole_records can be grown */
rec = (MailIndexRecord *) ((char *) index->mmap_base +
sizeof(MailIndexHeader)) +
index->header->first_hole_index +
index->header->first_hole_records;
end_rec = (MailIndexRecord *) ((char *) index->mmap_base +
index->mmap_used_length);
while (rec < end_rec && rec->uid == 0) {
index->header->first_hole_records++;
rec++;
}
}
static int mail_index_truncate_hole(MailIndex *index)
{
index->header->used_file_size = sizeof(MailIndexHeader) +
(uoff_t)index->header->first_hole_index *
sizeof(MailIndexRecord);
index->header->first_hole_index = 0;
index->header->first_hole_records = 0;
index->mmap_used_length = index->header->used_file_size;
if (!mail_index_truncate(index))
return FALSE;
if (index->header->messages_count == 0) {
/* all mail was deleted, truncate data file */
if (!mail_index_data_reset(index->data))
return FALSE;
}
return TRUE;
}
#define INDEX_NEED_COMPRESS(records, hdr) \
((records) > INDEX_MIN_RECORDS_COUNT && \
(records) * (100-INDEX_COMPRESS_PERCENTAGE) / 100 > \
(hdr)->messages_count)
int mail_index_expunge(MailIndex *index, MailIndexRecord *rec,
unsigned int seq, int external_change)
{
MailIndexHeader *hdr;
unsigned int records, uid, idx;
i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
i_assert(seq != 0);
i_assert(rec->uid != 0);
if (!mail_index_verify_hole_range(index))
return FALSE;
hdr = index->header;
/* setting UID to 0 is enough for deleting the mail from index */
uid = rec->uid;
rec->uid = 0;
/* update first hole */
idx = INDEX_RECORD_INDEX(index, rec);
if (hdr->first_hole_records == 0) {
/* first deleted message in index */
hdr->first_hole_index = idx;
hdr->first_hole_records = 1;
} else if (idx+1 == hdr->first_hole_index) {
/* deleted the previous record before hole */
hdr->first_hole_index--;
hdr->first_hole_records++;
} else if (idx == hdr->first_hole_index + hdr->first_hole_records) {
/* deleted the next record after hole */
hdr->first_hole_records++;
update_first_hole_records(index);
} else {
/* second hole coming to index file */
if (idx < hdr->first_hole_index) {
/* new hole before the old hole */
hdr->first_hole_index = idx;
hdr->first_hole_records = 1;
}
}
/* update message counts */
if (hdr->messages_count == 0) {
/* corrupted */
index_set_corrupted(index,
"Header says there's no mail while expunging");
return FALSE;
}
hdr->messages_count--;
mail_index_mark_flag_changes(index, rec, rec->msg_flags, 0);
(void)mail_index_data_delete(index->data, rec);
records = MAIL_INDEX_RECORD_COUNT(index);
if (hdr->first_hole_index + hdr->first_hole_records == records) {
/* the hole reaches end of file, truncate it */
(void)mail_index_truncate_hole(index);
} else {
if (INDEX_NEED_COMPRESS(records, hdr))
hdr->flags |= MAIL_INDEX_FLAG_COMPRESS;
}
/* expunge() may be called while index is being rebuilt and when
tree file hasn't been opened yet */
if (index->tree != NULL)
mail_tree_delete(index->tree, uid);
else {
/* make sure it also gets updated */
index->header->flags |= MAIL_INDEX_FLAG_REBUILD_TREE;
}
if (seq != 0 && index->modifylog != NULL) {
if (!mail_modifylog_add_expunge(index->modifylog, seq,
uid, external_change))
return FALSE;
}
return TRUE;
}
int mail_index_update_flags(MailIndex *index, MailIndexRecord *rec,
unsigned int seq, MailFlags flags,
int external_change)
{
i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
i_assert(seq != 0);
if (flags == rec->msg_flags)
return TRUE; /* no changes */
mail_index_mark_flag_changes(index, rec, rec->msg_flags, flags);
rec->msg_flags = flags;
return index->modifylog == NULL ? TRUE :
mail_modifylog_add_flags(index->modifylog, seq,
rec->uid, external_change);
}
static int mail_index_grow(MailIndex *index)
{
uoff_t pos;
unsigned int grow_count;
void *base;
grow_count = index->header->messages_count *
INDEX_GROW_PERCENTAGE / 100;
if (grow_count < 16)
grow_count = 16;
pos = index->mmap_full_length + (grow_count * sizeof(MailIndexRecord));
i_assert(pos < OFF_T_MAX);
if (index->anon_mmap) {
i_assert(pos < SSIZE_T_MAX);
base = mremap_anon(index->mmap_base, index->mmap_full_length,
(size_t)pos, MREMAP_MAYMOVE);
if (base == MAP_FAILED)
return index_set_syscall_error(index, "mremap_anon()");
index->mmap_base = base;
index->mmap_full_length = (size_t)pos;
return mmap_verify(index);
}
if (file_set_size(index->fd, (off_t)pos) < 0) {
if (errno == ENOSPC)
index->nodiskspace = TRUE;
return index_set_syscall_error(index, "file_set_size()");
}
/* file size changed, let others know about it too by changing
sync_id in header. */
index->header->sync_id++;
if (!mail_index_mmap_update(index))
return FALSE;
return TRUE;
}
MailIndexRecord *mail_index_append_begin(MailIndex *index)
{
MailIndexRecord *rec;
i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
if (index->mmap_used_length == index->mmap_full_length) {
if (!mail_index_grow(index))
return NULL;
}
i_assert(index->header->used_file_size == index->mmap_used_length);
i_assert(index->mmap_used_length + sizeof(MailIndexRecord) <=
index->mmap_full_length);
rec = (MailIndexRecord *) ((char *) index->mmap_base +
index->mmap_used_length);
memset(rec, 0, sizeof(MailIndexRecord));
index->header->used_file_size += sizeof(MailIndexRecord);
index->mmap_used_length += sizeof(MailIndexRecord);
return rec;
}
int mail_index_append_end(MailIndex *index, MailIndexRecord *rec)
{
i_assert(rec->uid == 0);
if (index->header->next_uid == MAX_ALLOWED_UID) {
index->set_flags |= MAIL_INDEX_FLAG_REBUILD;
index_set_error(index, "Reached maximum UID in mailbox %s, "
"rebuilding index", index->filepath);
return FALSE;
}
index->header->messages_count++;
rec->uid = index->header->next_uid++;
if (index->tree != NULL) {
mail_tree_insert(index->tree, rec->uid,
INDEX_RECORD_INDEX(index, rec));
}
return TRUE;
}
MailIndexError mail_index_get_last_error(MailIndex *index)
{
if (index->inconsistent)
return MAIL_INDEX_ERROR_INCONSISTENT;
if (index->nodiskspace)
return MAIL_INDEX_ERROR_DISKSPACE;
if (index->index_lock_timeout)
return MAIL_INDEX_ERROR_INDEX_LOCK_TIMEOUT;
if (index->mailbox_lock_timeout)
return MAIL_INDEX_ERROR_MAILBOX_LOCK_TIMEOUT;
if (index->error != NULL)
return MAIL_INDEX_ERROR_INTERNAL;
return MAIL_INDEX_ERROR_NONE;
}
const char *mail_index_get_last_error_text(MailIndex *index)
{
return index->error;
}