bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen Expunging works like:
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen 1. Lock map index by beginning a map sync.
65b94e73c305dcb209cf958f938b93ec061c67a9Timo Sirainen 2. Write map UID refcount changes to map index (=> tail != head).
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen 3. Expunge messages from mailbox index.
65b94e73c305dcb209cf958f938b93ec061c67a9Timo Sirainen 4. Finish map sync, which updates tail=head and unlocks map index.
65b94e73c305dcb209cf958f938b93ec061c67a9Timo Sirainen If something crashes after 2 but before 4 is finished, tail != head and
c977374bd4651cafc1626ebe308aa66dfd8b30e0Timo Sirainen reader can do a full resync to figure out what got broken.
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainendbox_sync_verify_expunge_guid(struct mdbox_sync_context *ctx, uint32_t seq,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_lookup_uid(ctx->sync_view, seq, &uid);
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "Expunged GUID mismatch for UID %u: %s vs %s",
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi uid, guid_128_to_string(data), guid_128_to_string(guid_128));
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen mdbox_storage_set_corrupted(ctx->mbox->storage);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int mdbox_sync_expunge(struct mdbox_sync_context *ctx, uint32_t seq,
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainen if (seq_range_array_add(&ctx->expunged_seqs, seq)) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* already marked as expunged in this sync */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (dbox_sync_verify_expunge_guid(ctx, seq, guid_128) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (mdbox_mail_lookup(ctx->mbox, ctx->sync_view, seq, &map_uid) < 0)
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen if (mdbox_map_update_refcount(ctx->map_trans, map_uid, -1) < 0)
73a87c2ff65c6116cde6fb158dfddb8ef7346901Timo Sirainenstatic int mdbox_sync_rec(struct mdbox_sync_context *ctx,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (sync_rec->type != MAIL_INDEX_SYNC_TYPE_EXPUNGE) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* not interested */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!mail_index_lookup_seq_range(ctx->sync_view,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* already expunged everything. nothing to do. */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (mdbox_sync_expunge(ctx, seq, sync_rec->guid_128) < 0)
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainenstatic int dbox_sync_mark_expunges(struct mdbox_sync_context *ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int n;
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen /* use a separate transaction here so that we can commit the changes
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen during map transaction */
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen trans = mail_index_transaction_begin(ctx->sync_view, flags);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_iter_init(&iter, &ctx->expunged_seqs); n = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen while (seq_range_array_iter_nth(&iter, n++, &seq)) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_lookup_uid(ctx->sync_view, seq, &uid);
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen if (mail_index_transaction_commit(&trans) < 0)
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen /* do notifications after commit finished successfully */
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen seq_range_array_iter_init(&iter, &ctx->expunged_seqs); n = 0;
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen while (seq_range_array_iter_nth(&iter, n++, &seq)) {
637455ebee0453f860c9bce0626c485e35fb83deTimo Sirainen mail_index_lookup_uid(ctx->sync_view, seq, &uid);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen box->v.sync_notify(box, uid, MAILBOX_SYNC_TYPE_EXPUNGE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int mdbox_sync_index(struct mdbox_sync_context *ctx)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* newly created index file */
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen /* could be just a race condition where we opened the
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen mailbox between mkdir and index creation. fix this
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen if (mdbox_mailbox_create_indexes(box, NULL, ctx->trans) < 0)
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(box, "Broken index: missing UIDVALIDITY");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* mark the newly seen messages as recent */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid,
817d027593510c3ba70ad542ce0011f5f6916d1eTimo Sirainen mailbox_recent_flags_set_seqs(&ctx->mbox->box, ctx->sync_view,
65b94e73c305dcb209cf958f938b93ec061c67a9Timo Sirainen /* handle syncing records without map being locked. */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (mdbox_map_atomic_is_locked(ctx->atomic)) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen ctx->map_trans = mdbox_map_transaction_begin(ctx->atomic, FALSE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) {
73a87c2ff65c6116cde6fb158dfddb8ef7346901Timo Sirainen if ((ret = mdbox_sync_rec(ctx, &sync_rec)) < 0)
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* write refcount changes to map index. transaction commit updates the
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen log head, while tail is left behind. */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (mdbox_map_atomic_is_locked(ctx->atomic)) {
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen ret = mdbox_map_transaction_commit(ctx->map_trans, "mdbox syncing");
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* write changes to mailbox index */
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* finish the map changes and unlock the map. this also updates
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen map's tail -> head. */
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen ctx->expunged_count = seq_range_count(&ctx->expunged_seqs);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainenstatic int mdbox_sync_try_begin(struct mdbox_sync_context *ctx,
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen ret = index_storage_expunged_sync_begin(&mbox->box, &ctx->index_sync_ctx,
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (!mdbox_map_atomic_is_locked(ctx->atomic) &&
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen mail_index_sync_has_expunges(ctx->index_sync_ctx)) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* we have expunges, so we need to write to map.
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen it needs to be locked before mailbox index. */
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen mail_index_sync_set_reason(ctx->index_sync_ctx, "mdbox expunge check");
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen index_storage_expunging_deinit(&ctx->mbox->box);
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen if (mdbox_map_atomic_lock(ctx->atomic, "mdbox syncing with expunges") < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_sync_begin(struct mdbox_mailbox *mbox, enum mdbox_sync_flags flags,
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen /* avoid race conditions with mailbox creation, don't check for dbox
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen headers until syncing has locked the mailbox */
96088ba9ef3b6c113a0a41f3778cd38f437ddc3dTimo Sirainen (hdr->flags & MAIL_INDEX_HDR_FLAG_FSCKD) != 0 ||
f71c2d4e6b802bf8e622bcd5df29286262d05d5aTimo Sirainen if (rebuild && (flags & MDBOX_SYNC_FLAG_NO_REBUILD) == 0) {
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen if (mdbox_storage_rebuild_in_context(mbox->storage, atomic) < 0)
3f91e60401495a4046c73992fabaa5e77200a451Timo Sirainen sync_flags = index_storage_get_sync_flags(&mbox->box);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!rebuild && (flags & MDBOX_SYNC_FLAG_FORCE) == 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_REQUIRE_CHANGES;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* don't write unnecessary dirty flag updates */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sync_flags |= MAIL_INDEX_SYNC_FLAG_AVOID_FLAG_UPDATES;
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* failed / nothing to do */
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen mail_index_sync_set_reason(ctx->index_sync_ctx,
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen "mdbox syncing found corruption");
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* corrupted */
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "mdbox: Storage keeps breaking");
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen /* we'll need to rebuild storage.
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen try again from the beginning. */
37ee89f3cba22cd975912a882f0d3097fa5031e1Timo Sirainen if ((flags & MDBOX_SYNC_FLAG_NO_REBUILD) != 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "mdbox: Can't rebuild storage");
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen return mdbox_sync_begin(mbox, flags, atomic, ctx_r);
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen /* may be 0 msgs, but that still informs that the map
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen was locked */
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen reason = t_strdup_printf("mdbox synced - %u msgs expunged",
62958c5eefcd7dd84717b487ca36ec3a86949eb9Timo Sirainen mail_index_sync_set_reason(ctx->index_sync_ctx, reason);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_sync_finish(struct mdbox_sync_context **_ctx, bool success)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (mail_index_sync_commit(&ctx->index_sync_ctx) < 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_sync_rollback(&ctx->index_sync_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint mdbox_sync(struct mdbox_mailbox *mbox, enum mdbox_sync_flags flags)
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen atomic = mdbox_map_atomic_begin(mbox->storage->map);
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen ret = mdbox_sync_begin(mbox, flags, atomic, &sync_ctx);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenmdbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek struct mdbox_mailbox *mbox = MDBOX_MAILBOX(box);
e098ac1cd4ff0afb33b22cc7fd288014f4fdb0bcTimo Sirainen if (index_mailbox_want_full_sync(&mbox->box, flags) ||
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FORCE_RESYNC) != 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mdbox_sync_flags |= MDBOX_SYNC_FLAG_FORCE_REBUILD;