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