1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#ifndef MDBOX_STORAGE_H
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define MDBOX_STORAGE_H
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "index-storage.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "dbox-storage.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "mdbox-settings.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define MDBOX_STORAGE_NAME "mdbox"
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen#define MDBOX_DELETED_STORAGE_NAME "mdbox_deleted"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define MDBOX_GLOBAL_INDEX_PREFIX "dovecot.map.index"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define MDBOX_GLOBAL_DIR_NAME "storage"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define MDBOX_MAIL_FILE_PREFIX "m."
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define MDBOX_MAIL_FILE_FORMAT MDBOX_MAIL_FILE_PREFIX"%u"
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen#define MDBOX_MAX_OPEN_UNUSED_FILES 2
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen#define MDBOX_CLOSE_UNUSED_FILES_TIMEOUT_SECS 30
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define MDBOX_INDEX_HEADER_MIN_SIZE (sizeof(uint32_t))
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct mdbox_index_header {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t map_uid_validity;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_128_t mailbox_guid;
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen uint8_t flags; /* enum dbox_index_header_flags */
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen uint8_t unused[3];
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct mdbox_storage {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_storage storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const struct mdbox_settings *set;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* paths for storage directories */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const char *storage_dir, *alt_storage_dir;
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen struct mdbox_map *map;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mdbox_file *) open_files;
04c3ac276103b56185119bcff9a66de7a8bb0e68Timo Sirainen struct timeout *to_close_unused_files;
1dd054126238349e1a7d3d1ffe7f8bc5fdbacb7aTimo Sirainen
5fb3f13537dffd15a31e997da133a721c0728af8Timo Sirainen ARRAY_TYPE(uint32_t) move_to_alt_map_uids;
5fb3f13537dffd15a31e997da133a721c0728af8Timo Sirainen ARRAY_TYPE(uint32_t) move_from_alt_map_uids;
5fb3f13537dffd15a31e997da133a721c0728af8Timo Sirainen
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen /* if non-zero, storage should be rebuilt (except if rebuild_count
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen has changed from this value) */
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen uint32_t corrupted_rebuild_count;
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool corrupted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool rebuilding_storage:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool preallocate_space:1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct mdbox_mail_index_record {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t map_uid;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* UNIX timestamp of when the message was saved/copied to this
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mailbox */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t save_date;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct mdbox_mailbox {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct mailbox box;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct mdbox_storage *storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t map_uid_validity;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t ext_id, hdr_ext_id, guid_ext_id;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool mdbox_deleted_synced:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool creating:1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek#define MDBOX_DBOX_STORAGE(s) container_of(s, struct mdbox_storage, storage)
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek#define MDBOX_STORAGE(s) MDBOX_DBOX_STORAGE(DBOX_STORAGE(s))
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek#define MDBOX_MAILBOX(s) container_of(s, struct mdbox_mailbox, box)
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenextern struct dbox_storage_vfuncs mdbox_dbox_storage_vfuncs;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenextern struct mail_vfuncs mdbox_mail_vfuncs;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_mail_open(struct dbox_mail *mail, uoff_t *offset_r,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct dbox_file **file_r);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* Get map_uid for wanted message. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_mail_lookup(struct mdbox_mailbox *mbox, struct mail_index_view *view,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t seq, uint32_t *map_uid_r);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenuint32_t dbox_get_uidvalidity_next(struct mailbox_list *list);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_read_header(struct mdbox_mailbox *mbox,
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen struct mdbox_index_header *hdr, bool *need_resize_r);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid mdbox_update_header(struct mdbox_mailbox *mbox,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct mail_index_transaction *trans,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen const struct mailbox_update *update) ATTR_NULL(3);
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainenint mdbox_mailbox_create_indexes(struct mailbox *box,
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen const struct mailbox_update *update,
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen struct mail_index_transaction *trans);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct mail_save_context *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenmdbox_save_alloc(struct mailbox_transaction_context *_t);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_save_begin(struct mail_save_context *ctx, struct istream *input);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_save_finish(struct mail_save_context *ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid mdbox_save_cancel(struct mail_save_context *ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct dbox_file *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenmdbox_save_file_get_file(struct mailbox_transaction_context *t,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t seq, uoff_t *offset_r);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_transaction_save_commit_pre(struct mail_save_context *ctx);
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainenvoid mdbox_transaction_save_commit_post(struct mail_save_context *ctx,
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen struct mail_index_transaction_commit_result *result);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenvoid mdbox_transaction_save_rollback(struct mail_save_context *ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_copy(struct mail_save_context *ctx, struct mail *mail);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
5fb3f13537dffd15a31e997da133a721c0728af8Timo Sirainenvoid mdbox_purge_alt_flag_change(struct mail *mail, bool move_to_alt);
5fb3f13537dffd15a31e997da133a721c0728af8Timo Sirainenint mdbox_purge(struct mail_storage *storage);
5fb3f13537dffd15a31e997da133a721c0728af8Timo Sirainen
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenint mdbox_storage_create(struct mail_storage *_storage,
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen struct mail_namespace *ns, const char **error_r);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenvoid mdbox_storage_destroy(struct mail_storage *_storage);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenint mdbox_mailbox_open(struct mailbox *box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainen
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainenvoid mdbox_storage_set_corrupted(struct mdbox_storage *storage);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenvoid mdbox_set_mailbox_corrupted(struct mailbox *box);
b90c23a9862b91594959b918b035d73f7bc0b265Timo Sirainenvoid mdbox_set_file_corrupted(struct dbox_file *file);
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#endif