mail-index-lock.c revision 5ec0ca7ff13595daf0d096c17100afb352e6294a
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody/* Copyright (C) 2003-2004 Timo Sirainen */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen Locking should never fail or timeout. Exclusive locks must be kept as short
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen time as possible. Shared locks can be long living, so if we can't get
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen exclusive lock directly within 2 seconds, we'll replace the index file with
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen a copy of it. That means the shared lock holders can keep using the old file
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen while we're modifying the new file.
09060303d565e15d54e42b4ef722f9d3c26f5336Timo Sirainen lock_id is used to figure out if acquired lock is still valid. When index
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen file is reopened, the lock_id can become invalid. It doesn't matter however,
9522aa5f33cc37fe8ccd0d647cc51dd3ba6a9b55Timo Sirainen as no-one's going to modify the old file anymore.
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen lock_id also tells if we're referring to shared or exclusive lock. This
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen allows us to drop back to shared locking once all exclusive locks are
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen dropped. Shared locks have even numbers, exclusive locks have odd numbers.
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen The number is increased by two every time the lock is dropped or index file
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic int mail_index_reopen(struct mail_index *index, int fd)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen unsigned int old_shared_locks, old_lock_id, lock_id = 0;
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen /* new file, new locks. the old fd can keep it's locks, they don't
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen matter anymore as no-one's going to modify the file. */
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen ret = mail_index_lock_shared(index, FALSE, &lock_id);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen else if (ret == 0) {
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen /* index file is lost */
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen mail_index_set_syscall_error(index, "close()");
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainen mail_index_set_syscall_error(index, "close()");
8b16d3b00f051401c97568697ccdbba48663759aTimo Sirainenstatic int mail_index_has_changed(struct mail_index *index)
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen return mail_index_set_syscall_error(index, "fstat()");
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen mail_index_set_syscall_error(index, "stat()");
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen /* lost it? recreate */
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainenint mail_index_map_lock_mprotect(struct mail_index *index,
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen lock_type == F_WRLCK ? (PROT_READ|PROT_WRITE) :
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen if (mprotect(map->mmap_base, map->mmap_size, prot) < 0) {
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen mail_index_set_syscall_error(index, "mprotect()");
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainenstatic int mail_index_lock_mprotect(struct mail_index *index, int lock_type)
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen return mail_index_map_lock_mprotect(index, index->map, lock_type);
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainenstatic int mail_index_lock(struct mail_index *index, int lock_type,
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen unsigned int *lock_id_r)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen i_assert(lock_type == F_RDLCK || lock_type == F_WRLCK);
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (lock_type == F_RDLCK && index->lock_type != F_UNLCK) {
8c2b4a45f17a5cb13bb01058ca37798cf48d91baTimo Sirainen } else if (lock_type == F_WRLCK && index->lock_type == F_WRLCK) {
4fda77c9e9fc68feb292c4dacae1fac49dd08165Timo Sirainen if (update_index && index->excl_lock_count == 0) {
4fda77c9e9fc68feb292c4dacae1fac49dd08165Timo Sirainen /* FIXME: exclusive locking will rewrite the index file every
4fda77c9e9fc68feb292c4dacae1fac49dd08165Timo Sirainen time. shouldn't really be needed.. reading doesn't require
4fda77c9e9fc68feb292c4dacae1fac49dd08165Timo Sirainen locks then, though */
8c2b4a45f17a5cb13bb01058ca37798cf48d91baTimo Sirainen if (update_index && index->lock_type == F_UNLCK) {
ad404d294fedf792619aed432ed8de5174e9ce7cTimo Sirainen if (mail_index_lock_mprotect(index, lock_type) < 0)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen ret = file_wait_lock_full(index->fd, lock_type, timeout_secs,
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen /* deadlock equals to timeout */
009217abb57a24a4076092e8e4e165545747839eStephan Bosch mail_index_set_syscall_error(index, "file_wait_lock()");
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen if (mail_index_lock_mprotect(index, lock_type) < 0)
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainenint mail_index_lock_shared(struct mail_index *index, int update_index,
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen unsigned int *lock_id_r)
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen ret = mail_index_lock(index, F_RDLCK, DEFAULT_LOCK_TIMEOUT,
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen mail_index_set_error(index, "Timeout while waiting for release of "
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen "shared fcntl() lock for index file %s",
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainenstatic int mail_index_copy(struct mail_index *index)
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen fd = mail_index_create_tmp_file(index, &path);
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainen if (mail_index_lock_mprotect(index, F_RDLCK) < 0)
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainen ret = write_full(fd, index->map->hdr, sizeof(*index->map->hdr));
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (ret < 0 || write_full(fd, index->map->records,
09060303d565e15d54e42b4ef722f9d3c26f5336Timo Sirainen sizeof(struct mail_index_record)) < 0) {
09060303d565e15d54e42b4ef722f9d3c26f5336Timo Sirainen mail_index_file_set_syscall_error(index, path, "write_full()");
a4502a71879d6018bd2c64f13614bb619911dd9fTimo Sirainen (void)mail_index_lock_mprotect(index, F_UNLCK);
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainenstatic int mail_index_lock_exclusive_copy(struct mail_index *index)
9058f3006fffd25835ad701e1b2c3c8faafd3c80Timo Sirainen /* copy the index to index.tmp and use it */
a4502a71879d6018bd2c64f13614bb619911dd9fTimo Sirainen (void)mail_index_lock_mprotect(index, F_WRLCK);
a4502a71879d6018bd2c64f13614bb619911dd9fTimo Sirainenint mail_index_lock_exclusive(struct mail_index *index,
a5ddfd7a8b473f73135b93d5e081e470a87f0f7eTimo Sirainen unsigned int *lock_id_r)
a5ddfd7a8b473f73135b93d5e081e470a87f0f7eTimo Sirainen /* exclusive transaction log lock protects exclusive locking
a5ddfd7a8b473f73135b93d5e081e470a87f0f7eTimo Sirainen for the main index file */
a5ddfd7a8b473f73135b93d5e081e470a87f0f7eTimo Sirainen /* wait two seconds for exclusive lock */
a5ddfd7a8b473f73135b93d5e081e470a87f0f7eTimo Sirainen ret = mail_index_lock(index, F_WRLCK, 2, TRUE, lock_id_r);
a5ddfd7a8b473f73135b93d5e081e470a87f0f7eTimo Sirainen if (mail_index_lock_exclusive_copy(index) < 0)
28789c7ce1aa66ab21798bfb73ec64308b9a4de8Timo Sirainenstatic int mail_index_copy_lock_finish(struct mail_index *index)
87842f621233257b7a7945d994ba931508b34877Timo Sirainen /* leave ourself shared locked. */
a5ddfd7a8b473f73135b93d5e081e470a87f0f7eTimo Sirainen "file_try_lock()");
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen mail_index_file_set_syscall_error(index, index->copy_lock_path,
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen if (rename(index->copy_lock_path, index->filepath) < 0) {
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen mail_index_set_error(index, "rename(%s, %s) failed: %m",
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenstatic void mail_index_excl_unlock_finish(struct mail_index *index)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if (index->map != NULL && index->map->write_to_disk) {
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen /* new mapping replaces the old */
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainenvoid mail_index_unlock(struct mail_index *index, unsigned int lock_id)
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen /* shared lock */
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen /* unlocking some older generation of the index file.
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen we've already closed the file so just ignore this. */
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen /* exclusive lock */
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen if (index->shared_lock_count == 0 && index->excl_lock_count == 0) {
7bccaece91a0b1bc61111c30443d6bad6b22c6d0Timo Sirainen (void)mail_index_lock_mprotect(index, F_UNLCK);
7bccaece91a0b1bc61111c30443d6bad6b22c6d0Timo Sirainen "file_wait_lock()");
7bccaece91a0b1bc61111c30443d6bad6b22c6d0Timo Sirainenint mail_index_is_locked(struct mail_index *index, unsigned int lock_id)