index-mailbox-size.c revision ca004511f36c090b07c6f097cf237ce836d8faf6
/* Copyright (c) 2002-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "strescape.h"
#include "net.h"
#include "write-full.h"
#include "file-create-locked.h"
#include "mail-search-build.h"
#include "index-storage.h"
#include "index-mailbox-size.h"
/*
Saving new mails: After transaction is committed and synced, trigger
vsize updating. Lock vsize updates. Check if the message count +
last-indexed-uid are still valid. If they are, add all the missing new
mails. Unlock.
Fetching vsize: Lock vsize updates. Check if the message count +
last-indexed-uid are still valid. If not, set them to zero. Add all
the missing mails. Unlock.
Expunging mails: Check if syncing would expunge any mails. If so, lock the
vsize updates before locking syncing (to avoid deadlocks). Check if the
message count + last-indexed-uid are still valid. If not, unlock vsize and
do nothing else. Otherwise, for each expunged mail whose UID <=
last-indexed-uid, decrease the message count and the vsize in memory. After
syncing is successfully committed, write the changes to header. Unlock.
Note that the final expunge handling with some mailbox formats is done while
syncing is no longer locked. Because of this we need to have the vsize
locking. The final vsize header update requires committing a transaction,
which internally is the same as a sync lock. So to avoid deadlocks we always
need to lock vsize updates before sync.
*/
#define VSIZE_LOCK_SUFFIX ".vsize.lock"
#define VSIZE_UPDATE_MAX_LOCK_SECS 10
#define INDEXER_SOCKET_NAME "indexer"
#define INDEXER_HANDSHAKE "VERSION\tindexer\t1\t0\n"
struct mailbox_vsize_update {
struct mail_index_view *view;
char *lock_path;
int lock_fd;
bool rebuild;
bool written;
bool finish_in_background;
};
{
const void *data;
if (size > 0) {
}
else {
if (size != 0) {
"vsize-hdr has invalid size: %"PRIuSIZE_T,
size);
}
}
}
static void
{
return;
seq2 = 0;
"vsize-hdr has invalid message-count (%u < %u)",
} else {
/* some messages have been expunged, rescan */
}
}
}
struct mailbox_vsize_update *
{
struct mailbox_vsize_update *update;
return update;
}
unsigned int lock_secs)
{
const struct mailbox_permissions *perm;
struct file_create_settings set;
const char *error;
bool created;
return FALSE;
i_error("file_create_locked(%s) failed: %m",
}
return FALSE;
}
return TRUE;
}
{
return vsize_update_lock_full(update, 0);
}
{
}
{
}
static void
{
struct mail_index_transaction *trans;
return;
/* no changes */
return;
}
(void)mail_index_transaction_commit(&trans);
}
{
const char *path;
int fd;
if (fd == -1) {
"Can't start vsize building on background: "
"net_connect_unix(%s) failed: %m", path);
return;
}
"Can't start vsize building on background: "
"write(%s) failed: %m", path);
}
i_close_fd(&fd);
}
{
}
if (update->finish_in_background)
}
{
return;
"vsize-hdr's message_count shrank below 0");
return;
}
"vsize-hdr's vsize shrank below 0");
return;
}
}
static int
bool require_result)
{
struct mailbox_transaction_context *trans;
struct mail_search_context *search_ctx;
struct mail_search_args *search_args;
struct mailbox_status status;
unsigned int mails_left;
int ret = 0;
/* nothing to do - we should have usually caught this already
before locking */
return 0;
}
/* note that update->view may be more up-to-date than box->view.
we'll just add whatever new mails are in box->view. if we'll notice
that some of the new mails are missing, we'll need to stop there
since that expunge will be applied later on to the vsize header. */
/* nothing existed, but update uidnext */
return 0;
}
if (!require_result)
mails_left = 0;
else
if (mails_left == 0) {
/* if there are any more mails whose vsize can't be
looked up from cache, abort and finish on
background. */
}
if (ret < 0 &&
/* abort and finish on background */
i_assert(mails_left == 0);
"Finishing vsize calculation on background");
if (require_result)
break;
}
/* slow vsize lookup */
i_assert(mails_left > 0);
mails_left--;
}
if (ret < 0) {
continue;
ret = -1;
break;
}
}
if (mailbox_search_deinit(&search_ctx) < 0)
ret = -1;
if (ret == 0) {
/* success, cache all */
} else {
/* search failed, cache only up to highest seen uid */
}
(void)mailbox_transaction_commit(&trans);
return ret;
}
struct mailbox_metadata *metadata_r)
{
struct mailbox_vsize_update *update;
struct mailbox_status status;
int ret;
/* up to date */
return 0;
}
/* we need to update it - lock it if possible. if not, update it
anyway internally even though we won't be saving the result. */
return ret;
}
struct mailbox_metadata *metadata_r)
{
struct mailbox_transaction_context *trans;
struct mail_search_context *ctx;
struct mail_search_args *search_args;
int ret = 0;
/* if physical size = virtual size always for the storage, we can
use the optimized vsize code for this */
return -1;
return 0;
}
/* do it the slow way (we could implement similar logic as for vsize,
but for now it's not really needed) */
return -1;
metadata_r->physical_size = 0;
else {
const char *errstr;
enum mail_error error;
if (error != MAIL_ERROR_EXPUNGED) {
i_error("Couldn't get size of mail UID %u in %s: %s",
ret = -1;
break;
}
}
}
if (mailbox_search_deinit(&ctx) < 0) {
i_error("Listing mails in %s failed: %s",
ret = -1;
}
(void)mailbox_transaction_commit(&trans);
return ret;
}
{
struct mailbox_vsize_update *update;
struct mailbox_status status;
/* update here only if we don't need to rebuild the whole vsize. */
}