1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
|
/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "buffer.h"
#include "str.h"
#include "mail-cache-private.h"
#define CACHE_PREFETCH IO_BLOCK_SIZE
int mail_cache_get_record(struct mail_cache *cache, uint32_t offset,
const struct mail_cache_record **rec_r)
{
const struct mail_cache_record *rec;
const void *data;
int ret;
i_assert(offset != 0);
if (offset % sizeof(uint32_t) != 0) {
/* records are always 32-bit aligned */
mail_cache_set_corrupted(cache, "invalid record offset");
return -1;
}
/* we don't know yet how large the record is, so just guess */
if (mail_cache_map(cache, offset, sizeof(*rec) + CACHE_PREFETCH,
&data) < 0)
return -1;
if (offset + sizeof(*rec) > cache->mmap_length) {
mail_cache_set_corrupted(cache, "record points outside file");
return -1;
}
rec = data;
if (rec->size < sizeof(*rec)) {
mail_cache_set_corrupted(cache, "invalid record size");
return -1;
}
if (rec->size > CACHE_PREFETCH) {
/* larger than we guessed. map the rest of the record. */
if ((ret = mail_cache_map(cache, offset, rec->size, &data)) < 0)
return -1;
if (ret == 0) {
mail_cache_set_corrupted(cache, "record points outside file");
return -1;
}
rec = data;
}
*rec_r = rec;
return 0;
}
uint32_t mail_cache_lookup_cur_offset(struct mail_index_view *view,
uint32_t seq, uint32_t *reset_id_r)
{
struct mail_cache *cache = mail_index_view_get_index(view)->cache;
struct mail_index_map *map;
const void *data;
uint32_t offset;
mail_index_lookup_ext_full(view, seq, cache->ext_id, &map, &data, NULL);
if (data == NULL) {
/* no cache offsets */
return 0;
}
offset = *((const uint32_t *)data);
if (offset == 0)
return 0;
if (!mail_index_ext_get_reset_id(view, map, cache->ext_id, reset_id_r))
i_unreached();
return offset;
}
static int
mail_cache_lookup_offset(struct mail_cache *cache, struct mail_index_view *view,
uint32_t seq, uint32_t *offset_r)
{
uint32_t offset, reset_id, reset_id2;
int ret;
offset = mail_cache_lookup_cur_offset(view, seq, &reset_id);
if (offset == 0)
return 0;
while (cache->hdr->file_seq != reset_id) {
/* reset_it doesn't match - sync the index/cache */
if ((ret = mail_cache_sync_reset_id(cache)) <= 0)
return ret;
/* lookup again after syncing */
offset = mail_cache_lookup_cur_offset(view, seq, &reset_id2);
if (offset == 0)
return 0;
if (cache->hdr->file_seq == reset_id2)
break; /* match - all good */
if (reset_id == reset_id2) {
/* reset_id didn't change after sync. This means it's
pointing to an old already deleted cache file. */
return 0;
}
/* reset_id changed - try again */
reset_id = reset_id2;
}
*offset_r = offset;
return 1;
}
bool mail_cache_track_loops(struct mail_cache_loop_track *loop_track,
uoff_t offset, uoff_t size)
{
i_assert(offset != 0);
i_assert(size != 0);
/* looping happens only in rare error conditions, so it's enough if we
just catch it eventually. we do this by checking if we've seen
more record data than possible in the accessed file area. */
if (loop_track->size_sum == 0) {
/* first call */
loop_track->min_offset = offset;
loop_track->max_offset = offset + size;
} else {
if (loop_track->min_offset > offset)
loop_track->min_offset = offset;
if (loop_track->max_offset < offset + size)
loop_track->max_offset = offset + size;
}
loop_track->size_sum += size;
return loop_track->size_sum >
(loop_track->max_offset - loop_track->min_offset);
}
void mail_cache_lookup_iter_init(struct mail_cache_view *view, uint32_t seq,
struct mail_cache_lookup_iterate_ctx *ctx_r)
{
struct mail_cache_lookup_iterate_ctx *ctx = ctx_r;
int ret;
if (!view->cache->opened)
(void)mail_cache_open_and_verify(view->cache);
i_zero(ctx);
ctx->view = view;
ctx->seq = seq;
if (!MAIL_CACHE_IS_UNUSABLE(view->cache)) {
/* look up the first offset */
ret = mail_cache_lookup_offset(view->cache, view->view, seq,
&ctx->offset);
if (ret <= 0) {
ctx->stop = TRUE;
ctx->failed = ret < 0;
}
}
ctx->remap_counter = view->cache->remap_counter;
i_zero(&view->loop_track);
}
static bool
mail_cache_lookup_iter_transaction(struct mail_cache_lookup_iterate_ctx *ctx)
{
ctx->rec = mail_cache_transaction_lookup_rec(ctx->view->transaction,
ctx->seq,
&ctx->trans_next_idx);
if (ctx->rec == NULL)
return FALSE;
ctx->inmemory_field_idx = TRUE;
ctx->remap_counter = ctx->view->cache->remap_counter;
ctx->pos = sizeof(*ctx->rec);
ctx->rec_size = ctx->rec->size;
return TRUE;
}
static int
mail_cache_lookup_iter_next_record(struct mail_cache_lookup_iterate_ctx *ctx)
{
struct mail_cache_view *view = ctx->view;
if (ctx->failed)
return -1;
if (ctx->rec != NULL)
ctx->offset = ctx->rec->prev_offset;
if (ctx->offset == 0) {
/* end of this record list. check newly appended data. */
if (view->trans_seq1 > ctx->seq ||
view->trans_seq2 < ctx->seq)
return 0;
/* check data still in memory. this works for recent mails
even with INDEX=MEMORY */
if (!ctx->memory_appends_checked) {
if (mail_cache_lookup_iter_transaction(ctx))
return 1;
ctx->memory_appends_checked = TRUE;
}
if (MAIL_CACHE_IS_UNUSABLE(view->cache) || ctx->stop)
return 0;
/* check data already written to cache file */
if (ctx->disk_appends_checked ||
mail_cache_lookup_offset(view->cache, view->trans_view,
ctx->seq, &ctx->offset) <= 0)
return 0;
ctx->disk_appends_checked = TRUE;
ctx->remap_counter = view->cache->remap_counter;
i_zero(&view->loop_track);
}
if (ctx->stop)
return 0;
/* look up the next record */
if (mail_cache_get_record(view->cache, ctx->offset, &ctx->rec) < 0)
return -1;
if (mail_cache_track_loops(&view->loop_track, ctx->offset,
ctx->rec->size)) {
mail_cache_set_corrupted(view->cache,
"record list is circular");
return -1;
}
ctx->inmemory_field_idx = FALSE;
ctx->remap_counter = view->cache->remap_counter;
ctx->pos = sizeof(*ctx->rec);
ctx->rec_size = ctx->rec->size;
return 1;
}
static int
mail_cache_lookup_rec_get_field(struct mail_cache_lookup_iterate_ctx *ctx,
unsigned int *field_idx_r)
{
struct mail_cache *cache = ctx->view->cache;
uint32_t file_field;
file_field = *((const uint32_t *)CONST_PTR_OFFSET(ctx->rec, ctx->pos));
if (ctx->inmemory_field_idx) {
*field_idx_r = file_field;
return 0;
}
if (file_field >= cache->file_fields_count) {
/* new field, have to re-read fields header to figure
out its size. don't do this if we're purging. */
if (!cache->locked) {
if (mail_cache_header_fields_read(cache) < 0)
return -1;
}
if (file_field >= cache->file_fields_count) {
mail_cache_set_corrupted(cache,
"field index too large (%u >= %u)",
file_field, cache->file_fields_count);
return -1;
}
/* field reading might have re-mmaped the file and
caused rec pointer to break. need to get it again. */
if (mail_cache_get_record(cache, ctx->offset, &ctx->rec) < 0)
return -1;
ctx->remap_counter = cache->remap_counter;
}
*field_idx_r = cache->file_field_map[file_field];
return 0;
}
int mail_cache_lookup_iter_next(struct mail_cache_lookup_iterate_ctx *ctx,
struct mail_cache_iterate_field *field_r)
{
struct mail_cache *cache = ctx->view->cache;
unsigned int field_idx;
unsigned int data_size;
int ret;
i_assert(ctx->remap_counter == cache->remap_counter);
if (ctx->pos + sizeof(uint32_t) > ctx->rec_size) {
if (ctx->pos != ctx->rec_size) {
mail_cache_set_corrupted(cache,
"record has invalid size");
return -1;
}
if ((ret = mail_cache_lookup_iter_next_record(ctx)) <= 0)
return ret;
}
/* return the next field */
if (mail_cache_lookup_rec_get_field(ctx, &field_idx) < 0)
return -1;
ctx->pos += sizeof(uint32_t);
data_size = cache->fields[field_idx].field.field_size;
if (data_size == UINT_MAX &&
ctx->pos + sizeof(uint32_t) <= ctx->rec->size) {
/* variable size field. get its size from the file. */
data_size = *((const uint32_t *)
CONST_PTR_OFFSET(ctx->rec, ctx->pos));
ctx->pos += sizeof(uint32_t);
}
if (ctx->rec->size - ctx->pos < data_size) {
mail_cache_set_corrupted(cache,
"record continues outside its allocated size");
return -1;
}
field_r->field_idx = field_idx;
field_r->data = CONST_PTR_OFFSET(ctx->rec, ctx->pos);
field_r->size = data_size;
field_r->offset = ctx->offset + ctx->pos;
/* each record begins from 32bit aligned position */
ctx->pos += (data_size + sizeof(uint32_t)-1) & ~(sizeof(uint32_t)-1);
return 1;
}
static int mail_cache_seq(struct mail_cache_view *view, uint32_t seq)
{
struct mail_cache_lookup_iterate_ctx iter;
struct mail_cache_iterate_field field;
int ret;
view->cached_exists_value = (view->cached_exists_value + 1) & UINT8_MAX;
if (view->cached_exists_value == 0) {
/* wrapped, we'll have to clear the buffer */
buffer_set_used_size(view->cached_exists_buf, 0);
view->cached_exists_value++;
}
view->cached_exists_seq = seq;
mail_cache_lookup_iter_init(view, seq, &iter);
while ((ret = mail_cache_lookup_iter_next(&iter, &field)) > 0) {
buffer_write(view->cached_exists_buf, field.field_idx,
&view->cached_exists_value, 1);
}
return ret;
}
int mail_cache_field_exists(struct mail_cache_view *view, uint32_t seq,
unsigned int field)
{
const uint8_t *data;
i_assert(seq > 0);
/* NOTE: view might point to a non-committed transaction that has
fields that don't yet exist in the cache file. So don't add any
fast-paths checking whether the field exists in the file. */
/* FIXME: we should discard the cache if view has been synced */
if (view->cached_exists_seq != seq) {
if (mail_cache_seq(view, seq) < 0)
return -1;
}
data = view->cached_exists_buf->data;
return (field < view->cached_exists_buf->used &&
data[field] == view->cached_exists_value) ? 1 : 0;
}
bool mail_cache_field_exists_any(struct mail_cache_view *view, uint32_t seq)
{
uint32_t reset_id;
return mail_cache_lookup_cur_offset(view->view, seq, &reset_id) != 0;
}
enum mail_cache_decision_type
mail_cache_field_get_decision(struct mail_cache *cache, unsigned int field_idx)
{
i_assert(field_idx < cache->fields_count);
return cache->fields[field_idx].field.decision;
}
static int
mail_cache_lookup_bitmask(struct mail_cache_lookup_iterate_ctx *iter,
unsigned int field_idx, unsigned int field_size,
buffer_t *dest_buf)
{
struct mail_cache_iterate_field field;
const unsigned char *src;
unsigned char *dest;
unsigned int i;
bool found = FALSE;
int ret;
/* make sure all bits are cleared first */
buffer_write_zero(dest_buf, 0, field_size);
while ((ret = mail_cache_lookup_iter_next(iter, &field)) > 0) {
if (field.field_idx != field_idx)
continue;
/* merge all bits */
src = field.data;
dest = buffer_get_space_unsafe(dest_buf, 0, field.size);
for (i = 0; i < field.size; i++)
dest[i] |= src[i];
found = TRUE;
}
return ret < 0 ? -1 : (found ? 1 : 0);
}
int mail_cache_lookup_field(struct mail_cache_view *view, buffer_t *dest_buf,
uint32_t seq, unsigned int field_idx)
{
struct mail_cache_lookup_iterate_ctx iter;
struct mail_cache_iterate_field field;
int ret;
ret = mail_cache_field_exists(view, seq, field_idx);
mail_cache_decision_state_update(view, seq, field_idx);
if (ret <= 0)
return ret;
/* the field should exist */
mail_cache_lookup_iter_init(view, seq, &iter);
if (view->cache->fields[field_idx].field.type == MAIL_CACHE_FIELD_BITMASK) {
ret = mail_cache_lookup_bitmask(&iter, field_idx,
view->cache->fields[field_idx].field.field_size,
dest_buf);
} else {
/* return the first one that's found. if there are multiple
they're all identical. */
while ((ret = mail_cache_lookup_iter_next(&iter, &field)) > 0) {
if (field.field_idx == field_idx) {
buffer_append(dest_buf, field.data, field.size);
break;
}
}
}
/* NOTE: view->cache->fields may have been reallocated by
mail_cache_lookup_*(). */
return ret;
}
struct header_lookup_data {
uint32_t data_size;
const unsigned char *data;
};
struct header_lookup_line {
uint32_t line_num;
struct header_lookup_data *data;
};
struct header_lookup_context {
struct mail_cache_view *view;
pool_t pool;
ARRAY(struct header_lookup_line) lines;
};
enum {
HDR_FIELD_STATE_DONTWANT = 0,
HDR_FIELD_STATE_WANT,
HDR_FIELD_STATE_SEEN
};
static void header_lines_save(struct header_lookup_context *ctx,
const struct mail_cache_iterate_field *field)
{
const uint32_t *lines = field->data;
uint32_t data_size = field->size;
struct header_lookup_line hdr_line;
struct header_lookup_data *hdr_data;
void *data_dup;
unsigned int i, lines_count, pos;
/* data = { line_nums[], 0, "headers" } */
for (i = 0; data_size >= sizeof(uint32_t); i++) {
data_size -= sizeof(uint32_t);
if (lines[i] == 0)
break;
}
lines_count = i;
pos = (lines_count+1) * sizeof(uint32_t);
hdr_data = p_new(ctx->pool, struct header_lookup_data, 1);
hdr_data->data_size = data_size;
if (data_size > 0) {
hdr_data->data = data_dup =
p_malloc(ctx->pool, data_size);
memcpy(data_dup, CONST_PTR_OFFSET(field->data, pos), data_size);
}
for (i = 0; i < lines_count; i++) {
hdr_line.line_num = lines[i];
hdr_line.data = hdr_data;
array_push_back(&ctx->lines, &hdr_line);
}
}
static int header_lookup_line_cmp(const struct header_lookup_line *l1,
const struct header_lookup_line *l2)
{
return (int)l1->line_num - (int)l2->line_num;
}
static int
mail_cache_lookup_headers_real(struct mail_cache_view *view, string_t *dest,
uint32_t seq, const unsigned int field_idxs[],
unsigned int fields_count, pool_t *pool_r)
{
struct mail_cache_lookup_iterate_ctx iter;
struct mail_cache_iterate_field field;
struct header_lookup_context ctx;
struct header_lookup_line *lines;
const unsigned char *p, *start, *end;
uint8_t *field_state;
unsigned int i, count, max_field = 0;
size_t hdr_size;
uint8_t want = HDR_FIELD_STATE_WANT;
buffer_t *buf;
int ret;
*pool_r = NULL;
if (fields_count == 0)
return 1;
/* update the decision state regardless of whether the fields
actually exist or not. */
for (i = 0; i < fields_count; i++)
mail_cache_decision_state_update(view, seq, field_idxs[i]);
/* mark all the fields we want to find. */
buf = t_buffer_create(32);
for (i = 0; i < fields_count; i++) {
if (field_idxs[i] > max_field)
max_field = field_idxs[i];
buffer_write(buf, field_idxs[i], &want, 1);
}
field_state = buffer_get_modifiable_data(buf, NULL);
/* lookup the fields */
i_zero(&ctx);
ctx.view = view;
ctx.pool = *pool_r = pool_alloconly_create(MEMPOOL_GROWING"mail cache headers", 1024);
t_array_init(&ctx.lines, 32);
mail_cache_lookup_iter_init(view, seq, &iter);
while ((ret = mail_cache_lookup_iter_next(&iter, &field)) > 0) {
if (field.field_idx > max_field ||
field_state[field.field_idx] != HDR_FIELD_STATE_WANT) {
/* a) don't want it, b) duplicate */
} else {
field_state[field.field_idx] = HDR_FIELD_STATE_SEEN;
header_lines_save(&ctx, &field);
}
}
if (ret < 0)
return -1;
/* check that all fields were found */
for (i = 0; i <= max_field; i++) {
if (field_state[i] == HDR_FIELD_STATE_WANT)
return 0;
}
/* we need to return headers in the order they existed originally.
we can do this by sorting the messages by their line numbers. */
array_sort(&ctx.lines, header_lookup_line_cmp);
lines = array_get_modifiable(&ctx.lines, &count);
/* then start filling dest buffer from the headers */
for (i = 0; i < count; i++) {
start = lines[i].data->data;
end = start + lines[i].data->data_size;
/* find the end of the (multiline) header */
for (p = start; p != end; p++) {
if (*p == '\n' &&
(p+1 == end || (p[1] != ' ' && p[1] != '\t'))) {
p++;
break;
}
}
hdr_size = (size_t)(p - start);
buffer_append(dest, start, hdr_size);
/* if there are more lines for this header, the following lines
continue after this one. so skip this line. */
lines[i].data->data += hdr_size;
lines[i].data->data_size -= hdr_size;
}
return 1;
}
int mail_cache_lookup_headers(struct mail_cache_view *view, string_t *dest,
uint32_t seq, const unsigned int field_idxs[],
unsigned int fields_count)
{
pool_t pool = NULL;
int ret;
if (buffer_get_pool(dest)->datastack_pool)
ret = mail_cache_lookup_headers_real(view, dest, seq,
field_idxs, fields_count,
&pool);
else T_BEGIN {
ret = mail_cache_lookup_headers_real(view, dest, seq,
field_idxs, fields_count,
&pool);
} T_END;
pool_unref(&pool);
return ret;
}
static uint32_t
mail_cache_get_highest_seq_with_cache(struct mail_cache_view *view,
uint32_t below_seq, uint32_t *reset_id_r)
{
struct mail_cache_missing_reason_cache *rc = &view->reason_cache;
uint32_t seq = below_seq-1, highest_checked_seq = 0;
/* find the newest mail that has anything in cache */
if (rc->log_file_head_offset == view->view->log_file_head_offset &&
rc->log_file_head_seq == view->view->log_file_head_seq) {
/* reason_cache matches the current view - we can use it */
highest_checked_seq = rc->highest_checked_seq;
} else {
rc->log_file_head_offset = view->view->log_file_head_offset;
rc->log_file_head_seq = view->view->log_file_head_seq;
}
rc->highest_checked_seq = below_seq;
/* first check anything not already in reason_cache */
for (; seq > highest_checked_seq; seq--) {
if (mail_cache_lookup_cur_offset(view->view, seq, reset_id_r) != 0) {
rc->highest_seq_with_cache = seq;
rc->reset_id = *reset_id_r;
return seq;
}
}
if (seq == 0)
return 0;
/* then return the result from cache */
*reset_id_r = rc->reset_id;
return rc->highest_seq_with_cache;
}
const char *
mail_cache_get_missing_reason(struct mail_cache_view *view, uint32_t seq)
{
uint32_t offset, reset_id;
if (mail_index_is_expunged(view->view, seq))
return "Mail is already expunged";
if (MAIL_CACHE_IS_UNUSABLE(view->cache))
return "Cache file is unusable";
offset = mail_cache_lookup_cur_offset(view->view, seq, &reset_id);
if (offset != 0) {
if (view->cache->hdr->file_seq != reset_id) {
return t_strdup_printf(
"Index reset_id=%u doesn't match cache reset_id=%u",
reset_id, view->cache->hdr->file_seq);
}
return t_strdup_printf(
"Mail has other cached fields, reset_id=%u", reset_id);
}
seq = mail_cache_get_highest_seq_with_cache(view, seq, &reset_id);
if (seq == 0) {
return t_strdup_printf("Cache file is empty, reset_id=%u",
view->cache->hdr->file_seq);
}
uint32_t uid;
mail_index_lookup_uid(view->view, seq, &uid);
if (view->cache->hdr->file_seq != reset_id) {
return t_strdup_printf(
"Mail not cached, highest cached seq=%u uid=%u: "
"Index reset_id=%u doesn't match cache reset_id=%u",
seq, uid, reset_id, view->cache->hdr->file_seq);
}
return t_strdup_printf(
"Mail not cached, highest cached seq=%u uid=%u: reset_id=%u",
seq, uid, reset_id);
}
|