diff -r 1171265c3834 src/lib-storage/index/mbox/mbox-sync-rewrite.c
--- a/src/lib-storage/index/mbox/mbox-sync-rewrite.c	Wed May 13 19:26:45 2015 +0300
+++ b/src/lib-storage/index/mbox/mbox-sync-rewrite.c	Wed May 13 19:28:20 2015 +0300
@@ -189,6 +189,8 @@
 	*size = 0;
 
 	if (ctx->mail.space < (off_t)(data_size - last_line_pos)) {
+		i_debug("mbox_sync_header_remove_space(%d) space(%d) = %d-%d", ctx->sync_ctx->seq,
+			(int)ctx->mail.space, (int)data_size, (int)last_line_pos);
 		ctx->mail.space = data_size - last_line_pos;
 		ctx->mail.offset = ctx->hdr_offset;
 		if (ctx->mail.space > 0)
@@ -253,9 +255,13 @@
 	if (new_hdr_size <= old_hdr_size) {
 		/* add space. note that we must call add_space() even if we're
 		   not adding anything so mail.offset gets fixed. */
+		i_debug("mbox_sync_try_rewrite(%d): add space %ld",
+			ctx->sync_ctx->seq, old_hdr_size - new_hdr_size);
 		mbox_sync_headers_add_space(ctx, old_hdr_size - new_hdr_size);
 	} else if (new_hdr_size > old_hdr_size) {
 		/* try removing the space where we can */
+		i_debug("mbox_sync_try_rewrite(%d): remove space %ld",
+			ctx->sync_ctx->seq, new_hdr_size - old_hdr_size);
 		mbox_sync_headers_remove_space(ctx,
 					       new_hdr_size - old_hdr_size);
 		new_hdr_size = str_len(ctx->header);
@@ -263,6 +269,7 @@
 		if (new_hdr_size <= old_hdr_size) {
 			/* good, we removed enough. */
 			i_assert(new_hdr_size == old_hdr_size);
+			i_debug(" - have enough");
 		} else if (move_diff < 0 &&
 			   new_hdr_size - old_hdr_size <= (uoff_t)-move_diff) {
 			/* moving backwards - we can use the extra space from
@@ -270,12 +277,16 @@
 			i_assert(ctx->mail.space == 0);
 			i_assert(sync_ctx->expunged_space >=
 				 (off_t)(new_hdr_size - old_hdr_size));
+			i_debug(" - removing expunged space %lu -> %lu", sync_ctx->expunged_space,
+			       sync_ctx->expunged_space - (new_hdr_size - old_hdr_size));
 			sync_ctx->expunged_space -= new_hdr_size - old_hdr_size;
 		} else {
 			/* couldn't get enough space */
 			i_assert(ctx->mail.space == 0);
 			ctx->mail.space =
 				-(ssize_t)(new_hdr_size - old_hdr_size);
+			i_debug("mbox_sync_try_rewrite(%d) need more space=%d, move_diff=%ld expunged=%ld",
+				ctx->sync_ctx->seq, (int)ctx->mail.space, move_diff, sync_ctx->expunged_space);
 			return 0;
 		}
 	}
@@ -410,10 +421,15 @@
 		mail_ctx = &new_mail_ctx;
 	} else {
 		i_assert(seq == mail_ctx->seq);
-		if (mail_ctx->mail.space < 0)
+		if (mail_ctx->mail.space < 0) {
+			i_debug("mbox_sync_try_rewrite(%d) space=%u",
+				sync_ctx->seq, (int)mail_ctx->mail.space);
 			mail_ctx->mail.space = 0;
+		}
 		i_stream_seek(sync_ctx->input, mail_ctx->body_offset);
 	}
+	i_debug("mbox_sync_read_and_move(%d): initially idx=%d padding=%d space=%d",
+		sync_ctx->seq, idx, padding, (int)mail_ctx->mail.space);
 
 	if (mail_ctx->mail.space <= 0) {
 		need_space = str_len(mail_ctx->header) - mail_ctx->mail.space -
@@ -480,6 +496,8 @@
 
 	mails[idx].offset = dest_offset +
 		(mail_ctx->mail.offset - mail_ctx->hdr_offset);
+	i_debug("mbox_sync_try_rewrite(%d) final: idx=%d space=%d",
+		sync_ctx->seq, idx, (int)mail_ctx->mail.space);
 	mails[idx].space = mail_ctx->mail.space;
 	return 0;
 }
@@ -500,6 +518,9 @@
 	i_assert(extra_space < OFF_T_MAX);
 	i_assert(sync_ctx->mbox->mbox_lock_type == F_WRLCK);
 
+	i_debug("mbox_sync_rewrite(seq=%d..%d): move_diff=%ld extra_space=%lu end_offset=%lu",
+		first_seq, last_seq, move_diff, extra_space, end_offset);
+
 	mails = array_get_modifiable(&sync_ctx->mails, &count);
 	i_assert(count == last_seq - first_seq + 1);
 
