bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/*
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Locking should never fail or timeout. Exclusive locks must be kept as short
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen time as possible. Shared locks can be long living, so if we can't get
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen exclusive lock directly, we'll recreate the index. That means the shared
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen lock holders can keep using the old file.
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen lock_id is used to figure out if acquired lock is still valid. When index
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen file is reopened, the lock_id can become invalid. It doesn't matter however,
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen as no-one's going to modify the old file anymore.
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen lock_id also tells us if we're referring to a shared or an exclusive lock.
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen This allows us to drop back to shared locking once all exclusive locks
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen are dropped. Shared locks have even numbers, exclusive locks have odd numbers.
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen The number is increased by two every time the lock is dropped or index file
fddec1bf093b45eaedcece13c649b811208e0547Timo Sirainen is reopened.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen*/
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen#include "nfs-workarounds.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-index-private.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
cc833a7a4e2258afdc834ace4bfe6579820a1df3Timo Sirainen#define MAIL_INDEX_SHARED_LOCK_TIMEOUT 120
cc833a7a4e2258afdc834ace4bfe6579820a1df3Timo Sirainen
2af769daebd83719ac696a440e06f6020471cec0Timo Sirainenint mail_index_lock_fd(struct mail_index *index, const char *path, int fd,
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen int lock_type, unsigned int timeout_secs,
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen struct file_lock **lock_r)
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen{
dfc99c18e4ab76a8fbfd18da91e474146da5ab16Timo Sirainen if (fd == -1) {
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen i_assert(MAIL_INDEX_IS_IN_MEMORY(index));
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return 1;
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen }
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen return file_wait_lock(fd, path, lock_type, index->lock_method,
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen timeout_secs, lock_r);
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen}
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainenvoid mail_index_flush_read_cache(struct mail_index *index, const char *path,
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen int fd, bool locked)
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen{
66ecc94150cbce23aad3240135e0782e0a74d479Timo Sirainen if ((index->flags & MAIL_INDEX_OPEN_FLAG_NFS_FLUSH) == 0)
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen return;
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen /* Assume flock() is emulated with fcntl(), because that's how most
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen OSes work nowadays. */
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen if (locked &&
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen (index->lock_method == FILE_LOCK_METHOD_FCNTL ||
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen index->lock_method == FILE_LOCK_METHOD_FLOCK)) {
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen nfs_flush_read_cache_locked(path, fd);
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen } else {
bc564f1d3d953cf724828322b11ae89e0f59ffc9Timo Sirainen nfs_flush_read_cache_unlocked(path, fd);
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen }
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen}