mail-transaction-log-file.c revision c026384095b555cc86d032b043d107cc371aacec
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2003-2008 Dovecot authors, see the included COPYING file */
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen#define MEMORY_LOG_NAME "(in-memory transaction log file)"
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainenmail_transaction_log_file_set_corrupted(struct mail_transaction_log_file *file,
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen const char *fmt, ...)
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen if (!MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)) {
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen /* indexid=0 marks the log file as corrupted */
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen mail_index_file_set_syscall_error(file->log->index,
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen "Corrupted transaction log file %s: %s",
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainenmail_transaction_log_file_alloc(struct mail_transaction_log *log,
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen file = i_new(struct mail_transaction_log_file, 1);
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainenvoid mail_transaction_log_file_free(struct mail_transaction_log_file **_file)
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen struct mail_transaction_log_file *file = *_file;
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen for (p = &file->log->files; *p != NULL; p = &(*p)->next) {
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen if (munmap(file->mmap_base, file->mmap_size) < 0) {
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen mail_index_file_set_syscall_error(file->log->index,
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen mail_index_file_set_syscall_error(file->log->index,
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainenmail_transaction_log_file_add_to_list(struct mail_transaction_log_file *file)
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen if (map != NULL && file->hdr.file_seq == map->hdr.log_file_seq &&
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen /* we can get a valid log offset from index file. initialize
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen sync_offset from it so we don't have to read the whole log
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen file from beginning. */
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen if (map->hdr.log_file_head_offset >= file->hdr.hdr_size)
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen file->sync_offset = map->hdr.log_file_head_offset;
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen "%s: log_file_head_offset too small",
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen file->saved_tail_offset = map->hdr.log_file_tail_offset;
0950aed81d1e9618264e6aa4d214d89e005ec8d6Timo Sirainen /* insert it to correct position */
0950aed81d1e9618264e6aa4d214d89e005ec8d6Timo Sirainen for (p = &log->files; *p != NULL; p = &(*p)->next) {
0950aed81d1e9618264e6aa4d214d89e005ec8d6Timo Sirainen i_assert((*p)->hdr.file_seq < file->hdr.file_seq);
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainenmail_transaction_log_init_hdr(struct mail_transaction_log *log,
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen hdr->major_version = MAIL_TRANSACTION_LOG_MAJOR_VERSION;
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen hdr->minor_version = MAIL_TRANSACTION_LOG_MINOR_VERSION;
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen hdr->hdr_size = sizeof(struct mail_transaction_log_header);
fcaf124d4a727424a338cccfd4274c2393818cd3Timo Sirainen /* not creating index - make sure we have latest header */
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen /* if we got here from mapping, the .log file is
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen corrupted. use whatever values we got from index
737561538a2dcdcda948a1da2830a612d8263a23Timo Sirainen hdr->prev_file_seq = index->map->hdr.log_file_seq;
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen hdr->prev_file_offset = index->map->hdr.log_file_head_offset;
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen hdr->file_seq = index->map->hdr.log_file_seq + 1;
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen if (log->head != NULL && hdr->file_seq <= log->head->hdr.file_seq) {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen /* make sure the sequence grows */
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainenmail_transaction_log_file_alloc_in_memory(struct mail_transaction_log *log)
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen file = mail_transaction_log_file_alloc(log, MEMORY_LOG_NAME);
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen if (mail_transaction_log_init_hdr(log, &file->hdr) < 0) {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen file->buffer = buffer_create_dynamic(default_pool, 4096);
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainenmail_transaction_log_file_dotlock(struct mail_transaction_log_file *file)
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen ret = file_dotlock_create(&file->log->dotlock_settings,
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen mail_index_file_set_syscall_error(file->log->index,
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen "file_dotlock_create()");
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen "Timeout while waiting for "
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen "dotlock for transaction log file %s",
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainenmail_transaction_log_file_undotlock(struct mail_transaction_log_file *file)
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen ret = file_dotlock_delete(&file->log->dotlock);
fcaf124d4a727424a338cccfd4274c2393818cd3Timo Sirainen mail_index_file_set_syscall_error(file->log->index,
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen "Dotlock was lost for transaction log file %s",
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainenint mail_transaction_log_file_lock(struct mail_transaction_log_file *file)
6c00502d4ece417ead501db8f0ee3e8287ba4459Timo Sirainen if (MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)) {
643a81fff9003cba13deb49a565a3c8171da524dTimo Sirainen if (file->log->index->lock_method == FILE_LOCK_METHOD_DOTLOCK)
cc5fc3c813c527ca32fa49cb96edf59b859ded20Timo Sirainen return mail_transaction_log_file_dotlock(file);
if (ret > 0) {
if (ret < 0) {
bool ignore_estale)
struct mail_transaction_log_file *f;
int ret;
if (ret < 0) {
if (ret == 0) {
bool ignore_estale)
return TRUE;
return FALSE;
const char *path2;
bool rename_existing;
if (reset)
FALSE) > 0 &&
if (reset) {
if (ret < 0)
if (rename_existing) {
path2);
bool reset)
int fd;
bool check_existing)
bool ignore_estale;
int ret;
else if (check_existing &&
if (ret > 0) {
if (ret == 0) {
i == MAIL_INDEX_ESTALE_RETRY_COUNT) {
const unsigned int offset_pos =
sizeof(sync_offset));
unsigned int trans_size)
int ret;
sizeof(*hdr));
if (ret != 0)
const void *data;
if (trans_size == 0) {
trans_size) < 0)
trans_size = 0;
if (trans_size != 0) {
void *data;
if (ret > 0) {
if (ret == 0) {
void *data;
if (ret > 0)
if (ret < 0) {
return TRUE;
return TRUE;
return FALSE;
int ret;
if (ret <= 0)
return ret;
return ret;
MADV_SEQUENTIAL) < 0) {
int ret;
if (ret > 0)
FALSE);
} while (ret == 0);
int ret;
end_offset) == 0)
*file)