]> 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)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 31 Jan 2024 11:38:53 +0000 (13:38 +0200)
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 8461074fb13d73a532c0c0565b77c9ae9194c282..195eeec8977c8b8945b2a488c90d7a3cd6d2a6a6 100644 (file)
@@ -683,7 +683,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 56e5608a7846f2562614f330bec2e19a78a8375f..824e85d7543f357b14f5781eed0f6af77d483363 100644 (file)
@@ -900,7 +900,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;
@@ -913,6 +916,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,
@@ -939,9 +955,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)
 {
-       i_warning("mdbox %s: rebuilding indexes", ctx->storage->storage_dir);
+       i_warning("mdbox %s: rebuilding indexes: %s",
+                 ctx->storage->storage_dir, reason);
 
        if (mdbox_storage_rebuild_scan_dir(ctx, ctx->storage->storage_dir,
                                           FALSE) < 0)
@@ -963,9 +981,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) {
@@ -977,9 +998,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);
@@ -991,13 +1014,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 d1f2d156f5a214c8e1a791314c5118b7e4bc7047..a0501430db959439e1b208e67fbb396b47055cb1 100644 (file)
@@ -197,8 +197,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;