diff -r f3ca0b9e3628 src/lib-index/mail-cache-compress.c
--- a/src/lib-index/mail-cache-compress.c	Sun Oct 28 14:31:13 2007 +0200
+++ b/src/lib-index/mail-cache-compress.c	Sun Oct 28 14:31:47 2007 +0200
@@ -191,6 +191,7 @@ mail_cache_copy(struct mail_cache *cache
 	hdr.indexid = cache->index->indexid;
 	hdr.file_seq = get_next_file_seq(cache, view);
 	o_stream_send(output, &hdr, sizeof(hdr));
+	i_debug("mail_cache_copy: new file_seq = %u", hdr.file_seq);
 
 	memset(&ctx, 0, sizeof(ctx));
 	ctx.cache = cache;
@@ -337,6 +338,8 @@ static int mail_cache_compress_locked(st
 	unsigned int i, count;
 	int fd, ret;
 
+	i_debug("mail_cache_compress: begin");
+
 	if (!MAIL_CACHE_IS_UNUSABLE(cache))
 		mail_cache_flush_read_cache(cache, TRUE);
 
@@ -407,6 +410,8 @@ static int mail_cache_compress_locked(st
 		(void)mail_cache_unlock(cache);
 		*unlock = FALSE;
 	}
+
+	i_debug("mail_cache_compress: done");
 
 	mail_cache_file_close(cache);
 	cache->fd = fd;
diff -r f3ca0b9e3628 src/lib-index/mail-cache-fields.c
--- a/src/lib-index/mail-cache-fields.c	Sun Oct 28 14:31:13 2007 +0200
+++ b/src/lib-index/mail-cache-fields.c	Sun Oct 28 14:31:47 2007 +0200
@@ -81,6 +81,9 @@ void mail_cache_register_fields(struct m
 
 			fields[i].idx =
 				POINTER_CAST_TO(orig_value, unsigned int);
+			i_debug("mail_cache_register_fields: "
+				"registered %s as existing %u",
+				fields[i].name, fields[i].idx);
 			(void)field_type_verify(cache, fields[i].idx,
 						fields[i].type,
 						fields[i].field_size);
@@ -96,8 +99,16 @@ void mail_cache_register_fields(struct m
 			}
 		}
 
-		if (j == i)
+		if (j == i) {
 			fields[i].idx = new_idx++;
+			i_debug("mail_cache_register_fields: "
+				"registered %s as new %u",
+				fields[i].name, fields[i].idx);
+		} else {
+			i_debug("mail_cache_register_fields: "
+				"registered %s as existing %u",
+				fields[i].name, fields[i].idx);
+		}
 	}
 
 	if (new_idx == cache->fields_count)
@@ -223,6 +234,7 @@ int mail_cache_header_fields_read(struct
 
 	if (offset == 0) {
 		/* no fields - the file is empty */
+		i_debug("mail_cache_header_fields_read: file is empty");
 		return 0;
 	}
 
@@ -341,6 +353,10 @@ int mail_cache_header_fields_read(struct
 
                 names = p + 1;
 	}
+	i_debug("mail_cache_header_fields_read: file has %d/%d fields now (map: %u, %u)",
+		cache->file_fields_count, cache->fields_count,
+		cache->file_fields_count < 1 ? 0 : cache->file_field_map[0],
+		cache->file_fields_count < 2 ? 0 : cache->file_field_map[1]);
 	return 0;
 }
 
@@ -401,6 +417,8 @@ static int mail_cache_header_fields_upda
 	uint32_t i, offset, dec_offset;
 	int ret = 0;
 
+	i_debug("mail_cache_header_fields_update: updating..");
+
 	if (mail_cache_header_fields_read(cache) < 0 ||
 	    mail_cache_header_fields_get_offset(cache, &offset) < 0)
 		return -1;
@@ -411,6 +429,8 @@ static int mail_cache_header_fields_upda
 	copy_to_buf(cache, buffer, FALSE,
 		    offsetof(struct mail_cache_field_private, last_used),
 		    sizeof(uint32_t));
+	i_debug("mail_cache_header_fields_update_locked: writing to offset %u",
+		offset + MAIL_CACHE_FIELD_LAST_USED());
 	ret = mail_cache_write(cache, buffer->data, buffer->used,
 			       offset + MAIL_CACHE_FIELD_LAST_USED());
 	if (ret == 0) {
@@ -420,6 +440,8 @@ static int mail_cache_header_fields_upda
 
 		dec_offset = offset +
 			MAIL_CACHE_FIELD_DECISION(cache->file_fields_count);
+		i_debug("mail_cache_header_fields_update_locked: "
+			"writing to offset %u", dec_offset);
 		ret = mail_cache_write(cache, buffer->data, buffer->used,
 				       dec_offset);
 		if (ret == 0) {
diff -r f3ca0b9e3628 src/lib-index/mail-cache-lookup.c
--- a/src/lib-index/mail-cache-lookup.c	Sun Oct 28 14:31:13 2007 +0200
+++ b/src/lib-index/mail-cache-lookup.c	Sun Oct 28 14:31:47 2007 +0200
@@ -182,6 +182,7 @@ int mail_cache_lookup_iter_next(struct m
 
 	i_assert(ctx->remap_counter == cache->remap_counter);
 
+	//i_debug("mail_cache_lookup_iter_next: offset=%u", ctx->offset);
 	if (ctx->pos + sizeof(uint32_t) > ctx->rec_size) {
 		if (ctx->pos != ctx->rec_size) {
 			mail_cache_set_corrupted(cache,
@@ -197,9 +198,11 @@ int mail_cache_lookup_iter_next(struct m
 	file_field = *((const uint32_t *)CONST_PTR_OFFSET(ctx->rec, ctx->pos));
 	ctx->pos += sizeof(uint32_t);
 
+	//i_debug("mail_cache_lookup_iter_next:  - file_field=%u", file_field);
 	if (file_field >= cache->file_fields_count) {
 		/* new field, have to re-read fields header to figure
 		   out its size. don't do this if we're compressing. */
+		i_debug("mail_cache_lookup_iter_next: - remap");
 		if (!cache->locked) {
 			if (mail_cache_header_fields_read(cache) < 0)
 				return -1;
diff -r f3ca0b9e3628 src/lib-index/mail-cache-transaction.c
--- a/src/lib-index/mail-cache-transaction.c	Sun Oct 28 14:31:13 2007 +0200
+++ b/src/lib-index/mail-cache-transaction.c	Sun Oct 28 14:31:47 2007 +0200
@@ -69,6 +69,8 @@ mail_cache_get_transaction(struct mail_c
 
 static void mail_cache_transaction_reset(struct mail_cache_transaction_ctx *ctx)
 {
+	i_debug("mail_cache_transaction_reset: %u -> %u", ctx->cache_file_seq,
+		ctx->cache->hdr == NULL ? 0 : ctx->cache->hdr->file_seq);
 	ctx->cache_file_seq = MAIL_CACHE_IS_UNUSABLE(ctx->cache) ? 0 :
 		ctx->cache->hdr->file_seq;
 	mail_index_ext_set_reset_id(ctx->trans, ctx->cache->ext_id,
@@ -126,8 +128,12 @@ static int mail_cache_transaction_lock(s
 	if ((ret = mail_cache_lock(ctx->cache, FALSE)) <= 0)
 		return ret;
 
-	if (ctx->cache_file_seq != ctx->cache->hdr->file_seq)
+	i_debug("mail_cache_transaction_lock: %u vs %u",
+		ctx->cache_file_seq, ctx->cache->hdr->file_seq);
+	if (ctx->cache_file_seq != ctx->cache->hdr->file_seq) {
+		i_debug("mail_cache_transaction_lock: reopened - reset");
 		mail_cache_transaction_reset(ctx);
+	}
 	return 1;
 }
 
@@ -137,6 +143,8 @@ static int mail_cache_grow_file(struct m
 	uoff_t new_fsize, grow_size;
 
 	i_assert(cache->locked);
+
+	i_debug("mail_cache_grow_file: size=%"PRIuSIZE_T, size);
 
 	/* grow the file */
 	new_fsize = cache->hdr_copy.used_file_size + size;
@@ -169,6 +177,8 @@ static bool mail_cache_unlink_hole(struc
 
 	i_assert(cache->locked);
 
+	i_debug("mail_cache_unlink_hole: offset=%u", hdr->hole_offset);
+
 	offset = hdr->hole_offset; prev_offset = 0;
 	while (offset != 0) {
 		if (pread_full(cache->fd, &hole, sizeof(hole), offset) <= 0) {
@@ -194,6 +204,7 @@ static bool mail_cache_unlink_hole(struc
 	if (prev_offset == 0)
 		hdr->hole_offset = hole.next_offset;
 	else {
+		i_debug("mail_cache_unlink_hole: writing at %u", prev_offset);
 		if (mail_cache_write(cache, &hole.next_offset,
 				     sizeof(hole.next_offset), prev_offset) < 0)
 			return FALSE;
@@ -341,6 +352,7 @@ mail_cache_free_space(struct mail_cache 
 		hole.size = size;
 		hole.magic = MAIL_CACHE_HOLE_HEADER_MAGIC;
 
+		i_debug("mail_cache_free_space: writing at %u", offset);
 		if (mail_cache_write(cache, &hole, sizeof(hole), offset) < 0)
 			return;
 
@@ -412,6 +424,7 @@ mail_cache_transaction_get_space(struct 
 
 		if (cache_file_seq != ctx->cache_file_seq) {
 			/* cache file reopened - need to abort */
+			i_debug("mail_cache_transaction_flush: aborted");
 			return 0;
 		}
 
@@ -505,6 +518,9 @@ mail_cache_transaction_flush(struct mail
 		return -1;
 
 	commit = ctx->prev_seq == 0;
+	i_debug("mail_cache_transaction_flush: commit=%d size=%"
+		PRIuSIZE_T" (map: %u, %u)", commit, ctx->cache_data->used,
+		cache->file_field_map[0], cache->file_field_map[1]);
 	if (commit) {
 		/* committing, remove the last dummy record */
 		buffer_set_used_size(ctx->cache_data, ctx->prev_pos);
@@ -548,6 +564,9 @@ mail_cache_transaction_flush(struct mail
 		}
 
 		/* write it to file */
+		i_debug("mail_cache_transaction_flush: "
+			"writing at %u, size=%"PRIuSIZE_T,
+			write_offset, max_size);
 		i_assert(ctx->cache_file_seq == cache->hdr->file_seq);
 		if (mail_cache_write(cache, rec, max_size, write_offset) < 0)
 			return -1;
@@ -595,6 +614,9 @@ mail_cache_transaction_switch_seq(struct
 			buffer_create_dynamic(default_pool,
 					      MAIL_CACHE_WRITE_BUFFER);
 		i_array_init(&ctx->cache_data_seq, 64);
+		i_debug("mail_cache_transaction_switch_seq: new data (map: %u, %u)",
+			ctx->cache->file_field_map[0],
+			ctx->cache->file_field_map[1]);
 	}
 
 	memset(&new_rec, 0, sizeof(new_rec));
@@ -609,6 +631,8 @@ int mail_cache_transaction_commit(struct
 	struct mail_cache_transaction_ctx *ctx = *_ctx;
 	struct mail_cache *cache = ctx->cache;
 	int ret = 0;
+
+	i_debug("mail_cache_transaction_commit");
 
 	if (!ctx->changes || MAIL_CACHE_IS_UNUSABLE(cache)) {
 		mail_cache_transaction_free(_ctx);
@@ -643,6 +667,8 @@ void mail_cache_transaction_rollback(str
 	struct mail_cache *cache = ctx->cache;
 	const struct mail_cache_reservation *reservations;
 	unsigned int count;
+
+	i_debug("mail_cache_transaction_rollback");
 
 	if ((ctx->reserved_space > 0 || array_count(&ctx->reservations) > 0) &&
 	    !MAIL_CACHE_IS_UNUSABLE(cache)) {
@@ -696,6 +722,10 @@ mail_cache_header_fields_write(struct ma
 	mail_cache_transaction_partial_commit(ctx, offset, size);
 
 	/* after it's guaranteed to be in disk, update header offset */
+	i_debug("mail_cache_header_fields_write: writing at %u, "
+		"size=%"PRIuSIZE_T, offset, size);
+	i_debug("mail_cache_header_fields_write: writing hdr_offset at %u",
+		hdr_offset);
 	offset = mail_index_uint32_to_offset(offset);
 	if (mail_cache_write(cache, &offset, sizeof(offset), hdr_offset) < 0)
 		return -1;
@@ -715,6 +745,8 @@ static int mail_cache_header_add_field(s
 	struct mail_cache *cache = ctx->cache;
 	buffer_t *buffer;
 	int ret;
+
+	i_debug("mail_cache_header_add_field: field_idx=%u", field_idx);
 
 	if ((ret = mail_cache_transaction_lock(ctx)) <= 0) {
 		/* create the cache file if it doesn't exist yet */
@@ -888,6 +920,8 @@ static int mail_cache_link_unlocked(stru
 				    uint32_t old_offset, uint32_t new_offset)
 {
 	new_offset += offsetof(struct mail_cache_record, prev_offset);
+	i_debug("mail_cache_link_unlocked: writing at %u: %u -> %u",
+		new_offset, old_offset, new_offset);
 	return mail_cache_write(cache, &old_offset, sizeof(old_offset),
 				new_offset);
 }
diff -r f3ca0b9e3628 src/lib-index/mail-cache.c
--- a/src/lib-index/mail-cache.c	Sun Oct 28 14:31:13 2007 +0200
+++ b/src/lib-index/mail-cache.c	Sun Oct 28 14:31:47 2007 +0200
@@ -31,10 +31,10 @@ void mail_cache_set_corrupted(struct mai
 {
 	va_list va;
 
-	(void)unlink(cache->filepath);
+	//(void)unlink(cache->filepath);
 
 	/* mark the cache as unusable */
-	cache->hdr = NULL;
+	//cache->hdr = NULL;
 
 	va_start(va, fmt);
 	t_push();
@@ -42,6 +42,8 @@ void mail_cache_set_corrupted(struct mai
 			     cache->filepath, t_strdup_vprintf(fmt, va));
 	t_pop();
 	va_end(va);
+	sleep(1000);
+	abort();
 }
 
 void mail_cache_file_close(struct mail_cache *cache)
@@ -120,6 +122,8 @@ int mail_cache_reopen(struct mail_cache 
 
 	if (mail_cache_header_fields_read(cache) < 0)
 		return -1;
+	i_debug("mail_cache_reopen: re-opened, file_seq=%u",
+		cache->hdr->file_seq);
 
 	view = mail_index_view_open(cache->index);
 	ext = mail_index_view_get_ext(view, cache->ext_id);
@@ -269,6 +273,7 @@ int mail_cache_map(struct mail_cache *ca
 		return -1;
 	}
 
+	i_debug("mail_cache_map: re-mmaped");
 	return 0;
 }
 
@@ -476,11 +481,15 @@ int mail_cache_lock(struct mail_cache *c
 		return 0;
 	}
 
+	i_debug("mail_cache_lock: locking");
+
 	if (cache->hdr->file_seq != reset_id) {
 		/* we want the latest cache file */
 		ret = mail_cache_reopen(cache);
-		if (ret < 0 || (ret == 0 && require_same_reset_id))
+		if (ret < 0 || (ret == 0 && require_same_reset_id)) {
+			i_debug("mail_cache_lock: failed");
 			return ret;
+		}
 	}
 
 	for (i = 0; i < 3; i++) {
@@ -501,6 +510,7 @@ int mail_cache_lock(struct mail_cache *c
 			break;
 		ret = 0;
 	}
+	i_debug("mail_cache_lock: finished, ret = %d", ret);
 
 	if (ret > 0) {
 		/* make sure our header is up to date */
@@ -574,6 +584,7 @@ int mail_cache_unlock(struct mail_cache 
 	}
 
 	mail_cache_unlock_file(cache);
+	i_debug("mail_cache_unlock: unlocked");
 	return ret;
 }
 
