mail-index.c revision 947ad4dfcc7c5c07263a5cf2493fdefc9e12d4c3
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek/* Copyright (C) 2003-2004 Timo Sirainen */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekstruct mail_index_module_register mail_index_module_register = { 0 };
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekstruct mail_index *mail_index_alloc(const char *dir, const char *prefix)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek pool_alloconly_create(MEMPOOL_GROWING"index extension", 1024);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek p_array_init(&index->extensions, index->extension_pool, 5);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_ext_register(index, "keywords", 128, 2, 1);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek index->keywords_pool = pool_alloconly_create("keywords", 512);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek hash_create(default_pool, index->keywords_pool, 0,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek strcase_hash, (hash_cmp_callback_t *)strcasecmp);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek index->log = mail_transaction_log_alloc(index);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_free(struct mail_index **_index)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_set_permissions(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekuint32_t mail_index_ext_register(struct mail_index *index, const char *name,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek const struct mail_index_registered_ext *extensions;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek unsigned int i, ext_count;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek extensions = array_get(&index->extensions, &ext_count);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* see if it's already there */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek for (i = 0; i < ext_count; i++) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek rext.name = p_strdup(index->extension_pool, name);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_register_expunge_handler(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek rext = array_idx_modifiable(&index->extensions, ext_id);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek i_assert(rext->expunge_handler == NULL || rext->expunge_handler == cb);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek rext->expunge_handler_call_always = call_always;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_unregister_expunge_handler(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek rext = array_idx_modifiable(&index->extensions, ext_id);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_register_sync_handler(struct mail_index *index, uint32_t ext_id,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek rext = array_idx_modifiable(&index->extensions, ext_id);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_unregister_sync_handler(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek rext = array_idx_modifiable(&index->extensions, ext_id);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_register_sync_lost_handler(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek array_append(&index->sync_lost_handlers, &cb, 1);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekvoid mail_index_unregister_sync_lost_handler(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_sync_lost_handler_t *const *handlers;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek unsigned int i, count;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek handlers = array_get(&index->sync_lost_handlers, &count);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek for (i = 0; i < count; i++) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek array_delete(&index->sync_lost_handlers, i, 1);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekbool mail_index_keyword_lookup(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek unsigned int *idx_r)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* keywords_hash keeps a name => index mapping of keywords.
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek Keywords are never removed from it, so the index values are valid
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek for the lifetime of the mail_index. */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if (hash_lookup_full(index->keywords_hash, keyword, NULL, &value)) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek keyword = keyword_dup = p_strdup(index->keywords_pool, keyword);
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech hash_insert(index->keywords_hash, keyword_dup, POINTER_CAST(*idx_r));
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekint mail_index_map_parse_keywords(struct mail_index_map *map)
a263309322cf8fff15d21207a4eee5f301e3ad2eLukas Slebodnik const struct mail_index_keyword_header *kw_hdr;
a263309322cf8fff15d21207a4eee5f301e3ad2eLukas Slebodnik const struct mail_index_keyword_header_rec *kw_rec;
a263309322cf8fff15d21207a4eee5f301e3ad2eLukas Slebodnik unsigned int i, name_area_end_offset, old_count;
a263309322cf8fff15d21207a4eee5f301e3ad2eLukas Slebodnik ext_id = mail_index_map_lookup_ext(map, "keywords");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* Extension header contains:
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek - struct mail_index_keyword_header
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek - struct mail_index_keyword_header_rec * keywords_count
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek - const char names[] * keywords_count
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek i_assert(ext->hdr_offset < map->hdr.header_size);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek kw_hdr = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek name = (const char *)(kw_rec + kw_hdr->keywords_count);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek old_count = !array_is_created(&map->keyword_idx_map) ? 0 :
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* Keywords can only be added into same mapping. Removing requires a
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek new mapping (recreating the index file) */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* nothing changed */
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek /* make sure the header is valid */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_set_error(index, "Corrupted index file %s: "
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek "Keywords removed unexpectedly",
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if ((size_t)(name - (const char *)kw_hdr) > ext->hdr_size) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_set_error(index, "Corrupted index file %s: "
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek "keywords_count larger than header size",
a949dfb6b03c70896e6ab3c7a10781e8ecbaadc2Lukas Slebodnik name_area_end_offset = (const char *)kw_hdr + ext->hdr_size - name;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if (kw_rec[i].name_offset > name_area_end_offset) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_set_error(index, "Corrupted index file %s: "
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek "name_offset points outside allocated header",
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_set_error(index, "Corrupted index file %s: "
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech "Keyword header doesn't end with NUL",
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* create file -> index mapping */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek i_array_init(&map->keyword_idx_map, kw_hdr->keywords_count);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* Check that existing headers are still the same. It's behind DEBUG
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek since it's pretty useless waste of CPU normally. */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek for (i = 0; i < array_count(&map->keyword_idx_map); i++) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek const char *keyword = name + kw_rec[i].name_offset;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek const unsigned int *old_idx;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek unsigned int idx;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if (!mail_index_keyword_lookup(index, keyword, FALSE, &idx) ||
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek mail_index_set_error(index, "Corrupted index file %s: "
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek "Keywords changed unexpectedly",
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* Register the newly seen keywords */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek const char *keyword = name + kw_rec[i].name_offset;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek unsigned int idx;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek (void)mail_index_keyword_lookup(index, keyword, TRUE, &idx);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekconst ARRAY_TYPE(keywords) *mail_index_get_keywords(struct mail_index *index)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* Make sure all the keywords are in index->keywords. It's quick to do
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if nothing has changed. */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek (void)mail_index_map_parse_keywords(index->map);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekint mail_index_try_open_only(struct mail_index *index)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* Note that our caller must close index->fd by itself. */
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek index->fd = nfs_safe_open(index->filepath, O_RDWR);
a949dfb6b03c70896e6ab3c7a10781e8ecbaadc2Lukas Slebodnik index->fd = open(index->filepath, O_RDONLY);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek return mail_index_set_syscall_error(index, "open()");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* have to create it */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek i_assert(index->map == NULL || index->map->lock_id == 0);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek ret = mail_index_map(index, MAIL_INDEX_SYNC_HANDLER_HEAD);
bae42db17f223e9ba7fa239d899414877d9d8eafJakub Hrozek /* it's corrupted - recreate it */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_set_syscall_error(index, "close()");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekint mail_index_create_tmp_file(struct mail_index *index, const char **path_r)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek const char *path;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek path = *path_r = t_strconcat(index->filepath, ".tmp", NULL);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek fd = open(path, O_RDWR|O_CREAT|O_TRUNC, index->mode);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek return mail_index_file_set_syscall_error(index, path, "open()");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if (index->gid != (gid_t)-1 && fchown(fd, (uid_t)-1, index->gid) < 0) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_file_set_syscall_error(index, path, "fchown()");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekstatic bool mail_index_open_files(struct mail_index *index,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if ((flags & MAIL_INDEX_OPEN_FLAG_CREATE) == 0)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* if dovecot.index exists, read it first so that we can get
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek the correct indexid and log sequence */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* create a new indexid for us */
a949dfb6b03c70896e6ab3c7a10781e8ecbaadc2Lukas Slebodnik ret = index->map != NULL ? 0 : mail_index_try_open(index);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* doesn't exist / corrupted */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* open/create failed, fallback to in-memory indexes */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if ((flags & MAIL_INDEX_OPEN_FLAG_CREATE) == 0)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek index->cache = created ? mail_cache_create(index) :
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekint mail_index_open(struct mail_index *index, enum mail_index_open_flags flags,
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* corrupted, reopen files */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek index->filepath = MAIL_INDEX_IS_IN_MEMORY(index) ?
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek i_strconcat(index->dir, "/", index->prefix, NULL);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek (flags & MAIL_INDEX_OPEN_FLAG_MMAP_DISABLE) != 0;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek (flags & MAIL_INDEX_OPEN_FLAG_DOTLOCK_USE_EXCL) != 0;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek (flags & MAIL_INDEX_OPEN_FLAG_FSYNC_DISABLE) != 0;
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek i_fatal("nfs flush requires fsync_disable=no");
a949dfb6b03c70896e6ab3c7a10781e8ecbaadc2Lukas Slebodnik i_fatal("nfs flush requires mmap_disable=yes");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* doesn't exist and create flag not used */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* completely broken, reopen */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if (i++ < 3) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* too many tries */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekstatic void mail_index_close_file(struct mail_index *index)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek mail_index_set_syscall_error(index, "close()");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekint mail_index_reopen_if_changed(struct mail_index *index)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek return mail_index_set_syscall_error(index, "fstat()");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if (nfs_safe_stat(index->filepath, &st2) < 0) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek return mail_index_set_syscall_error(index, "stat()");
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek if (st1.st_ino == st2.st_ino && CMP_DEV_T(st1.st_dev, st2.st_dev)) {
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* the same file */
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik /* new file, new locks. the old fd can keep its locks, they don't
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek matter anymore as no-one's going to modify the file. */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekint mail_index_refresh(struct mail_index *index)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek /* we have index exclusively locked, nothing could
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek have changed. */
b8946a5dbde01a87465de707092716349a35248bJakub Hrozek ret = mail_index_map(index, MAIL_INDEX_SYNC_HANDLER_HEAD);
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekstruct mail_cache *mail_index_get_cache(struct mail_index *index)
b8946a5dbde01a87465de707092716349a35248bJakub Hrozekint mail_index_set_error(struct mail_index *index, const char *fmt, ...)
57a924e71230ea360b19a88e0d5818cf01017161Petr Čechint mail_index_move_to_memory(struct mail_index *index)
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech /* set the index as being into memory */
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech /* index was never even opened. just mark it as being in
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech memory and let the caller re-open the index. */
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech /* move index map to memory */
84ae0c4345baf1a589a237bfd132acdaf7a56738Jakub Hrozek if (!MAIL_INDEX_MAP_IS_IN_MEMORY(index->map)) {
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech /* move transaction log to memory */
57a924e71230ea360b19a88e0d5818cf01017161Petr Čech /* close the index file. */
57a924e71230ea360b19a88e0d5818cf01017161Petr Čechvoid mail_index_mark_corrupted(struct mail_index *index)
const char *function)
const char *filepath,
const char *function)
#ifdef WORDS_BIGENDIAN