Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib-index/mail-index-lock.c @ 9458:adee8cb3ff5d HEAD
Minor memory allocation tweaks.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 28 Oct 2009 14:10:04 -0400 |
parents | b9faf4db2a9f |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1 /* Copyright (c) 2003-2009 Dovecot authors, see the included COPYING file */ |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 /* |
1925 | 4 Locking should never fail or timeout. Exclusive locks must be kept as short |
5 time as possible. Shared locks can be long living, so if we can't get | |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
6 exclusive lock directly, we'll recreate the index. That means the shared |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
7 lock holders can keep using the old file. |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 |
1925 | 9 lock_id is used to figure out if acquired lock is still valid. When index |
10 file is reopened, the lock_id can become invalid. It doesn't matter however, | |
11 as no-one's going to modify the old file anymore. | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
13 lock_id also tells us if we're referring to a shared or an exclusive lock. |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
14 This allows us to drop back to shared locking once all exclusive locks |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
15 are dropped. Shared locks have even numbers, exclusive locks have odd numbers. |
1925 | 16 The number is increased by two every time the lock is dropped or index file |
17 is reopened. | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 */ |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 #include "lib.h" |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
21 #include "nfs-workarounds.h" |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 #include "mail-index-private.h" |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 |
6360
461ae76c84da
Replaced MAIL_INDEX_LOCK_SECS usage with cache/index/log specific defines
Timo Sirainen <tss@iki.fi>
parents:
5972
diff
changeset
|
24 #define MAIL_INDEX_SHARED_LOCK_TIMEOUT 120 |
461ae76c84da
Replaced MAIL_INDEX_LOCK_SECS usage with cache/index/log specific defines
Timo Sirainen <tss@iki.fi>
parents:
5972
diff
changeset
|
25 |
2941
89ab5c72430d
lock_method=dotlock doesn't crash anymore while trying to modify cache file.
Timo Sirainen <tss@iki.fi>
parents:
2936
diff
changeset
|
26 int mail_index_lock_fd(struct mail_index *index, const char *path, int fd, |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
27 int lock_type, unsigned int timeout_secs, |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
28 struct file_lock **lock_r) |
2723
12b503fbb8af
Replaced fcntl_locks_disable with lock_method, so it's now possible to use
Timo Sirainen <tss@iki.fi>
parents:
2563
diff
changeset
|
29 { |
3835 | 30 if (fd == -1) { |
3833
318c70c1d4ec
Beginnings of fallbacking to in-memory indexes when write fails with "out of
Timo Sirainen <tss@iki.fi>
parents:
3831
diff
changeset
|
31 i_assert(MAIL_INDEX_IS_IN_MEMORY(index)); |
3626
68ab7800d735
Don't leak alarm() with in-memory indexes
Timo Sirainen <tss@iki.fi>
parents:
3552
diff
changeset
|
32 return 1; |
3833
318c70c1d4ec
Beginnings of fallbacking to in-memory indexes when write fails with "out of
Timo Sirainen <tss@iki.fi>
parents:
3831
diff
changeset
|
33 } |
3626
68ab7800d735
Don't leak alarm() with in-memory indexes
Timo Sirainen <tss@iki.fi>
parents:
3552
diff
changeset
|
34 |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
35 return file_wait_lock(fd, path, lock_type, index->lock_method, |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
36 timeout_secs, lock_r); |
2723
12b503fbb8af
Replaced fcntl_locks_disable with lock_method, so it's now possible to use
Timo Sirainen <tss@iki.fi>
parents:
2563
diff
changeset
|
37 } |
12b503fbb8af
Replaced fcntl_locks_disable with lock_method, so it's now possible to use
Timo Sirainen <tss@iki.fi>
parents:
2563
diff
changeset
|
38 |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 static int mail_index_lock(struct mail_index *index, int lock_type, |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
40 unsigned int timeout_secs, unsigned int *lock_id_r) |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 { |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
42 int ret; |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 i_assert(lock_type == F_RDLCK || lock_type == F_WRLCK); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
45 |
1925 | 46 if (lock_type == F_RDLCK && index->lock_type != F_UNLCK) { |
47 index->shared_lock_count++; | |
5854 | 48 *lock_id_r = index->lock_id_counter; |
1940 | 49 ret = 1; |
50 } else if (lock_type == F_WRLCK && index->lock_type == F_WRLCK) { | |
1925 | 51 index->excl_lock_count++; |
5854 | 52 *lock_id_r = index->lock_id_counter + 1; |
1940 | 53 ret = 1; |
54 } else { | |
55 ret = 0; | |
56 } | |
57 | |
4026
20c335412d7d
Added some locking related asserts, comments and when replacing index file, open it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3924
diff
changeset
|
58 if (ret > 0) { |
20c335412d7d
Added some locking related asserts, comments and when replacing index file, open it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3924
diff
changeset
|
59 /* file is already locked */ |
1925 | 60 return 1; |
4026
20c335412d7d
Added some locking related asserts, comments and when replacing index file, open it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3924
diff
changeset
|
61 } |
1925 | 62 |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
63 if (index->lock_method == FILE_LOCK_METHOD_DOTLOCK && |
3924
8297377a569b
Don't break with INDEX=MEMORY and lock_method=dotlock
Timo Sirainen <tss@iki.fi>
parents:
3910
diff
changeset
|
64 !MAIL_INDEX_IS_IN_MEMORY(index)) { |
1920
d11efbf1d341
Added fcntl_lock_disable setting to allow indexes to work with NFS. Some
Timo Sirainen <tss@iki.fi>
parents:
1919
diff
changeset
|
65 /* FIXME: exclusive locking will rewrite the index file every |
d11efbf1d341
Added fcntl_lock_disable setting to allow indexes to work with NFS. Some
Timo Sirainen <tss@iki.fi>
parents:
1919
diff
changeset
|
66 time. shouldn't really be needed.. reading doesn't require |
d11efbf1d341
Added fcntl_lock_disable setting to allow indexes to work with NFS. Some
Timo Sirainen <tss@iki.fi>
parents:
1919
diff
changeset
|
67 locks then, though */ |
d11efbf1d341
Added fcntl_lock_disable setting to allow indexes to work with NFS. Some
Timo Sirainen <tss@iki.fi>
parents:
1919
diff
changeset
|
68 if (lock_type == F_WRLCK) |
d11efbf1d341
Added fcntl_lock_disable setting to allow indexes to work with NFS. Some
Timo Sirainen <tss@iki.fi>
parents:
1919
diff
changeset
|
69 return 0; |
1925 | 70 |
71 index->shared_lock_count++; | |
72 index->lock_type = F_RDLCK; | |
5854 | 73 *lock_id_r = index->lock_id_counter; |
1921 | 74 return 1; |
1920
d11efbf1d341
Added fcntl_lock_disable setting to allow indexes to work with NFS. Some
Timo Sirainen <tss@iki.fi>
parents:
1919
diff
changeset
|
75 } |
d11efbf1d341
Added fcntl_lock_disable setting to allow indexes to work with NFS. Some
Timo Sirainen <tss@iki.fi>
parents:
1919
diff
changeset
|
76 |
2021 | 77 if (lock_type == F_RDLCK || !index->log_locked) { |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
78 i_assert(index->file_lock == NULL); |
2941
89ab5c72430d
lock_method=dotlock doesn't crash anymore while trying to modify cache file.
Timo Sirainen <tss@iki.fi>
parents:
2936
diff
changeset
|
79 ret = mail_index_lock_fd(index, index->filepath, index->fd, |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
80 lock_type, timeout_secs, |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
81 &index->file_lock); |
2021 | 82 } else { |
83 /* this is kind of kludgy. we wish to avoid deadlocks while | |
84 trying to lock transaction log, but it can happen if our | |
85 process is holding transaction log lock and waiting for | |
86 index write lock, while the other process is holding index | |
87 read lock and waiting for transaction log lock. | |
88 | |
89 we don't have a problem with grabbing read index lock | |
90 because the only way for it to block is if it's | |
91 write-locked, which isn't allowed unless transaction log | |
92 is also locked. | |
93 | |
94 so, the workaround for this problem is that we simply try | |
95 locking once. if it doesn't work, just rewrite the file. | |
96 hopefully there won't be any other deadlocking issues. :) */ | |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
97 if (index->file_lock == NULL) { |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
98 ret = mail_index_lock_fd(index, index->filepath, |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
99 index->fd, lock_type, 0, |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
100 &index->file_lock); |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
101 } else { |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
102 ret = file_lock_try_update(index->file_lock, lock_type); |
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
103 } |
1925 | 104 } |
2941
89ab5c72430d
lock_method=dotlock doesn't crash anymore while trying to modify cache file.
Timo Sirainen <tss@iki.fi>
parents:
2936
diff
changeset
|
105 if (ret <= 0) |
89ab5c72430d
lock_method=dotlock doesn't crash anymore while trying to modify cache file.
Timo Sirainen <tss@iki.fi>
parents:
2936
diff
changeset
|
106 return ret; |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
107 |
1925 | 108 if (index->lock_type == F_UNLCK) |
5854 | 109 index->lock_id_counter += 2; |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
110 index->lock_type = lock_type; |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
111 |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
112 if (lock_type == F_RDLCK) { |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
113 index->shared_lock_count++; |
5854 | 114 *lock_id_r = index->lock_id_counter; |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
115 } else { |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
116 index->excl_lock_count++; |
5854 | 117 *lock_id_r = index->lock_id_counter + 1; |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
118 } |
1917
68938dccbc45
Forced locking to be right with mprotect()ing index file. Support for
Timo Sirainen <tss@iki.fi>
parents:
1915
diff
changeset
|
119 |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
120 return 1; |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
121 } |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
122 |
6829
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
123 void mail_index_flush_read_cache(struct mail_index *index, const char *path, |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
124 int fd, bool locked) |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
125 { |
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
126 if (!index->nfs_flush) |
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
127 return; |
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
128 |
6829
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
129 /* Assume flock() is emulated with fcntl(), because that's how most |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
130 OSes work nowadays. */ |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
131 if (locked && |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
132 (index->lock_method == FILE_LOCK_METHOD_FCNTL || |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
133 index->lock_method == FILE_LOCK_METHOD_FLOCK)) { |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
134 nfs_flush_read_cache_locked(path, fd); |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
135 } else { |
6829
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
136 nfs_flush_read_cache_unlocked(path, fd); |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
137 } |
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
138 } |
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
139 |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
140 int mail_index_lock_shared(struct mail_index *index, unsigned int *lock_id_r) |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
141 { |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
142 int ret; |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
143 |
6360
461ae76c84da
Replaced MAIL_INDEX_LOCK_SECS usage with cache/index/log specific defines
Timo Sirainen <tss@iki.fi>
parents:
5972
diff
changeset
|
144 ret = mail_index_lock(index, F_RDLCK, MAIL_INDEX_SHARED_LOCK_TIMEOUT, |
461ae76c84da
Replaced MAIL_INDEX_LOCK_SECS usage with cache/index/log specific defines
Timo Sirainen <tss@iki.fi>
parents:
5972
diff
changeset
|
145 lock_id_r); |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
146 if (ret > 0) { |
6829
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
147 mail_index_flush_read_cache(index, index->filepath, |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
148 index->fd, TRUE); |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
149 return 0; |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
150 } |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
151 if (ret < 0) |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
152 return -1; |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
153 |
3552 | 154 mail_index_set_error(index, |
155 "Timeout while waiting for shared lock for index file %s", | |
156 index->filepath); | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
157 index->index_lock_timeout = TRUE; |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
158 return -1; |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
159 } |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
160 |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
161 int mail_index_try_lock_exclusive(struct mail_index *index, |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
162 unsigned int *lock_id_r) |
1919
4aeb7357951a
locking fixes, mmap_disable=yes fixes
Timo Sirainen <tss@iki.fi>
parents:
1917
diff
changeset
|
163 { |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
164 int ret; |
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
165 |
6829
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
166 if ((ret = mail_index_lock(index, F_WRLCK, 0, lock_id_r)) > 0) { |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
167 mail_index_flush_read_cache(index, index->filepath, |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
168 index->fd, TRUE); |
dbab5e592577
NFS cache flushing APIs changed and backend implements it a bit differently.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
169 } |
5972
9fb9dc4d8df8
Flush NFS caches when needed if MAIL_INDEX_OPEN_FLAG_NFS_FLUSH is enabled.
Timo Sirainen <tss@iki.fi>
parents:
5854
diff
changeset
|
170 return ret; |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
171 } |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
172 |
5854 | 173 void mail_index_unlock(struct mail_index *index, unsigned int *_lock_id) |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
174 { |
5854 | 175 unsigned int lock_id = *_lock_id; |
176 | |
177 *_lock_id = 0; | |
178 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
179 if ((lock_id & 1) == 0) { |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
180 /* shared lock */ |
1925 | 181 if (!mail_index_is_locked(index, lock_id)) { |
182 /* unlocking some older generation of the index file. | |
183 we've already closed the file so just ignore this. */ | |
184 return; | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
185 } |
1925 | 186 |
187 i_assert(index->shared_lock_count > 0); | |
188 index->shared_lock_count--; | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
189 } else { |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
190 /* exclusive lock */ |
5854 | 191 i_assert(lock_id == index->lock_id_counter + 1); |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
192 i_assert(index->excl_lock_count > 0); |
5627
c46edae0e454
If index file is shared locked while we're trying to exclusively lock it,
Timo Sirainen <tss@iki.fi>
parents:
5620
diff
changeset
|
193 i_assert(index->lock_type == F_WRLCK); |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
194 if (--index->excl_lock_count == 0 && |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
195 index->shared_lock_count > 0) { |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
196 /* drop back to a shared lock. */ |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
197 index->lock_type = F_RDLCK; |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
198 (void)file_lock_try_update(index->file_lock, F_RDLCK); |
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
199 } |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
200 } |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
201 |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
202 if (index->shared_lock_count == 0 && index->excl_lock_count == 0) { |
5854 | 203 index->lock_id_counter += 2; |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
204 index->lock_type = F_UNLCK; |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
205 if (index->lock_method != FILE_LOCK_METHOD_DOTLOCK) { |
5689
c2362f144f15
Initial commit for major index file code cleanup.
Timo Sirainen <tss@iki.fi>
parents:
5627
diff
changeset
|
206 if (!MAIL_INDEX_IS_IN_MEMORY(index)) |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
207 file_unlock(&index->file_lock); |
1917
68938dccbc45
Forced locking to be right with mprotect()ing index file. Support for
Timo Sirainen <tss@iki.fi>
parents:
1915
diff
changeset
|
208 } |
4876
f1d77064884c
Lock handling changes. Everything goes through file-lock API now and there's
Timo Sirainen <tss@iki.fi>
parents:
4705
diff
changeset
|
209 i_assert(index->file_lock == NULL); |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
210 } |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
211 } |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
212 |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3835
diff
changeset
|
213 bool mail_index_is_locked(struct mail_index *index, unsigned int lock_id) |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
214 { |
5854 | 215 if ((index->lock_id_counter ^ lock_id) <= 1 && lock_id != 0) { |
2021 | 216 i_assert(index->lock_type != F_UNLCK); |
217 return TRUE; | |
218 } | |
219 | |
220 return FALSE; | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
221 } |