@@ -577,6 +598,8 @@
 			if (!mails[idx].expunged) {
 				move_diff -= padding_per_mail;
 				mails[idx].space = padding_per_mail;
+				i_debug("mbox_fill_space(%d): idx=%d space=%d",
+					sync_ctx->seq, idx, (int)mails[idx].space);
 
 				if (mbox_fill_space(sync_ctx, move_diff +
 						    mails[idx].offset,
@@ -600,7 +623,6 @@
 		i_assert(mails[idx].from_offset == start_offset);
 		i_assert(move_diff + (off_t)expunged_space >= 0);
 	}
-
 	mbox_sync_file_updated(sync_ctx, FALSE);
 	sync_ctx->prev_msg_uid = orig_prev_msg_uid;
 	return ret;
diff -r 1171265c3834 src/lib-storage/index/mbox/mbox-sync.c
--- a/src/lib-storage/index/mbox/mbox-sync.c	Wed May 13 19:26:45 2015 +0300
+++ b/src/lib-storage/index/mbox/mbox-sync.c	Wed May 13 19:28:20 2015 +0300
@@ -665,7 +665,12 @@
 		i_assert(mail_ctx->mail.from_offset + move_diff != 1 &&
 			 mail_ctx->mail.from_offset + move_diff != 2);
 
+		int old_size = str_len(mail_ctx->header);
+		i_debug("mbox_sync_handle_header(%d): old size=%lu (expunged space)", sync_ctx->seq, str_len(mail_ctx->header));
 		mbox_sync_update_header(mail_ctx);
+		i_debug("mbox_sync_handle_header(%d): new size=%lu diff=%ld (%ld) (expunged space)", sync_ctx->seq, str_len(mail_ctx->header),
+			str_len(mail_ctx->header)-(mail_ctx->body_offset - mail_ctx->hdr_offset),
+			str_len(mail_ctx->header)-old_size);
 		ret = mbox_sync_try_rewrite(mail_ctx, move_diff);
 		if (ret < 0)
 			return -1;
@@ -689,10 +694,13 @@
 		}
 	} else if (mail_ctx->need_rewrite) {
 		mbox_sync_update_header(mail_ctx);
+		i_debug("mbox_sync_handle_header(%d): new size=%lu diff=%ld (need_rewrite)", sync_ctx->seq, str_len(mail_ctx->header),
+			str_len(mail_ctx->header)-(mail_ctx->body_offset - mail_ctx->hdr_offset));
 		if (sync_ctx->delay_writes && sync_ctx->need_space_seq == 0) {
 			/* mark it dirty and do it later. we can't do this
 			   if we're in the middle of rewriting acquiring more
 			   space. */
+			i_debug(" - mark dirty");
 			mail_ctx->dirty = TRUE;
 			return 0;
 		}
@@ -732,6 +740,8 @@
 			i_assert(sync_ctx->space_diff < -mail_ctx->mail.space);
 
 			sync_ctx->need_space_seq--;
+			i_debug("added dummy space seq=%u space=%ld",
+				sync_ctx->need_space_seq + array_count(&sync_ctx->mails), mail.space);
 			array_append(&sync_ctx->mails, &mail, 1);
 		}
 	}
@@ -758,8 +768,13 @@
 		array_append_array(&keywords_copy, &mail_ctx->mail.keywords);
 		mail_ctx->mail.keywords = keywords_copy;
 	}
+	i_debug("mbox_sync_handle_missing_space(%d) space_diff=%ld",
+		sync_ctx->need_space_seq + array_count(&sync_ctx->mails),
+		mail_ctx->mail.space);
 	array_append(&sync_ctx->mails, &mail_ctx->mail, 1);
 
+	i_debug("mbox_sync_handle_missing_space() sync_ctx.space_diff(%ld) += %ld",
+		sync_ctx->space_diff, mail_ctx->mail.space);
 	sync_ctx->space_diff += mail_ctx->mail.space;
 	if (sync_ctx->space_diff < 0) {
 		if (sync_ctx->expunged_space > 0) {
@@ -1328,6 +1343,10 @@
 		return 0;
 	}
 
+	i_debug("mbox_sync_handle_eof_updates(): need_space_seq=%d, offset=%ld, file_size=%ld",
+		sync_ctx->need_space_seq, sync_ctx->file_input->v_offset,
+	       file_size);
+
 	if (file_size < sync_ctx->file_input->v_offset) {
 		mbox_sync_set_critical(sync_ctx,
 			"file size unexpectedly shrank "
@@ -1355,6 +1374,7 @@
 
 		i_assert(sync_ctx->space_diff < 0);
 
+		i_debug(" - appending %ld bytes", -sync_ctx->space_diff);
 		if (mbox_append_zero(sync_ctx, file_size,
 				     -sync_ctx->space_diff) < 0)
 			return -1;
