]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
mdbox: Log reason when rebuilding indexes
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 3 Jul 2023 10:19:32 +0000 (13:19 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Mon, 30 Oct 2023 08:40:13 +0000 (08:40 +0000)
Already include the reason also as bitmask in preparation for a later commit
using it.

src/lib-storage/index/dbox-multi/mdbox-purge.c
src/lib-storage/index/dbox-multi/mdbox-storage-rebuild.c
src/lib-storage/index/dbox-multi/mdbox-storage-rebuild.h
src/lib-storage/index/dbox-multi/mdbox-storage.c
src/lib-storage/index/dbox-multi/mdbox-sync.c

index 1eca969677717283ea09ba00a8128e0b01a175aa..62aaf6af17d58b709ef63f8b1443dc39f2cb5a2a 100644 (file)
@@ -684,7 +684,8 @@ int mdbox_purge(struct mail_storage *_storage)
 
        if (storage->corrupted) {
                /* purging found corrupted files */
-               (void)mdbox_storage_rebuild(storage);
+               (void)mdbox_storage_rebuild(storage, NULL,
+                                           MDBOX_REBUILD_REASON_CORRUPTED);
                ret = -1;
        }
        return ret;
index d3ad4e705829e66cf728051bd38dfc069c94e2da..c4930537f4bf050f86d4e8b92a72c303d0456a64 100644 (file)
@@ -904,7 +904,10 @@ mdbox_storage_rebuild_scan_dir(struct mdbox_storage_rebuild_context *ctx,
 }
 
 static int
-mdbox_storage_rebuild_scan_prepare(struct mdbox_storage_rebuild_context *ctx)
+mdbox_storage_rebuild_scan_prepare(struct mdbox_storage_rebuild_context *ctx,
+                                  struct mailbox *fsckd_box,
+                                  enum mdbox_rebuild_reason reason,
+                                  const char **reason_string_r)
 {
        const void *data;
        size_t data_size;
@@ -917,6 +920,19 @@ mdbox_storage_rebuild_scan_prepare(struct mdbox_storage_rebuild_context *ctx)
        if (mdbox_map_atomic_lock(ctx->atomic, "mdbox storage rebuild") < 0)
                return -1;
 
+       if ((reason & MDBOX_REBUILD_REASON_FORCED) != 0)
+               *reason_string_r = "Rebuild forced";
+       else if ((reason & MDBOX_REBUILD_REASON_CORRUPTED) != 0)
+               *reason_string_r = "Storage was marked corrupted";
+       else if ((reason & MDBOX_REBUILD_REASON_MAP_FSCKD) != 0)
+               *reason_string_r = "dovecot.index.map was fsck'd";
+       else if ((reason & MDBOX_REBUILD_REASON_MAILBOX_FSCKD) != 0) {
+               *reason_string_r = t_strdup_printf(
+                       "Mailbox %s index was fsck'd", fsckd_box->vname);
+       } else {
+               i_unreached();
+       }
+
        /* fsck the map just in case its UIDs are broken */
        if (mail_index_fsck(ctx->storage->map->index) < 0) {
                mail_storage_set_index_error(&ctx->storage->storage.storage,
@@ -945,10 +961,11 @@ mdbox_storage_rebuild_scan_prepare(struct mdbox_storage_rebuild_context *ctx)
        return 1;
 }
 
-static int mdbox_storage_rebuild_scan(struct mdbox_storage_rebuild_context *ctx)
+static int mdbox_storage_rebuild_scan(struct mdbox_storage_rebuild_context *ctx,
+                                     const char *reason)
 {
        struct event *event = ctx->storage->storage.storage.event;
-       e_warning(event, "rebuilding indexes");
+       e_warning(event, "rebuilding indexes: %s", reason);
 
        if (mdbox_storage_rebuild_scan_dir(ctx, ctx->storage->storage_dir,
                                           FALSE) < 0)
@@ -970,9 +987,12 @@ static int mdbox_storage_rebuild_scan(struct mdbox_storage_rebuild_context *ctx)
 
 static int
 mdbox_storage_rebuild_in_context(struct mdbox_storage *storage,
-                                struct mdbox_map_atomic_context *atomic)
+                                struct mdbox_map_atomic_context *atomic,
+                                struct mailbox *fsckd_box,
+                                enum mdbox_rebuild_reason rebuild_reason)
 {
        struct mdbox_storage_rebuild_context *ctx;
+       const char *reason_string;
        int ret;
 
        if (dbox_verify_alt_storage(storage->map->root_list) < 0) {
@@ -984,9 +1004,11 @@ mdbox_storage_rebuild_in_context(struct mdbox_storage *storage,
        }
 
        ctx = mdbox_storage_rebuild_init(storage, atomic);
-       if ((ret = mdbox_storage_rebuild_scan_prepare(ctx)) > 0) {
+       if ((ret = mdbox_storage_rebuild_scan_prepare(ctx, fsckd_box,
+                                                     rebuild_reason,
+                                                     &reason_string)) > 0) {
                struct event_reason *reason = event_reason_begin("mdbox:rebuild");
-               ret = mdbox_storage_rebuild_scan(ctx);
+               ret = mdbox_storage_rebuild_scan(ctx, reason_string);
                event_reason_end(&reason);
        }
        mdbox_storage_rebuild_deinit(ctx);
@@ -998,13 +1020,16 @@ mdbox_storage_rebuild_in_context(struct mdbox_storage *storage,
        return ret;
 }
 
-int mdbox_storage_rebuild(struct mdbox_storage *storage)
+int mdbox_storage_rebuild(struct mdbox_storage *storage,
+                         struct mailbox *fsckd_box,
+                         enum mdbox_rebuild_reason reason)
 {
        struct mdbox_map_atomic_context *atomic;
        int ret;
 
        atomic = mdbox_map_atomic_begin(storage->map);
-       ret = mdbox_storage_rebuild_in_context(storage, atomic);
+       ret = mdbox_storage_rebuild_in_context(storage, atomic,
+                                              fsckd_box, reason);
        mdbox_map_atomic_set_success(atomic);
        mdbox_map_atomic_unset_fscked(atomic);
        if (mdbox_map_atomic_finish(&atomic) < 0)
index 087ef2cc967e7e739617859e077e9b25b24d7fe8..6e2c488811cd2b1b749ab64821633b760bb6170c 100644 (file)
@@ -1,8 +1,19 @@
 #ifndef MDBOX_STORAGE_REBUILD_H
 #define MDBOX_STORAGE_REBUILD_H
 
-struct mdbox_map_atomic_context;
+enum mdbox_rebuild_reason {
+       /* Storage was marked as corrupted earlier */
+       MDBOX_REBUILD_REASON_CORRUPTED = BIT(0),
+       /* Mailbox index was marked fsck'd */
+       MDBOX_REBUILD_REASON_MAILBOX_FSCKD = BIT(1),
+       /* dovecot.map.index was marked fsck'd */
+       MDBOX_REBUILD_REASON_MAP_FSCKD = BIT(2),
+       /* Forced rebuild (e.g. doveadm force-resync) */
+       MDBOX_REBUILD_REASON_FORCED = BIT(3),
+};
 
-int mdbox_storage_rebuild(struct mdbox_storage *storage);
+int mdbox_storage_rebuild(struct mdbox_storage *storage,
+                         struct mailbox *fsckd_box,
+                         enum mdbox_rebuild_reason reason);
 
 #endif
index 50397598a3738dce29453570d67f3f0f8a608f1f..ff0591d8bafcd77ab3c105733d7ce2e6df089e28 100644 (file)
@@ -201,8 +201,10 @@ static void mdbox_mailbox_close(struct mailbox *box)
 {
        struct mdbox_storage *mstorage = MDBOX_STORAGE(box->storage);
 
-       if (mstorage->corrupted && !mstorage->rebuilding_storage)
-               (void)mdbox_storage_rebuild(mstorage);
+       if (mstorage->corrupted && !mstorage->rebuilding_storage) {
+               (void)mdbox_storage_rebuild(mstorage, box,
+                                           MDBOX_REBUILD_REASON_CORRUPTED);
+       }
 
        dbox_mailbox_close(box);
 }
index 5b16b1a1ecf2f8586f9ee0d5c051b0024f5ce710..3bb522f7453766456a98ea9fc7eb497d8c89f75c 100644 (file)
@@ -318,14 +318,21 @@ int mdbox_sync(struct mdbox_mailbox *mbox, enum mdbox_sync_flags flags)
                mail_index_get_header(mbox->box.view);
        struct mdbox_sync_context *sync_ctx;
        struct mdbox_map_atomic_context *atomic;
+       enum mdbox_rebuild_reason rebuild_reason = 0;
        bool corrupted, storage_rebuilt = FALSE;
        int ret;
 
-       if (mbox->storage->corrupted ||
-           (hdr->flags & MAIL_INDEX_HDR_FLAG_FSCKD) != 0 ||
-           mdbox_map_is_fscked(mbox->storage->map) ||
-           (flags & MDBOX_SYNC_FLAG_FORCE_REBUILD) != 0) {
-               if (mdbox_storage_rebuild(mbox->storage) < 0)
+       if (mbox->storage->corrupted)
+               rebuild_reason |= MDBOX_REBUILD_REASON_CORRUPTED;
+       if ((hdr->flags & MAIL_INDEX_HDR_FLAG_FSCKD) != 0)
+               rebuild_reason |= MDBOX_REBUILD_REASON_MAILBOX_FSCKD;
+       if (mdbox_map_is_fscked(mbox->storage->map))
+               rebuild_reason |= MDBOX_REBUILD_REASON_MAP_FSCKD;
+       if ((flags & MDBOX_SYNC_FLAG_FORCE_REBUILD) != 0)
+               rebuild_reason |= MDBOX_REBUILD_REASON_FORCED;
+       if (rebuild_reason != 0) {
+               if (mdbox_storage_rebuild(mbox->storage, &mbox->box,
+                                         rebuild_reason) < 0)
                        return -1;
                mailbox_recent_flags_reset(&mbox->box);
                storage_rebuilt = TRUE;