mail-transaction-log-private.h revision bdd7a96c363346f7c38f389791be1487ca08775b
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen#ifndef MAIL_TRANSACTION_LOG_VIEW_H
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#define MAIL_TRANSACTION_LOG_VIEW_H
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "buffer.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "mail-transaction-log.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct dotlock_settings;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen/* Synchronization can take a while sometimes, especially when copying lots of
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mails. */
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen#define MAIL_TRANSCATION_LOG_LOCK_TIMEOUT (3*60)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#define MAIL_TRANSCATION_LOG_LOCK_CHANGE_TIMEOUT (3*60)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen/* Rotate when log is older than ROTATE_TIME and larger than MIN_SIZE */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#define MAIL_TRANSACTION_LOG_ROTATE_MIN_SIZE (1024*32)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen/* If log is larger than MAX_SIZE, rotate regardless of the time */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#define MAIL_TRANSACTION_LOG_ROTATE_MAX_SIZE (1024*1024)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#define MAIL_TRANSACTION_LOG_ROTATE_TIME (60*5)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen/* Delete .log.2 files older than this many seconds */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#define MAIL_TRANSACTION_LOG2_STALE_SECS (60*30)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#define MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file) ((file)->fd == -1)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#define LOG_FILE_MODSEQ_CACHE_SIZE 10
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct modseq_cache {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uoff_t offset;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint64_t highest_modseq;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen};
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_transaction_log_file {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_transaction_log *log;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_transaction_log_file *next;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* refcount=0 is a valid state. files start that way, and they're
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen freed only when mail_transaction_logs_clean() is called. */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen int refcount;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen char *filepath;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen int fd;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ino_t st_ino;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen dev_t st_dev;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen time_t last_mtime;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uoff_t last_size;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_transaction_log_header hdr;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen buffer_t mmap_buffer;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen buffer_t *buffer;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uoff_t buffer_offset;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen void *mmap_base;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen size_t mmap_size;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* points to the next uncommitted transaction. usually same as EOF. */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uoff_t sync_offset;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* highest modseq at sync_offset */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint64_t sync_highest_modseq;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* saved_tail_offset is the offset that was last written to transaction
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen log. max_tail_offset is what should be written to the log the next
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen time a transaction is written. transaction log handling may update
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen max_tail_offset automatically by making it skip external transactions
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen after the last saved offset (to avoid re-reading them unneededly). */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uoff_t saved_tail_offset, max_tail_offset;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* don't give warnings about saved_tail_offset shrinking if
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen sync_offset is less than this. */
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen uoff_t saved_tail_sync_offset;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen /* if we've seen _INDEX_[UN9DELETED transaction in this file,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen this is the offset. otherwise (uoff_t)-1 */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uoff_t index_deleted_offset, index_undeleted_offset;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct modseq_cache modseq_cache[LOG_FILE_MODSEQ_CACHE_SIZE];
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct file_lock *file_lock;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int locked:1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen unsigned int corrupted:1;
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen};
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_transaction_log {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_index *index;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_transaction_log_view *views;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen char *filepath, *filepath2;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* files is a linked list of all the opened log files. the list is
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen sorted by the log file sequence, so that transaction views can use
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen them easily. head contains a pointer to the newest log file. */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_transaction_log_file *files, *head;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* open_file is used temporarily while opening the log file.
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if _open() failed, it's left there for _create(). */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_transaction_log_file *open_file;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int dotlock_count;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct dotlock *dotlock;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int nfs_flush:1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen};
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvoid
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenmail_transaction_log_file_set_corrupted(struct mail_transaction_log_file *file,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *fmt, ...)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ATTR_FORMAT(2, 3);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvoid mail_transaction_log_get_dotlock_set(struct mail_transaction_log *log,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct dotlock_settings *set_r);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct mail_transaction_log_file *
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenmail_transaction_log_file_alloc_in_memory(struct mail_transaction_log *log);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenstruct mail_transaction_log_file *
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenmail_transaction_log_file_alloc(struct mail_transaction_log *log,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *path);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvoid mail_transaction_log_file_free(struct mail_transaction_log_file **file);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenint mail_transaction_log_file_open(struct mail_transaction_log_file *file,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bool check_existing);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenint mail_transaction_log_file_create(struct mail_transaction_log_file *file,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen bool reset);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_transaction_log_file_lock(struct mail_transaction_log_file *file);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_transaction_log_find_file(struct mail_transaction_log *log,
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen uint32_t file_seq, bool nfs_flush,
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen struct mail_transaction_log_file **file_r);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen/* Returns 1 if ok, 0 if file is corrupted or offset range is invalid,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen -1 if I/O error */
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainenint mail_transaction_log_file_map(struct mail_transaction_log_file *file,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen uoff_t start_offset, uoff_t end_offset);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenvoid mail_transaction_log_file_move_to_memory(struct mail_transaction_log_file
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen *file);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenvoid mail_transaction_logs_clean(struct mail_transaction_log *log);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenbool mail_transaction_log_want_rotate(struct mail_transaction_log *log);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenint mail_transaction_log_rotate(struct mail_transaction_log *log, bool reset);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenint mail_transaction_log_lock_head(struct mail_transaction_log *log);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenvoid mail_transaction_log_file_unlock(struct mail_transaction_log_file *file);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenvoid mail_transaction_update_modseq(const struct mail_transaction_header *hdr,
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen const void *data, uint64_t *cur_modseq);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenint mail_transaction_log_file_get_highest_modseq_at(
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct mail_transaction_log_file *file,
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen uoff_t offset, uint64_t *highest_modseq_r);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenint mail_transaction_log_file_get_modseq_next_offset(
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct mail_transaction_log_file *file,
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen uint64_t modseq, uoff_t *next_offset_r);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen#endif
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen