/src/dovecot/src/lib-index/mail-transaction-log-append.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "array.h" |
5 | | #include "write-full.h" |
6 | | #include "mail-index-private.h" |
7 | | #include "mail-transaction-log-private.h" |
8 | | |
9 | | void mail_transaction_log_append_add(struct mail_transaction_log_append_ctx *ctx, |
10 | | enum mail_transaction_type type, |
11 | | const void *data, size_t size) |
12 | 0 | { |
13 | 0 | struct mail_transaction_header hdr; |
14 | |
|
15 | 0 | i_assert((type & MAIL_TRANSACTION_TYPE_MASK) != 0); |
16 | 0 | i_assert((size % 4) == 0); |
17 | | |
18 | 0 | if (size == 0) |
19 | 0 | return; |
20 | | |
21 | 0 | i_zero(&hdr); |
22 | 0 | hdr.type = type | ctx->trans_flags; |
23 | 0 | if (type == MAIL_TRANSACTION_EXPUNGE || |
24 | 0 | type == MAIL_TRANSACTION_EXPUNGE_GUID) |
25 | 0 | hdr.type |= MAIL_TRANSACTION_EXPUNGE_PROT; |
26 | 0 | if (type == MAIL_TRANSACTION_BOUNDARY) |
27 | 0 | hdr.type |= MAIL_TRANSACTION_EXTERNAL; |
28 | 0 | hdr.size = sizeof(hdr) + size; |
29 | 0 | hdr.size = mail_index_uint32_to_offset(hdr.size); |
30 | |
|
31 | 0 | buffer_append(ctx->output, &hdr, sizeof(hdr)); |
32 | 0 | buffer_append(ctx->output, data, size); |
33 | |
|
34 | 0 | mail_transaction_update_modseq(&hdr, data, &ctx->new_highest_modseq, |
35 | 0 | MAIL_TRANSACTION_LOG_HDR_VERSION(&ctx->log->head->hdr)); |
36 | 0 | ctx->transaction_count++; |
37 | 0 | } |
38 | | |
39 | | static int |
40 | | log_buffer_move_to_memory(struct mail_transaction_log_append_ctx *ctx) |
41 | 0 | { |
42 | 0 | struct mail_transaction_log_file *file = ctx->log->head; |
43 | |
|
44 | 0 | if (mail_index_move_to_memory(ctx->log->index) < 0) |
45 | 0 | return -1; |
46 | 0 | i_assert(MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)); |
47 | | |
48 | 0 | i_assert(file->buffer_offset + file->buffer->used == file->sync_offset); |
49 | 0 | buffer_append_buf(file->buffer, ctx->output, 0, SIZE_MAX); |
50 | 0 | file->sync_offset = file->buffer_offset + file->buffer->used; |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | | /* Keep the first keep_size bytes of the write, which are already visible to |
55 | | the other processes reading the log. The rest of the write can't be |
56 | | truncated away, so the log needs to be rotated before it can be appended |
57 | | to again. */ |
58 | | static int |
59 | | log_buffer_keep_written(struct mail_transaction_log_append_ctx *ctx, |
60 | | size_t keep_size) |
61 | 0 | { |
62 | 0 | struct mail_transaction_log_file *file = ctx->log->head; |
63 | |
|
64 | 0 | if (keep_size < ctx->output->used) { |
65 | 0 | mail_transaction_log_file_set_garbage_at_eof(file, |
66 | 0 | "Partially written transaction at " |
67 | 0 | "offset %"PRIuUOFF_T, file->sync_offset + keep_size); |
68 | 0 | } |
69 | |
|
70 | 0 | if (file->mmap_base == NULL && file->buffer != NULL) { |
71 | 0 | i_assert(file->buffer_offset + |
72 | 0 | file->buffer->used == file->sync_offset); |
73 | 0 | buffer_append(file->buffer, ctx->output->data, keep_size); |
74 | 0 | } |
75 | 0 | file->sync_offset += keep_size; |
76 | | /* Don't update max_tail_offset. Either the update to it wasn't |
77 | | written, or it wasn't durably written. */ |
78 | 0 | return 0; |
79 | 0 | } |
80 | | |
81 | | static int log_buffer_write(struct mail_transaction_log_append_ctx *ctx, |
82 | | size_t trans_size) |
83 | 0 | { |
84 | 0 | struct mail_transaction_log_file *file = ctx->log->head; |
85 | 0 | size_t written; |
86 | |
|
87 | 0 | if (ctx->output->used == 0) |
88 | 0 | return 0; |
89 | | |
90 | 0 | if (MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)) { |
91 | 0 | if (file->buffer == NULL) { |
92 | 0 | file->buffer = buffer_create_dynamic(default_pool, 4096); |
93 | 0 | file->buffer_offset = sizeof(file->hdr); |
94 | 0 | } |
95 | 0 | buffer_append_buf(file->buffer, ctx->output, 0, SIZE_MAX); |
96 | 0 | file->sync_offset = file->buffer_offset + file->buffer->used; |
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | 0 | if (write_full_count(file->fd, ctx->output->data, ctx->output->used, |
101 | 0 | &written) < 0) { |
102 | | /* write failure, fallback to in-memory indexes. */ |
103 | 0 | mail_index_file_set_syscall_error(ctx->log->index, |
104 | 0 | file->filepath, |
105 | 0 | "write_full_count()"); |
106 | 0 | if (trans_size == 0 || written < trans_size) { |
107 | | /* The transaction wasn't fully written. Readers skip |
108 | | it, but it still can't be removed from the file. */ |
109 | 0 | if (written > 0) { |
110 | 0 | mail_transaction_log_file_set_garbage_at_eof(file, |
111 | 0 | "Partially written transaction at " |
112 | 0 | "offset %"PRIuUOFF_T, file->sync_offset); |
113 | 0 | } |
114 | 0 | return log_buffer_move_to_memory(ctx); |
115 | 0 | } |
116 | | /* The transaction itself was fully written, only the |
117 | | log_file_tail_offset update following it wasn't. */ |
118 | 0 | return log_buffer_keep_written(ctx, trans_size); |
119 | 0 | } |
120 | | |
121 | 0 | if ((ctx->want_fsync && |
122 | 0 | file->log->index->set.fsync_mode != FSYNC_MODE_NEVER) || |
123 | 0 | file->log->index->set.fsync_mode == FSYNC_MODE_ALWAYS) { |
124 | 0 | if (fdatasync(file->fd) < 0) { |
125 | 0 | mail_index_file_set_syscall_error(ctx->log->index, |
126 | 0 | file->filepath, |
127 | 0 | "fdatasync()"); |
128 | | /* The write itself succeeded, so the whole transaction |
129 | | is already visible to the other processes reading the |
130 | | log. It can't be truncated away anymore. */ |
131 | 0 | return log_buffer_keep_written(ctx, ctx->output->used); |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | 0 | if (file->mmap_base == NULL && file->buffer != NULL) { |
136 | | /* we're reading from a file. avoid re-reading the data that |
137 | | we just wrote. this is also important for some NFS clients, |
138 | | which for some reason sometimes can't read() this data we |
139 | | just wrote in the same process */ |
140 | 0 | i_assert(file->buffer_offset + |
141 | 0 | file->buffer->used == file->sync_offset); |
142 | 0 | buffer_append(file->buffer, ctx->output->data, |
143 | 0 | ctx->output->used); |
144 | 0 | } |
145 | 0 | file->sync_offset += ctx->output->used; |
146 | 0 | if (ctx->sync_includes_this) |
147 | 0 | file->max_tail_offset = file->sync_offset; |
148 | 0 | return 0; |
149 | 0 | } |
150 | | |
151 | | static void |
152 | | log_append_sync_offset_if_needed(struct mail_transaction_log_append_ctx *ctx) |
153 | 0 | { |
154 | 0 | struct mail_transaction_log_file *file = ctx->log->head; |
155 | 0 | struct mail_transaction_header_update *u; |
156 | 0 | struct mail_transaction_header *hdr; |
157 | 0 | uint32_t offset; |
158 | 0 | buffer_t buf; |
159 | 0 | unsigned char update_data[sizeof(*u) + sizeof(offset)]; |
160 | |
|
161 | 0 | offset = file->max_tail_offset; |
162 | 0 | if (!ctx->index_sync_transaction) { |
163 | | /* this is a non-syncing transaction. update the tail offset |
164 | | only if we're already writing something else to transaction |
165 | | log anyway. */ |
166 | 0 | i_assert(!ctx->tail_offset_changed); |
167 | | /* FIXME: For now we never do this update, because it would |
168 | | cause errors about shrinking tail offsets with old Dovecot |
169 | | versions. This is anyway just an optimization, so it doesn't |
170 | | matter all that much if we don't do it here. Finish this |
171 | | in v2.3. */ |
172 | | /*if (ctx->output->used == 0)*/ |
173 | 0 | return; |
174 | 0 | } else if (file->max_tail_offset == file->sync_offset) { |
175 | | /* we're synced all the way to tail offset, so this sync |
176 | | transaction can also be included in the same tail offset. */ |
177 | 0 | if (ctx->output->used == 0 && !ctx->tail_offset_changed) { |
178 | | /* nothing to write here after all (e.g. all unchanged |
179 | | flag updates were dropped by export) */ |
180 | 0 | return; |
181 | 0 | } |
182 | | |
183 | 0 | offset = file->max_tail_offset + ctx->output->used + |
184 | 0 | sizeof(*hdr) + sizeof(*u) + sizeof(offset); |
185 | 0 | ctx->sync_includes_this = TRUE; |
186 | 0 | } else { |
187 | | /* This is a syncing transaction. Since we're finishing a sync, |
188 | | we may need to update the tail offset even if we don't have |
189 | | anything else to do. */ |
190 | 0 | } |
191 | | |
192 | 0 | if (file->last_read_hdr_tail_offset == offset) |
193 | 0 | return; |
194 | 0 | i_assert(offset > file->last_read_hdr_tail_offset); |
195 | | |
196 | 0 | buffer_create_from_data(&buf, update_data, sizeof(update_data)); |
197 | 0 | u = buffer_append_space_unsafe(&buf, sizeof(*u)); |
198 | 0 | u->offset = offsetof(struct mail_index_header, log_file_tail_offset); |
199 | 0 | u->size = sizeof(offset); |
200 | 0 | buffer_append(&buf, &offset, sizeof(offset)); |
201 | |
|
202 | 0 | mail_transaction_log_append_add(ctx, MAIL_TRANSACTION_HEADER_UPDATE, |
203 | 0 | buf.data, buf.used); |
204 | 0 | } |
205 | | |
206 | | static int |
207 | | mail_transaction_log_append_locked(struct mail_transaction_log_append_ctx *ctx) |
208 | 0 | { |
209 | 0 | struct mail_transaction_log_file *file = ctx->log->head; |
210 | 0 | struct mail_transaction_boundary *boundary; |
211 | |
|
212 | 0 | if (file->sync_offset < file->last_size) { |
213 | | /* There is some garbage at the end of the transaction log |
214 | | (eg. a previous write failed). The log is append-only, so it |
215 | | can't be truncated away. */ |
216 | 0 | buffer_set_used_size(file->buffer, |
217 | 0 | file->sync_offset - file->buffer_offset); |
218 | 0 | if (!MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)) { |
219 | 0 | mail_transaction_log_file_set_garbage_at_eof(file, |
220 | 0 | "Partially written transaction at " |
221 | 0 | "offset %"PRIuUOFF_T, file->sync_offset); |
222 | 0 | } |
223 | 0 | } |
224 | 0 | if (file->garbage_at_eof && |
225 | 0 | !MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)) { |
226 | | /* Appending after the partially written transaction would |
227 | | make the appended transactions invisible to the readers, |
228 | | which stop at it. Wait until the log is rotated. */ |
229 | 0 | mail_index_set_error(ctx->log->index, |
230 | 0 | "Transaction log %s has garbage at EOF: %s - " |
231 | 0 | "Write failed because log is not yet rotated", |
232 | 0 | file->filepath, file->need_rotate); |
233 | 0 | return -1; |
234 | 0 | } |
235 | | |
236 | | /* don't include log_file_tail_offset update in the transaction */ |
237 | 0 | boundary = buffer_get_space_unsafe(ctx->output, |
238 | 0 | sizeof(struct mail_transaction_header), |
239 | 0 | sizeof(*boundary)); |
240 | 0 | boundary->size = ctx->output->used; |
241 | |
|
242 | 0 | if (ctx->transaction_count <= 2) { |
243 | | /* 0-1 changes. don't bother with the boundary */ |
244 | 0 | unsigned int boundary_size = |
245 | 0 | sizeof(struct mail_transaction_header) + |
246 | 0 | sizeof(*boundary); |
247 | |
|
248 | 0 | buffer_delete(ctx->output, 0, boundary_size); |
249 | 0 | } |
250 | | |
251 | | /* The log_file_tail_offset update is appended outside the transaction, |
252 | | so remember where the transaction ends. */ |
253 | 0 | size_t trans_size = ctx->output->used; |
254 | |
|
255 | 0 | log_append_sync_offset_if_needed(ctx); |
256 | 0 | if (log_buffer_write(ctx, trans_size) < 0) |
257 | 0 | return -1; |
258 | 0 | file->sync_highest_modseq = ctx->new_highest_modseq; |
259 | 0 | return 0; |
260 | 0 | } |
261 | | |
262 | | int mail_transaction_log_append_begin(struct mail_index *index, |
263 | | enum mail_transaction_type flags, |
264 | | struct mail_transaction_log_append_ctx **ctx_r) |
265 | 0 | { |
266 | 0 | struct mail_transaction_log_append_ctx *ctx; |
267 | 0 | struct mail_transaction_boundary boundary; |
268 | |
|
269 | 0 | if (!index->log_sync_locked) { |
270 | 0 | if (mail_transaction_log_lock_head(index->log, "appending") < 0) |
271 | 0 | return -1; |
272 | 0 | } |
273 | 0 | ctx = i_new(struct mail_transaction_log_append_ctx, 1); |
274 | 0 | ctx->log = index->log; |
275 | 0 | ctx->output = buffer_create_dynamic(default_pool, 1024); |
276 | 0 | ctx->trans_flags = flags; |
277 | |
|
278 | 0 | i_zero(&boundary); |
279 | 0 | mail_transaction_log_append_add(ctx, MAIL_TRANSACTION_BOUNDARY, |
280 | 0 | &boundary, sizeof(boundary)); |
281 | |
|
282 | 0 | *ctx_r = ctx; |
283 | 0 | return 0; |
284 | 0 | } |
285 | | |
286 | | int mail_transaction_log_append_commit(struct mail_transaction_log_append_ctx **_ctx) |
287 | 0 | { |
288 | 0 | struct mail_transaction_log_append_ctx *ctx = *_ctx; |
289 | 0 | struct mail_index *index = ctx->log->index; |
290 | 0 | int ret = 0; |
291 | |
|
292 | 0 | *_ctx = NULL; |
293 | |
|
294 | 0 | ret = mail_transaction_log_append_locked(ctx); |
295 | 0 | if (!index->log_sync_locked) |
296 | 0 | mail_transaction_log_file_unlock(index->log->head, "appending"); |
297 | |
|
298 | 0 | buffer_free(&ctx->output); |
299 | | i_free(ctx); |
300 | 0 | return ret; |
301 | 0 | } |