static int mail_transaction_log_refresh(struct mail_transaction_log *log, bool nfs_flush) { struct mail_transaction_log_file *file; struct stat st; i_assert(log->head != NULL); if (MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(log->head)) return 0; if (nfs_flush && log->nfs_flush) nfs_flush_file_handle_cache(log->filepath); if (nfs_safe_stat(log->filepath, &st) < 0) { if (errno != ENOENT) { mail_index_file_set_syscall_error(log->index, log->filepath, "stat()"); return -1; } /* see if the whole directory got deleted */ if (nfs_safe_stat(log->index->dir, &st) < 0 && errno == ENOENT) { log->index->index_deleted = TRUE; return -1; } /* the file should always exist at this point. if it doesn't, someone deleted it manually while the index was open. try to handle this nicely by creating a new log file. */ file = log->head; if (mail_transaction_log_create(log, FALSE) < 0) return -1; i_assert(file->refcount > 0); file->refcount--; log->index->need_recreate = TRUE; return 0; } else if (log->head->st_ino == st.st_ino && CMP_DEV_T(log->head->st_dev, st.st_dev)) { /* NFS: log files get rotated to .log.2 files instead of being unlinked, so we don't bother checking if the existing file has already been unlinked here (in which case inodes could match but point to different files) */ return 0; } file = mail_transaction_log_file_alloc(log, log->filepath); if (mail_transaction_log_file_open(file, FALSE) <= 0) { mail_transaction_log_file_free(&file); return -1; } i_assert(!file->locked); if (--log->head->refcount == 0) mail_transaction_logs_clean(log); mail_transaction_log_set_head(log, file); return 0; }
int mail_transaction_log_rotate(struct mail_transaction_log *log, bool reset) { struct mail_transaction_log_file *file; const char *path = log->head->filepath; struct stat st; int ret; i_assert(log->head->locked); if (MAIL_INDEX_IS_IN_MEMORY(log->index)) { file = mail_transaction_log_file_alloc_in_memory(log); if (reset) { file->hdr.prev_file_seq = 0; file->hdr.prev_file_offset = 0; } } else { /* we're locked, we shouldn't need to worry about ESTALE problems in here. */ if (fstat(log->head->fd, &st) < 0) { mail_index_file_set_syscall_error(log->index, log->head->filepath, "fstat()"); return -1; } file = mail_transaction_log_file_alloc(log, path); file->st_dev = st.st_dev; file->st_ino = st.st_ino; file->last_mtime = st.st_mtime; file->last_size = st.st_size; if ((ret = mail_transaction_log_file_create(file, reset)) < 0) { mail_transaction_log_file_free(&file); return -1; } if (ret == 0) { mail_index_set_error(log->index, "Transaction log %s was recreated while we had it locked - " "locking is broken (lock_method=%s)", path, file_lock_method_to_str(log->index->lock_method)); mail_transaction_log_file_free(&file); return -1; } i_assert(file->locked); } if (--log->head->refcount == 0) mail_transaction_logs_clean(log); else { /* the newly created log file is already locked */ mail_transaction_log_file_unlock(log->head, !log->index->log_sync_locked ? "rotating" : "rotating while syncing"); } mail_transaction_log_set_head(log, file); return 0; }
void mail_transaction_log_close(struct mail_transaction_log *log) { i_assert(log->views == NULL); if (log->open_file != NULL) mail_transaction_log_file_free(&log->open_file); if (log->head != NULL) log->head->refcount--; mail_transaction_logs_clean(log); i_assert(log->files == NULL); }
int mail_transaction_log_lock_head(struct mail_transaction_log *log) { struct mail_transaction_log_file *file; int ret = 0; if (!log->log_2_unlink_checked) { /* we need to check once in a while if .log.2 should be deleted to avoid wasting space on such old files. but we also don't want to waste time on checking it when the same mailbox gets opened over and over again rapidly (e.g. pop3). so do this only when there have actually been some changes to mailbox (i.e. when it's being locked here) */ log->log_2_unlink_checked = TRUE; mail_transaction_log_2_unlink_old(log); } /* we want to get the head file locked. this is a bit racy, since by the time we have it locked a new log file may have been created. creating new log file requires locking the head file, so if we can lock it and don't see another file, we can be sure no-one is creating a new log at the moment */ for (;;) { file = log->head; if (mail_transaction_log_file_lock(file) < 0) return -1; file->refcount++; ret = mail_transaction_log_refresh(log, TRUE); if (--file->refcount == 0) { mail_transaction_logs_clean(log); file = NULL; } if (ret == 0 && log->head == file) { /* success */ break; } if (file != NULL) mail_transaction_log_file_unlock(file); if (ret < 0) break; /* try again */ } return ret; }
int mail_transaction_log_rotate(struct mail_transaction_log *log, bool reset) { struct mail_transaction_log_file *file; const char *path = log->head->filepath; struct stat st; i_assert(log->head->locked); if (MAIL_INDEX_IS_IN_MEMORY(log->index)) { file = mail_transaction_log_file_alloc_in_memory(log); if (reset) { file->hdr.prev_file_seq = 0; file->hdr.prev_file_offset = 0; } } else { /* we're locked, we shouldn't need to worry about ESTALE problems in here. */ if (fstat(log->head->fd, &st) < 0) { mail_index_file_set_syscall_error(log->index, log->head->filepath, "fstat()"); return -1; } file = mail_transaction_log_file_alloc(log, path); file->st_dev = st.st_dev; file->st_ino = st.st_ino; file->last_mtime = st.st_mtime; file->last_size = st.st_size; if (mail_transaction_log_file_create(file, reset) < 0) { mail_transaction_log_file_free(&file); return -1; } } if (--log->head->refcount == 0) mail_transaction_logs_clean(log); else mail_transaction_log_file_unlock(log->head); mail_transaction_log_set_head(log, file); return 0; }
void mail_transaction_log_view_close(struct mail_transaction_log_view **_view) { struct mail_transaction_log_view *view = *_view; struct mail_transaction_log_view **p; *_view = NULL; for (p = &view->log->views; *p != NULL; p = &(*p)->next) { if (*p == view) { *p = view->next; break; } } mail_transaction_log_view_unref_all(view); mail_transaction_logs_clean(view->log); array_free(&view->file_refs); i_free(view); }
void mail_transaction_log_indexid_changed(struct mail_transaction_log *log) { struct mail_transaction_log_file *file; mail_transaction_logs_clean(log); for (file = log->files; file != NULL; file = file->next) { if (file->hdr.indexid != log->index->indexid) { mail_transaction_log_file_set_corrupted(file, "indexid changed: %u -> %u", file->hdr.indexid, log->index->indexid); } } if (log->head != NULL && log->head->hdr.indexid != log->index->indexid) { if (--log->head->refcount == 0) mail_transaction_log_file_free(&log->head); (void)mail_transaction_log_create(log, FALSE); } }
int mail_transaction_log_lock_head(struct mail_transaction_log *log, const char *lock_reason) { struct mail_transaction_log_file *file; time_t lock_wait_started, lock_secs = 0; const char *reason; int ret = 0; if (!log->log_2_unlink_checked) { /* we need to check once in a while if .log.2 should be deleted to avoid wasting space on such old files. but we also don't want to waste time on checking it when the same mailbox gets opened over and over again rapidly (e.g. pop3). so do this only when there have actually been some changes to mailbox (i.e. when it's being locked here) */ log->log_2_unlink_checked = TRUE; mail_transaction_log_2_unlink_old(log); } /* we want to get the head file locked. this is a bit racy, since by the time we have it locked a new log file may have been created. creating new log file requires locking the head file, so if we can lock it and don't see another file, we can be sure no-one is creating a new log at the moment */ lock_wait_started = time(NULL); for (;;) { file = log->head; if (mail_transaction_log_file_lock(file) < 0) return -1; file->refcount++; ret = mail_transaction_log_refresh(log, TRUE, &reason); if (--file->refcount == 0) { mail_transaction_log_file_unlock(file, t_strdup_printf( "trying to lock head for %s", lock_reason)); mail_transaction_logs_clean(log); file = NULL; } if (ret == 0 && log->head == file) { /* success */ i_assert(file != NULL); lock_secs = file->lock_created - lock_wait_started; break; } if (file != NULL) { mail_transaction_log_file_unlock(file, t_strdup_printf( "trying to lock head for %s", lock_reason)); } if (ret < 0) break; /* try again */ } if (lock_secs > MAIL_TRANSACTION_LOG_LOCK_WARN_SECS) { i_warning("Locking transaction log file %s took %ld seconds (%s)", log->head->filepath, (long)lock_secs, lock_reason); } i_assert(ret < 0 || log->head != NULL); return ret; }