~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/lib-index/mail-index-lock.c

  • Committer: Package Import Robot
  • Author(s): Jaldhar H. Vyas
  • Date: 2013-09-09 00:57:32 UTC
  • mfrom: (1.13.11)
  • mto: (4.8.5 experimental) (1.16.1)
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: package-import@ubuntu.com-20130909005732-dn1eell8srqbhh0e
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2003-2012 Dovecot authors, see the included COPYING file */
 
1
/* Copyright (c) 2003-2013 Dovecot authors, see the included COPYING file */
2
2
 
3
3
/*
4
4
   Locking should never fail or timeout. Exclusive locks must be kept as short
36
36
                              timeout_secs, lock_r);
37
37
}
38
38
 
39
 
static int mail_index_lock(struct mail_index *index, int lock_type,
40
 
                           unsigned int timeout_secs, unsigned int *lock_id_r)
41
 
{
42
 
        int ret;
43
 
 
44
 
        i_assert(lock_type == F_RDLCK || lock_type == F_WRLCK);
45
 
 
46
 
        if (lock_type == F_RDLCK && index->lock_type != F_UNLCK) {
47
 
                index->shared_lock_count++;
48
 
                *lock_id_r = index->lock_id_counter;
49
 
                ret = 1;
50
 
        } else if (lock_type == F_WRLCK && index->lock_type == F_WRLCK) {
51
 
                index->excl_lock_count++;
52
 
                *lock_id_r = index->lock_id_counter + 1;
53
 
                ret = 1;
54
 
        } else {
55
 
                ret = 0;
56
 
        }
57
 
 
58
 
        if (ret > 0) {
59
 
                /* file is already locked */
60
 
                return 1;
61
 
        }
62
 
 
63
 
        if (index->lock_method == FILE_LOCK_METHOD_DOTLOCK &&
64
 
            !MAIL_INDEX_IS_IN_MEMORY(index)) {
65
 
                /* FIXME: exclusive locking will rewrite the index file every
66
 
                   time. shouldn't really be needed.. reading doesn't require
67
 
                   locks then, though */
68
 
                if (lock_type == F_WRLCK)
69
 
                        return 0;
70
 
 
71
 
                index->shared_lock_count++;
72
 
                index->lock_type = F_RDLCK;
73
 
                *lock_id_r = index->lock_id_counter;
74
 
                return 1;
75
 
        }
76
 
 
77
 
        if (index->file_lock == NULL) {
78
 
                i_assert(index->lock_type == F_UNLCK);
79
 
                ret = mail_index_lock_fd(index, index->filepath, index->fd,
80
 
                                         lock_type, timeout_secs,
81
 
                                         &index->file_lock);
82
 
        } else {
83
 
                i_assert(index->lock_type == F_RDLCK && lock_type == F_WRLCK);
84
 
                ret = file_lock_try_update(index->file_lock, lock_type);
85
 
        }
86
 
        if (ret <= 0)
87
 
                return ret;
88
 
 
89
 
        if (index->lock_type == F_UNLCK)
90
 
                index->lock_id_counter += 2;
91
 
        index->lock_type = lock_type;
92
 
 
93
 
        if (lock_type == F_RDLCK) {
94
 
                index->shared_lock_count++;
95
 
                *lock_id_r = index->lock_id_counter;
96
 
        } else {
97
 
                index->excl_lock_count++;
98
 
                *lock_id_r = index->lock_id_counter + 1;
99
 
        }
100
 
 
101
 
        return 1;
102
 
}
103
 
 
104
39
void mail_index_flush_read_cache(struct mail_index *index, const char *path,
105
40
                                 int fd, bool locked)
106
41
{
117
52
                nfs_flush_read_cache_unlocked(path, fd);
118
53
        }
119
54
}
120
 
 
121
 
int mail_index_lock_shared(struct mail_index *index, unsigned int *lock_id_r)
122
 
{
123
 
        unsigned int timeout_secs;
124
 
        int ret;
125
 
 
126
 
        timeout_secs = I_MIN(MAIL_INDEX_SHARED_LOCK_TIMEOUT,
127
 
                             index->max_lock_timeout_secs);
128
 
        ret = mail_index_lock(index, F_RDLCK, timeout_secs, lock_id_r);
129
 
        if (ret > 0) {
130
 
                mail_index_flush_read_cache(index, index->filepath,
131
 
                                            index->fd, TRUE);
132
 
                return 0;
133
 
        }
134
 
        if (ret < 0)
135
 
                return -1;
136
 
 
137
 
        mail_index_set_error(index,
138
 
                "Timeout (%us) while waiting for shared lock for index file %s",
139
 
                timeout_secs, index->filepath);
140
 
        index->index_lock_timeout = TRUE;
141
 
        return -1;
142
 
}
143
 
 
144
 
int mail_index_try_lock_exclusive(struct mail_index *index,
145
 
                                  unsigned int *lock_id_r)
146
 
{
147
 
        int ret;
148
 
 
149
 
        if ((ret = mail_index_lock(index, F_WRLCK, 0, lock_id_r)) > 0) {
150
 
                mail_index_flush_read_cache(index, index->filepath,
151
 
                                            index->fd, TRUE);
152
 
        }
153
 
        return ret;
154
 
}
155
 
 
156
 
void mail_index_unlock(struct mail_index *index, unsigned int *_lock_id)
157
 
{
158
 
        unsigned int lock_id = *_lock_id;
159
 
 
160
 
        *_lock_id = 0;
161
 
 
162
 
        if ((lock_id & 1) == 0) {
163
 
                /* shared lock */
164
 
                if (!mail_index_is_locked(index, lock_id)) {
165
 
                        /* unlocking some older generation of the index file.
166
 
                           we've already closed the file so just ignore this. */
167
 
                        return;
168
 
                }
169
 
 
170
 
                i_assert(index->shared_lock_count > 0);
171
 
                index->shared_lock_count--;
172
 
        } else {
173
 
                /* exclusive lock */
174
 
                i_assert(lock_id == index->lock_id_counter + 1);
175
 
                i_assert(index->excl_lock_count > 0);
176
 
                i_assert(index->lock_type == F_WRLCK);
177
 
                if (--index->excl_lock_count == 0 &&
178
 
                    index->shared_lock_count > 0) {
179
 
                        /* drop back to a shared lock. */
180
 
                        index->lock_type = F_RDLCK;
181
 
                        (void)file_lock_try_update(index->file_lock, F_RDLCK);
182
 
                }
183
 
        }
184
 
 
185
 
        if (index->shared_lock_count == 0 && index->excl_lock_count == 0) {
186
 
                index->lock_id_counter += 2;
187
 
                index->lock_type = F_UNLCK;
188
 
                if (index->lock_method != FILE_LOCK_METHOD_DOTLOCK) {
189
 
                        if (!MAIL_INDEX_IS_IN_MEMORY(index))
190
 
                                file_unlock(&index->file_lock);
191
 
                }
192
 
                i_assert(index->file_lock == NULL);
193
 
        }
194
 
}
195
 
 
196
 
bool mail_index_is_locked(struct mail_index *index, unsigned int lock_id)
197
 
{
198
 
        if ((index->lock_id_counter ^ lock_id) <= 1 && lock_id != 0) {
199
 
                i_assert(index->lock_type != F_UNLCK);
200
 
                return TRUE;
201
 
        }
202
 
 
203
 
        return FALSE;
204
 
}