mail-cache.c revision e8fd7988ec183fb6c104aed19a61f1a096c51d34
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2003-2010 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_cache_set_syscall_error(struct mail_cache *cache,
e8fd7988ec183fb6c104aed19a61f1a096c51d34Timo Sirainen mail_index_file_set_syscall_error(cache->index, cache->filepath,
6bc0f424bcdb9119d8159874cf98adfa53eefd9aTimo Sirainenstatic void mail_cache_unlink(struct mail_cache *cache)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_cache_set_corrupted(struct mail_cache *cache, const char *fmt, ...)
ad850190d946d34966a56838cfdb216e021b5b5fTimo Sirainen /* mark the cache as unusable */
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen "Corrupted index cache file %s: %s",
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainenvoid mail_cache_file_close(struct mail_cache *cache)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (munmap(cache->mmap_base, cache->mmap_length) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_syscall_error(cache, "munmap()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_syscall_error(cache, "close()");
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainenstatic void mail_cache_init_file_cache(struct mail_cache *cache)
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen file_cache_set_fd(cache->file_cache, cache->fd);
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen file_cache_set_size(cache->file_cache, st.st_size);
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen mail_cache_set_syscall_error(cache, "fstat()");
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainenstatic bool mail_cache_need_reopen(struct mail_cache *cache)
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen /* we're waiting for compression */
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen /* disabled */
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen /* see if the file has changed */
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen if ((cache->index->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) != 0) {
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen if (nfs_safe_stat(cache->filepath, &st) < 0) {
69e03a846f6980144aa75bff0590c04852bffbbcTimo Sirainen mail_cache_set_syscall_error(cache, "stat()");
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen /* file changed */
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen if ((cache->index->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) != 0) {
2131ef7a3390f15ea6a958256ea54908f1096350Timo Sirainen /* if the old file has been deleted, the new file may have
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen the same inode as the old one. we'll catch this here by
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen checking if fstat() fails with ESTALE */
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen mail_cache_set_syscall_error(cache, "fstat()");
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainenint mail_cache_reopen(struct mail_cache *cache)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* reopening does no good */
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen mail_cache_set_syscall_error(cache, "open()");
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen ext = mail_index_view_get_ext(view, cache->ext_id);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (ext == NULL || cache->hdr->file_seq != ext->reset_id) {
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen /* still different - maybe a race condition or maybe the
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen file_seq really is corrupted. either way, this shouldn't
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen happen often so we'll just mark cache to be compressed
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen later which fixes this. */
58be9d6bcc3800f5b3d76a064ee767fbe31a5a8aTimo Sirainen cache->need_compress_file_seq = cache->hdr->file_seq;
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainenstatic void mail_cache_update_need_compress(struct mail_cache *cache)
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen const struct mail_cache_header *hdr = cache->hdr;
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen cont_percentage = hdr->continued_record_count * 100 /
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen (cache->index->map->rec_map->records_count == 0 ? 1 :
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen if (cont_percentage >= MAIL_CACHE_COMPRESS_CONTINUED_PERCENTAGE &&
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen hdr->used_file_size >= MAIL_CACHE_COMPRESS_MIN_SIZE) {
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen /* too many continued rows, compress */
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen cache->need_compress_file_seq = hdr->file_seq;
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen /* see if we've reached the max. deleted space in file */
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen hdr->used_file_size >= MAIL_CACHE_COMPRESS_MIN_SIZE)
25c22e54d1071d120641e9eecd0023e7373e65ffTimo Sirainen cache->need_compress_file_seq = hdr->file_seq;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenstatic bool mail_cache_verify_header(struct mail_cache *cache)
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen const struct mail_cache_header *hdr = cache->data;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* check that the header is still ok */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (cache->mmap_length < sizeof(struct mail_cache_header)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_corrupted(cache, "File too small");
a40d26f83af808a0ea1e212c001d682a96d870b0Timo Sirainen /* version changed - upgrade silently */
e192a3b1ca8ae857e7d87298ea507d32977ba570Timo Sirainen if (hdr->compat_sizeof_uoff_t != sizeof(uoff_t)) {
e192a3b1ca8ae857e7d87298ea507d32977ba570Timo Sirainen /* architecture change - handle silently(?) */
c4877db8b6559846f4b58be8e42422dc734c193fTimo Sirainen /* index id changed - handle silently */
8b9342aa96b2f297e23afb261f9f7dd859800952Timo Sirainen mail_cache_set_corrupted(cache, "file_seq is 0");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* only check the header if we're locked */
a40d26f83af808a0ea1e212c001d682a96d870b0Timo Sirainen if (hdr->used_file_size < sizeof(struct mail_cache_header)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_corrupted(cache, "used_file_size too small");
a40d26f83af808a0ea1e212c001d682a96d870b0Timo Sirainen if ((hdr->used_file_size % sizeof(uint32_t)) != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_corrupted(cache, "used_file_size not aligned");
56f45b3f3ae20e5c933701f4657dda5ef1916855Timo Sirainen mail_cache_set_corrupted(cache, "used_file_size too large");
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainenint mail_cache_map(struct mail_cache *cache, size_t offset, size_t size)
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen ret = file_cache_read(cache->file_cache, offset, size);
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen /* In case of ESTALE we'll simply fail without error
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen messages. The caller will then just have to
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen fallback to generating the value itself.
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen We can't simply reopen the cache flie, because
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen using it requires also having updated file
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen mail_cache_set_syscall_error(cache, "read()");
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen cache->data = file_cache_get_map(cache->file_cache,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* already mapped */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (munmap(cache->mmap_base, cache->mmap_length) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_syscall_error(cache, "munmap()");
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* unusable, waiting for compression or
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index is in memory */
58be9d6bcc3800f5b3d76a064ee767fbe31a5a8aTimo Sirainen i_assert(cache->need_compress_file_seq != 0 ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* map the whole file */
72cbf33ae81fde08384d30c779ff540752d9256cTimo Sirainen cache->mmap_base = mmap_ro_file(cache->fd, &cache->mmap_length);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_syscall_error(cache, "mmap()");
6a8a4c9f530668cd8961b73d702856ed94f05f80Timo Sirainenstatic int mail_cache_try_open(struct mail_cache *cache)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_cache_set_syscall_error(cache, "open()");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (mail_cache_map(cache, 0, sizeof(struct mail_cache_header)) < 0)
6a8a4c9f530668cd8961b73d702856ed94f05f80Timo Sirainenint mail_cache_open_and_verify(struct mail_cache *cache)
6a8a4c9f530668cd8961b73d702856ed94f05f80Timo Sirainen /* failed for some reason - doesn't really matter,
6a8a4c9f530668cd8961b73d702856ed94f05f80Timo Sirainen it's disabled for now. */
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainenstatic struct mail_cache *mail_cache_alloc(struct mail_index *index)
a835194f9a9dae88528367a791cbc282589f6c01Timo Sirainen i_strconcat(index->filepath, MAIL_CACHE_FILE_SUFFIX, NULL);
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen cache->field_pool = pool_alloconly_create("Cache fields", 1024);
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen hash_table_create(default_pool, cache->field_pool, 0,
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen strcase_hash, (hash_cmp_callback_t *)strcasecmp);
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen (index->flags & MAIL_INDEX_OPEN_FLAG_DOTLOCK_USE_EXCL) != 0;
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen (index->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) != 0;
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen cache->dotlock_settings.timeout = MAIL_CACHE_LOCK_TIMEOUT;
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen cache->dotlock_settings.stale_timeout = MAIL_CACHE_LOCK_CHANGE_TIMEOUT;
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen (index->flags & MAIL_INDEX_OPEN_FLAG_MMAP_DISABLE) != 0)
59151b71059df1190acd75d8717ed04a7920c862Timo Sirainen mail_index_register_expunge_handler(index, cache->ext_id, FALSE,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mail_index_register_sync_handler(index, cache->ext_id,
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainenstruct mail_cache *mail_cache_open_or_create(struct mail_index *index)
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainenstruct mail_cache *mail_cache_create(struct mail_index *index)
5196f9ea42d02000f9c3d22f20aa816140af4422Timo Sirainen if (unlink(cache->filepath) < 0 && errno != ENOENT)
5196f9ea42d02000f9c3d22f20aa816140af4422Timo Sirainen mail_cache_set_syscall_error(cache, "unlink()");
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid mail_cache_free(struct mail_cache **_cache)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_index_unregister_sync_lost_handler(cache->index,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_index_unregister_expunge_handler(cache->index, cache->ext_id);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_index_unregister_sync_handler(cache->index, cache->ext_id);
d0bbbc7057aa33b52ee378196dee7d773437468fTimo Sirainenstatic int mail_cache_lock_file(struct mail_cache *cache, bool nonblock)
763f83d3cc47bce05cbc396419c4db2b71dd8e68Timo Sirainen /* previous locking failed. don't waste time waiting on it
763f83d3cc47bce05cbc396419c4db2b71dd8e68Timo Sirainen again, just try once to see if it's available now. */
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen if (cache->index->lock_method != FILE_LOCK_METHOD_DOTLOCK) {
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen ret = mail_index_lock_fd(cache->index, cache->filepath,
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen ret = file_dotlock_create(&cache->dotlock_settings,
5bdad39213d28ab35e615a7f4ea1712ab25b6a80Timo Sirainen "file_dotlock_create()");
4aae8acbcfa9cac96b4af39bfabcbe569e804827Timo Sirainen /* don't bother warning if locking failed due to a timeout. since cache
4aae8acbcfa9cac96b4af39bfabcbe569e804827Timo Sirainen updating isn't all that important we're using a very short timeout
4aae8acbcfa9cac96b4af39bfabcbe569e804827Timo Sirainen so it can be triggered sometimes on heavy load */
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen mail_index_flush_read_cache(cache->index, cache->filepath, cache->fd,
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainenstatic void mail_cache_unlock_file(struct mail_cache *cache)
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen if (cache->index->lock_method != FILE_LOCK_METHOD_DOTLOCK)
d0bbbc7057aa33b52ee378196dee7d773437468fTimo Sirainenmail_cache_lock_full(struct mail_cache *cache, bool require_same_reset_id,
df6478c4cf605bd81b3891c148b84c14eb6c4035Timo Sirainen ext = mail_index_view_get_ext(iview, cache->ext_id);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen /* cache not used */
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen for (i = 0; i < 3; i++) {
cf3bea6d9b57f8608bec22d98ad547a507b05f66Timo Sirainen /* we want the latest cache file */
dc0474dc9d5652d76cb41f439844dd80c8b96642Timo Sirainen /* either we're still waiting for index to
dc0474dc9d5652d76cb41f439844dd80c8b96642Timo Sirainen catch up with a cache compression, or
dc0474dc9d5652d76cb41f439844dd80c8b96642Timo Sirainen that catching up is never going to happen */
e6e43b396799aa5704c679a3017d6c7195f9347dTimo Sirainen if (ret < 0 || (ret == 0 && require_same_reset_id))
d0bbbc7057aa33b52ee378196dee7d773437468fTimo Sirainen if ((ret = mail_cache_lock_file(cache, nonblock)) <= 0) {
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen /* okay, so it was just compressed. try again. */
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen /* make sure our header is up to date */
114a0f74e0f825c6bd8aeadfafb248a030762a1fTimo Sirainen sizeof(struct mail_cache_header));
6fdf8b5e4e71a69f5974f59eec2b8c19bc421fe2Timo Sirainen i_assert((ret <= 0 && !cache->locked) || (ret > 0 && cache->locked));
d0bbbc7057aa33b52ee378196dee7d773437468fTimo Sirainenint mail_cache_lock(struct mail_cache *cache, bool require_same_reset_id)
d0bbbc7057aa33b52ee378196dee7d773437468fTimo Sirainen return mail_cache_lock_full(cache, require_same_reset_id, FALSE);
d0bbbc7057aa33b52ee378196dee7d773437468fTimo Sirainenint mail_cache_try_lock(struct mail_cache *cache)
d0bbbc7057aa33b52ee378196dee7d773437468fTimo Sirainen return mail_cache_lock_full(cache, FALSE, TRUE);
a817fdcc43aedf423e2134091d5f83f91d64bcc9Timo Sirainenint mail_cache_unlock(struct mail_cache *cache)
62cfc346eb7b0a4fd9e1ab6edd63b98711161229Timo Sirainen /* we found it to be broken during the lock. just clean up. */
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen if ((cache->index->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) != 0) {
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen mail_cache_set_syscall_error(cache, "fdatasync()");
dbd9604da561399cc6255289d5b6f6f662ab2d00Timo Sirainenint mail_cache_write(struct mail_cache *cache, const void *data, size_t size,
dbd9604da561399cc6255289d5b6f6f662ab2d00Timo Sirainen if (pwrite_full(cache->fd, data, size, offset) < 0) {
dbd9604da561399cc6255289d5b6f6f662ab2d00Timo Sirainen mail_cache_set_syscall_error(cache, "pwrite_full()");
dbd9604da561399cc6255289d5b6f6f662ab2d00Timo Sirainen file_cache_write(cache->file_cache, data, size, offset);
f501ad38c51cf1d8f4f84313922c785e6ae6e81fTimo Sirainen /* data pointer may change if file cache was grown */
9566c1b4506d49778659e3dc65997f3c0399cb7eTimo Sirainen cache->data = file_cache_get_map(cache->file_cache,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_cache_view_open(struct mail_cache *cache, struct mail_index_view *iview)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_cache_view_close(struct mail_cache_view *view)
5724e7103eed12fe36b55a7b5a8653284a2184b9Timo Sirainen if (view->cache->field_header_write_pending &&
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen (void)mail_cache_header_fields_update(view->cache);
956f7778e413d3184d69e7b96e4a6b3cd5570bcdTimo Sirainenuint32_t mail_cache_get_first_new_seq(struct mail_index_view *view)
956f7778e413d3184d69e7b96e4a6b3cd5570bcdTimo Sirainen if (!mail_index_lookup_seq_range(view, idx_hdr->day_first_uid[7],
956f7778e413d3184d69e7b96e4a6b3cd5570bcdTimo Sirainen /* all messages are too old */