/
mail-transaction-log-file.c
1936 lines (1689 loc) · 55 KB
/
mail-transaction-log-file.c
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2003-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "ioloop.h"
#include "file-dotlock.h"
#include "nfs-workarounds.h"
#include "read-full.h"
#include "write-full.h"
#include "mmap-util.h"
#include "mail-index-private.h"
#include "mail-index-modseq.h"
#include "mail-transaction-log-private.h"
#define LOG_PREFETCH IO_BLOCK_SIZE
#define MEMORY_LOG_NAME "(in-memory transaction log file)"
#define LOG_NEW_DOTLOCK_SUFFIX ".newlock"
static int
mail_transaction_log_file_sync(struct mail_transaction_log_file *file,
bool *retry_r, const char **reason_r);
static void
log_file_set_syscall_error(struct mail_transaction_log_file *file,
const char *function)
{
mail_index_file_set_syscall_error(file->log->index,
file->filepath, function);
}
static void
mail_transaction_log_mark_corrupted(struct mail_transaction_log_file *file)
{
unsigned int offset =
offsetof(struct mail_transaction_log_header, indexid);
int flags;
if (MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file) ||
file->log->index->readonly)
return;
/* indexid=0 marks the log file as corrupted. we opened the file with
O_APPEND, and now we need to drop it for pwrite() to work (at least
in Linux) */
flags = fcntl(file->fd, F_GETFL, 0);
if (flags < 0) {
mail_index_file_set_syscall_error(file->log->index,
file->filepath, "fcntl(F_GETFL)");
return;
}
if (fcntl(file->fd, F_SETFL, flags & ~O_APPEND) < 0) {
mail_index_file_set_syscall_error(file->log->index,
file->filepath, "fcntl(F_SETFL)");
return;
}
if (pwrite_full(file->fd, &file->hdr.indexid,
sizeof(file->hdr.indexid), offset) < 0) {
mail_index_file_set_syscall_error(file->log->index,
file->filepath, "pwrite()");
}
}
void
mail_transaction_log_file_set_corrupted(struct mail_transaction_log_file *file,
const char *fmt, ...)
{
va_list va;
file->corrupted = TRUE;
file->hdr.indexid = 0;
mail_transaction_log_mark_corrupted(file);
va_start(va, fmt);
T_BEGIN {
mail_index_set_error(file->log->index,
"Corrupted transaction log file %s seq %u: %s "
"(sync_offset=%"PRIuUOFF_T")",
file->filepath, file->hdr.file_seq,
t_strdup_vprintf(fmt, va), file->sync_offset);
} T_END;
va_end(va);
}
struct mail_transaction_log_file *
mail_transaction_log_file_alloc(struct mail_transaction_log *log,
const char *path)
{
struct mail_transaction_log_file *file;
file = i_new(struct mail_transaction_log_file, 1);
file->log = log;
file->filepath = i_strdup(path);
file->fd = -1;
return file;
}
void mail_transaction_log_file_free(struct mail_transaction_log_file **_file)
{
struct mail_transaction_log_file *file = *_file;
struct mail_transaction_log_file **p;
int old_errno = errno;
*_file = NULL;
i_assert(!file->locked);
for (p = &file->log->files; *p != NULL; p = &(*p)->next) {
if (*p == file) {
*p = file->next;
break;
}
}
if (file == file->log->head)
file->log->head = NULL;
if (file->buffer != NULL)
buffer_free(&file->buffer);
if (file->mmap_base != NULL) {
if (munmap(file->mmap_base, file->mmap_size) < 0)
log_file_set_syscall_error(file, "munmap()");
}
if (file->fd != -1) {
if (close(file->fd) < 0)
log_file_set_syscall_error(file, "close()");
}
i_free(file->filepath);
i_free(file);
errno = old_errno;
}
static void
mail_transaction_log_file_skip_to_head(struct mail_transaction_log_file *file)
{
struct mail_transaction_log *log = file->log;
struct mail_index_map *map = log->index->map;
const struct mail_index_modseq_header *modseq_hdr;
uoff_t head_offset;
if (map == NULL || file->hdr.file_seq != map->hdr.log_file_seq ||
map->hdr.log_file_head_offset == 0)
return;
/* we can get a valid log offset from index file. initialize
sync_offset from it so we don't have to read the whole log
file from beginning. */
head_offset = map->hdr.log_file_head_offset;
modseq_hdr = mail_index_map_get_modseq_header(map);
if (head_offset < file->hdr.hdr_size) {
mail_index_set_error(log->index,
"%s: log_file_head_offset too small",
log->index->filepath);
file->sync_offset = file->hdr.hdr_size;
file->sync_highest_modseq = file->hdr.initial_modseq;
} else if (modseq_hdr == NULL && file->hdr.initial_modseq == 0) {
/* modseqs not used yet */
file->sync_offset = head_offset;
file->sync_highest_modseq = 0;
} else if (modseq_hdr == NULL ||
modseq_hdr->log_seq != file->hdr.file_seq) {
/* highest_modseq not synced, start from beginning */
file->sync_offset = file->hdr.hdr_size;
file->sync_highest_modseq = file->hdr.initial_modseq;
} else if (modseq_hdr->log_offset > head_offset) {
mail_index_set_error(log->index,
"%s: modseq_hdr.log_offset too large",
log->index->filepath);
file->sync_offset = file->hdr.hdr_size;
file->sync_highest_modseq = file->hdr.initial_modseq;
} else {
/* start from where we last stopped tracking modseqs */
file->sync_offset = modseq_hdr->log_offset;
file->sync_highest_modseq = modseq_hdr->highest_modseq;
}
if (file->hdr.file_seq == log->index->map->hdr.log_file_seq) {
file->saved_tail_offset =
log->index->map->hdr.log_file_tail_offset;
file->saved_tail_sync_offset = file->saved_tail_offset;
}
if (file->saved_tail_offset > file->max_tail_offset)
file->max_tail_offset = file->saved_tail_offset;
}
static void
mail_transaction_log_file_add_to_list(struct mail_transaction_log_file *file)
{
struct mail_transaction_log_file **p;
const char *reason;
bool retry;
file->sync_offset = file->hdr.hdr_size;
file->sync_highest_modseq = file->hdr.initial_modseq;
mail_transaction_log_file_skip_to_head(file);
/* insert it to correct position */
for (p = &file->log->files; *p != NULL; p = &(*p)->next) {
if ((*p)->hdr.file_seq > file->hdr.file_seq)
break;
i_assert((*p)->hdr.file_seq < file->hdr.file_seq);
}
file->next = *p;
*p = file;
if (file->buffer != NULL) {
/* if we read any unfinished data, make sure the buffer gets
truncated. */
(void)mail_transaction_log_file_sync(file, &retry, &reason);
buffer_set_used_size(file->buffer,
file->sync_offset - file->buffer_offset);
}
}
static int
mail_transaction_log_init_hdr(struct mail_transaction_log *log,
struct mail_transaction_log_header *hdr)
{
struct mail_index *index = log->index;
struct mail_transaction_log_file *file;
i_assert(index->indexid != 0);
i_zero(hdr);
hdr->major_version = MAIL_TRANSACTION_LOG_MAJOR_VERSION;
hdr->minor_version = MAIL_TRANSACTION_LOG_MINOR_VERSION;
hdr->hdr_size = sizeof(struct mail_transaction_log_header);
hdr->indexid = log->index->indexid;
hdr->create_stamp = ioloop_time;
#if !WORDS_BIGENDIAN
hdr->compat_flags |= MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
#endif
if (index->fd != -1) {
/* not creating index - make sure we have latest header */
if (!index->mapping) {
if (mail_index_map(index,
MAIL_INDEX_SYNC_HANDLER_HEAD) <= 0)
return -1;
} else {
/* if we got here from mapping, the .log file is
corrupted. use whatever values we got from index
file */
}
}
if (index->map != NULL) {
hdr->prev_file_seq = index->map->hdr.log_file_seq;
hdr->prev_file_offset = index->map->hdr.log_file_head_offset;
hdr->file_seq = index->map->hdr.log_file_seq + 1;
hdr->initial_modseq =
mail_index_map_modseq_get_highest(index->map);
} else {
hdr->file_seq = 1;
}
if (hdr->initial_modseq == 0) {
/* modseq tracking in log files is required for many reasons
nowadays, even if per-message modseqs aren't enabled in
dovecot.index. */
hdr->initial_modseq = 1;
}
if (log->head != NULL) {
/* make sure the sequence always increases to avoid crashes
later. this catches the buggy case where two processes
happen to replace the same log file. */
for (file = log->head->next; file != NULL; file = file->next) {
if (hdr->file_seq <= file->hdr.file_seq)
hdr->file_seq = file->hdr.file_seq + 1;
}
if (hdr->file_seq <= log->head->hdr.file_seq) {
/* make sure the sequence grows */
hdr->file_seq = log->head->hdr.file_seq+1;
}
if (hdr->initial_modseq < log->head->sync_highest_modseq) {
/* this should be always up-to-date */
hdr->initial_modseq = log->head->sync_highest_modseq;
}
}
return 0;
}
struct mail_transaction_log_file *
mail_transaction_log_file_alloc_in_memory(struct mail_transaction_log *log)
{
struct mail_transaction_log_file *file;
file = mail_transaction_log_file_alloc(log, MEMORY_LOG_NAME);
if (mail_transaction_log_init_hdr(log, &file->hdr) < 0) {
i_free(file);
return NULL;
}
file->buffer = buffer_create_dynamic(default_pool, 4096);
file->buffer_offset = sizeof(file->hdr);
mail_transaction_log_file_add_to_list(file);
return file;
}
static int
mail_transaction_log_file_dotlock(struct mail_transaction_log_file *file)
{
struct dotlock_settings dotlock_set;
int ret;
if (file->log->dotlock_count > 0)
ret = 1;
else {
mail_transaction_log_get_dotlock_set(file->log, &dotlock_set);
ret = file_dotlock_create(&dotlock_set, file->filepath, 0,
&file->log->dotlock);
}
if (ret > 0) {
file->log->dotlock_count++;
file->locked = TRUE;
file->lock_created = time(NULL);
return 0;
}
if (ret < 0) {
log_file_set_syscall_error(file, "file_dotlock_create()");
return -1;
}
mail_index_set_error(file->log->index,
"Timeout (%us) while waiting for "
"dotlock for transaction log file %s",
dotlock_set.timeout, file->filepath);
file->log->index->index_lock_timeout = TRUE;
return -1;
}
static int
mail_transaction_log_file_undotlock(struct mail_transaction_log_file *file)
{
int ret;
if (--file->log->dotlock_count > 0)
return 0;
ret = file_dotlock_delete(&file->log->dotlock);
if (ret < 0) {
log_file_set_syscall_error(file, "file_dotlock_delete()");
return -1;
}
if (ret == 0) {
mail_index_set_error(file->log->index,
"Dotlock was lost for transaction log file %s",
file->filepath);
return -1;
}
return 0;
}
int mail_transaction_log_file_lock(struct mail_transaction_log_file *file)
{
unsigned int lock_timeout_secs;
int ret;
if (file->locked)
return 0;
if (MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file)) {
file->locked = TRUE;
return 0;
}
if (file->log->index->lock_method == FILE_LOCK_METHOD_DOTLOCK)
return mail_transaction_log_file_dotlock(file);
if (file->log->index->readonly) {
mail_index_set_error(file->log->index,
"Index is read-only, can't write-lock %s",
file->filepath);
return -1;
}
i_assert(file->file_lock == NULL);
lock_timeout_secs = I_MIN(MAIL_TRANSACTION_LOG_LOCK_TIMEOUT,
file->log->index->max_lock_timeout_secs);
ret = mail_index_lock_fd(file->log->index, file->filepath, file->fd,
F_WRLCK, lock_timeout_secs,
&file->file_lock);
if (ret > 0) {
file->locked = TRUE;
file->lock_created = time(NULL);
return 0;
}
if (ret < 0) {
log_file_set_syscall_error(file, "mail_index_wait_lock_fd()");
return -1;
}
mail_index_set_error(file->log->index,
"Timeout (%us) while waiting for lock for "
"transaction log file %s%s",
lock_timeout_secs, file->filepath,
file_lock_find(file->fd, file->log->index->lock_method, F_WRLCK));
file->log->index->index_lock_timeout = TRUE;
return -1;
}
void mail_transaction_log_file_unlock(struct mail_transaction_log_file *file,
const char *lock_reason)
{
unsigned int lock_time;
if (!file->locked)
return;
file->locked = FALSE;
file->locked_sync_offset_updated = FALSE;
if (MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file))
return;
lock_time = time(NULL) - file->lock_created;
if (lock_time >= MAIL_TRANSACTION_LOG_LOCK_WARN_SECS && lock_reason != NULL) {
i_warning("Transaction log file %s was locked for %u seconds (%s)",
file->filepath, lock_time, lock_reason);
}
if (file->log->index->lock_method == FILE_LOCK_METHOD_DOTLOCK) {
(void)mail_transaction_log_file_undotlock(file);
return;
}
file_unlock(&file->file_lock);
}
static ssize_t
mail_transaction_log_file_read_header(struct mail_transaction_log_file *file)
{
void *dest;
size_t pos, dest_size;
ssize_t ret;
i_assert(file->buffer == NULL && file->mmap_base == NULL);
i_zero(&file->hdr);
if (file->last_size < mmap_get_page_size() && file->last_size > 0) {
/* just read the entire transaction log to memory.
note that if some of the data hasn't been fully committed
yet (hdr.size=0), the buffer must be truncated later */
file->buffer = buffer_create_dynamic(default_pool, 4096);
file->buffer_offset = 0;
dest_size = file->last_size;
dest = buffer_append_space_unsafe(file->buffer, dest_size);
} else {
/* read only the header */
dest = &file->hdr;
dest_size = sizeof(file->hdr);
}
/* it's not necessarily an error to read less than wanted header size,
since older versions of the log format used smaller headers. */
pos = 0;
do {
ret = pread(file->fd, PTR_OFFSET(dest, pos),
dest_size - pos, pos);
if (ret > 0)
pos += ret;
} while (ret > 0 && pos < dest_size);
if (file->buffer != NULL) {
buffer_set_used_size(file->buffer, pos);
memcpy(&file->hdr, file->buffer->data,
I_MIN(pos, sizeof(file->hdr)));
}
return ret < 0 ? -1 : (ssize_t)pos;
}
static int
mail_transaction_log_file_fail_dupe(struct mail_transaction_log_file *file)
{
int ret;
/* mark the old file corrupted. we can't safely remove
it from the list however, so return failure. */
file->hdr.indexid = 0;
if (strcmp(file->filepath, file->log->head->filepath) != 0) {
/* only mark .2 corrupted, just to make sure we don't lose any
changes from .log in case we're somehow wrong */
mail_transaction_log_mark_corrupted(file);
ret = 0;
} else {
ret = -1;
}
if (!file->corrupted) {
file->corrupted = TRUE;
mail_index_set_error(file->log->index,
"Transaction log %s: "
"duplicate transaction log sequence (%u)",
file->filepath, file->hdr.file_seq);
}
return ret;
}
static int
mail_transaction_log_file_read_hdr(struct mail_transaction_log_file *file,
bool ignore_estale)
{
struct mail_transaction_log_file *f;
int ret;
i_assert(!MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file));
if (file->corrupted)
return 0;
ret = mail_transaction_log_file_read_header(file);
if (ret < 0) {
if (errno != ESTALE || !ignore_estale)
log_file_set_syscall_error(file, "pread()");
return -1;
}
if (file->hdr.major_version != MAIL_TRANSACTION_LOG_MAJOR_VERSION) {
/* incompatible version - fix silently */
return 0;
}
if (ret < MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE) {
mail_transaction_log_file_set_corrupted(file,
"unexpected end of file while reading header");
return 0;
}
const unsigned int hdr_version =
MAIL_TRANSACTION_LOG_HDR_VERSION(&file->hdr);
if (MAIL_TRANSACTION_LOG_VERSION_HAVE(hdr_version, COMPAT_FLAGS)) {
/* we have compatibility flags */
enum mail_index_header_compat_flags compat_flags = 0;
#if !WORDS_BIGENDIAN
compat_flags |= MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
#endif
if (file->hdr.compat_flags != compat_flags) {
/* architecture change */
mail_index_set_error(file->log->index,
"Rebuilding index file %s: "
"CPU architecture changed",
file->log->index->filepath);
return 0;
}
}
if (file->hdr.hdr_size < MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE) {
mail_transaction_log_file_set_corrupted(file,
"Header size too small");
return 0;
}
if (file->hdr.hdr_size < sizeof(file->hdr)) {
/* @UNSAFE: smaller than we expected - zero out the fields we
shouldn't have filled */
memset(PTR_OFFSET(&file->hdr, file->hdr.hdr_size), 0,
sizeof(file->hdr) - file->hdr.hdr_size);
}
if (file->hdr.indexid == 0) {
/* corrupted */
file->corrupted = TRUE;
mail_index_set_error(file->log->index,
"Transaction log file %s: marked corrupted",
file->filepath);
return 0;
}
if (file->hdr.indexid != file->log->index->indexid) {
if (file->log->index->indexid != 0 &&
!file->log->index->initial_create) {
/* index file was probably just rebuilt and we don't
know about it yet */
mail_transaction_log_file_set_corrupted(file,
"indexid changed %u -> %u",
file->log->index->indexid, file->hdr.indexid);
return 0;
}
/* creating index file. since transaction log is created
first, use the indexid in it to create the main index
to avoid races. */
file->log->index->indexid = file->hdr.indexid;
}
/* make sure we already don't have a file with the same sequence
opened. it shouldn't happen unless the old log file was
corrupted. */
for (f = file->log->files; f != NULL; f = f->next) {
if (f->hdr.file_seq == file->hdr.file_seq) {
if (strcmp(f->filepath, f->log->head->filepath) != 0) {
/* old "f" is the .log.2 */
return mail_transaction_log_file_fail_dupe(f);
} else {
/* new "file" is probably the .log.2 */
return mail_transaction_log_file_fail_dupe(file);
}
}
}
file->sync_highest_modseq = file->hdr.initial_modseq;
return 1;
}
static int
mail_transaction_log_file_stat(struct mail_transaction_log_file *file,
bool ignore_estale)
{
struct stat st;
if (fstat(file->fd, &st) < 0) {
if (!ESTALE_FSTAT(errno) || !ignore_estale)
log_file_set_syscall_error(file, "fstat()");
return -1;
}
file->st_dev = st.st_dev;
file->st_ino = st.st_ino;
file->last_mtime = st.st_mtime;
file->last_size = st.st_size;
return 0;
}
static bool
mail_transaction_log_file_is_dupe(struct mail_transaction_log_file *file)
{
struct mail_transaction_log_file *tmp;
for (tmp = file->log->files; tmp != NULL; tmp = tmp->next) {
if (tmp->st_ino == file->st_ino &&
CMP_DEV_T(tmp->st_dev, file->st_dev))
return TRUE;
}
return FALSE;
}
static void log_write_ext_hdr_init_data(struct mail_index *index, buffer_t *buf)
{
const struct mail_index_registered_ext *rext;
struct mail_transaction_header *hdr;
struct mail_transaction_ext_intro *intro;
struct mail_transaction_ext_hdr_update *ext_hdr;
unsigned int hdr_offset;
rext = array_idx(&index->extensions, index->ext_hdr_init_id);
/* introduce the extension */
hdr_offset = buf->used;
hdr = buffer_append_space_unsafe(buf, sizeof(*hdr));
hdr->type = MAIL_TRANSACTION_EXT_INTRO;
intro = buffer_append_space_unsafe(buf, sizeof(*intro));
intro->ext_id = (uint32_t)-1;
intro->hdr_size = rext->hdr_size;
intro->record_size = rext->record_size;
intro->record_align = rext->record_align;
intro->name_size = strlen(rext->name);
buffer_append(buf, rext->name, intro->name_size);
if (buf->used % 4 != 0)
buffer_append_zero(buf, 4 - buf->used % 4);
hdr = buffer_get_space_unsafe(buf, hdr_offset, sizeof(*hdr));
hdr->size = mail_index_uint32_to_offset(buf->used - hdr_offset);
/* add the extension header data */
hdr_offset = buf->used;
hdr = buffer_append_space_unsafe(buf, sizeof(*hdr));
hdr->type = MAIL_TRANSACTION_EXT_HDR_UPDATE;
ext_hdr = buffer_append_space_unsafe(buf, sizeof(*ext_hdr));
ext_hdr->size = rext->hdr_size;
buffer_append(buf, index->ext_hdr_init_data, rext->hdr_size);
hdr = buffer_get_space_unsafe(buf, hdr_offset, sizeof(*hdr));
hdr->size = mail_index_uint32_to_offset(buf->used - hdr_offset);
}
static int
mail_transaction_log_file_create2(struct mail_transaction_log_file *file,
int new_fd, bool reset,
struct dotlock **dotlock)
{
struct mail_index *index = file->log->index;
struct stat st;
const char *path2;
buffer_t *writebuf;
int fd, ret;
bool rename_existing, need_lock;
need_lock = file->log->head != NULL && file->log->head->locked;
if (fcntl(new_fd, F_SETFL, O_APPEND) < 0) {
log_file_set_syscall_error(file, "fcntl(O_APPEND)");
return -1;
}
if (file->log->nfs_flush) {
/* although we check also mtime and file size below, it's done
only to fix broken log files. we don't bother flushing
attribute cache just for that. */
nfs_flush_file_handle_cache(file->filepath);
}
/* log creation is locked now - see if someone already created it.
note that if we're rotating, we need to keep the log locked until
the file has been rewritten. and because fcntl() locks are stupid,
if we go and open()+close() the file and we had it already opened,
its locks are lost. so we use stat() to check if the file has been
recreated, although it almost never is. */
if (reset)
rename_existing = FALSE;
else if (nfs_safe_stat(file->filepath, &st) < 0) {
if (errno != ENOENT) {
log_file_set_syscall_error(file, "stat()");
return -1;
}
rename_existing = FALSE;
} else if (st.st_ino == file->st_ino &&
CMP_DEV_T(st.st_dev, file->st_dev) &&
/* inode/dev checks are enough when we're rotating the file,
but not when we're replacing a broken log file */
st.st_mtime == file->last_mtime &&
(uoff_t)st.st_size == file->last_size) {
/* no-one else recreated the file */
rename_existing = TRUE;
} else {
/* recreated. use the file if its header is ok */
fd = nfs_safe_open(file->filepath, O_RDWR | O_APPEND);
if (fd == -1) {
if (errno != ENOENT) {
log_file_set_syscall_error(file, "open()");
return -1;
}
} else {
file->fd = fd;
file->last_size = 0;
if (mail_transaction_log_file_read_hdr(file,
FALSE) > 0 &&
mail_transaction_log_file_stat(file, FALSE) == 0) {
/* yes, it was ok */
file_dotlock_delete(dotlock);
mail_transaction_log_file_add_to_list(file);
return 0;
}
file->fd = -1;
if (close(fd) < 0)
log_file_set_syscall_error(file, "close()");
}
rename_existing = FALSE;
}
if (index->fd == -1 && !rename_existing) {
/* creating the initial index */
reset = TRUE;
}
if (mail_transaction_log_init_hdr(file->log, &file->hdr) < 0)
return -1;
if (reset) {
/* don't reset modseqs. if we're reseting due to rebuilding
indexes we'll probably want to keep uidvalidity and in such
cases we really don't want to shrink modseqs. */
file->hdr.prev_file_seq = 0;
file->hdr.prev_file_offset = 0;
}
writebuf = buffer_create_dynamic(pool_datastack_create(), 128);
buffer_append(writebuf, &file->hdr, sizeof(file->hdr));
if (index->ext_hdr_init_data != NULL && reset)
log_write_ext_hdr_init_data(index, writebuf);
if (write_full(new_fd, writebuf->data, writebuf->used) < 0) {
log_file_set_syscall_error(file, "write_full()");
return -1;
}
if (file->log->index->fsync_mode == FSYNC_MODE_ALWAYS) {
/* the header isn't important, so don't bother calling
fdatasync() unless it's required */
if (fdatasync(new_fd) < 0) {
log_file_set_syscall_error(file, "fdatasync()");
return -1;
}
}
file->fd = new_fd;
ret = mail_transaction_log_file_stat(file, FALSE);
if (need_lock && ret == 0) {
/* we'll need to preserve the lock */
if (mail_transaction_log_file_lock(file) < 0)
ret = -1;
}
/* if we return -1 the dotlock deletion code closes the fd */
file->fd = -1;
if (ret < 0)
return -1;
/* keep two log files */
if (rename_existing) {
/* rename() would be nice and easy way to do this, except then
there's a race condition between the rename and
file_dotlock_replace(). during that time the log file
doesn't exist, which could cause problems. */
path2 = t_strconcat(file->filepath, ".2", NULL);
if (i_unlink_if_exists(path2) < 0) {
/* try to link() anyway */
}
if (nfs_safe_link(file->filepath, path2, FALSE) < 0 &&
errno != ENOENT && errno != EEXIST) {
mail_index_set_error(index, "link(%s, %s) failed: %m",
file->filepath, path2);
/* ignore the error. we don't care that much about the
second log file and we're going to overwrite this
first one. */
}
/* NOTE: here's a race condition where both .log and .log.2
point to the same file. our reading code should ignore that
though by comparing the inodes. */
}
if (file_dotlock_replace(dotlock,
DOTLOCK_REPLACE_FLAG_DONT_CLOSE_FD) <= 0) {
/* need to unlock to avoid assert-crash in
mail_transaction_log_file_free() */
mail_transaction_log_file_unlock(file, "creation failed");
return -1;
}
/* success */
file->fd = new_fd;
mail_transaction_log_file_add_to_list(file);
i_assert(!need_lock || file->locked);
return 1;
}
int mail_transaction_log_file_create(struct mail_transaction_log_file *file,
bool reset)
{
struct mail_index *index = file->log->index;
struct dotlock_settings new_dotlock_set;
struct dotlock *dotlock;
mode_t old_mask;
int fd, ret;
i_assert(!MAIL_INDEX_IS_IN_MEMORY(index));
if (file->log->index->readonly) {
mail_index_set_error(index,
"Can't create log file %s: Index is read-only",
file->filepath);
return -1;
}
if (index->indexid == 0) {
mail_index_set_error(index,
"Can't create log file %s: Index is marked corrupted",
file->filepath);
return -1;
}
mail_transaction_log_get_dotlock_set(file->log, &new_dotlock_set);
new_dotlock_set.lock_suffix = LOG_NEW_DOTLOCK_SUFFIX;
/* With dotlocking we might already have path.lock created, so this
filename has to be different. */
old_mask = umask(index->mode ^ 0666);
fd = file_dotlock_open(&new_dotlock_set, file->filepath, 0, &dotlock);
umask(old_mask);
if (fd == -1) {
log_file_set_syscall_error(file, "file_dotlock_open()");
return -1;
}
mail_index_fchown(index, fd, file_dotlock_get_lock_path(dotlock));
/* either fd gets used or the dotlock gets deleted and returned fd
is for the existing file */
ret = mail_transaction_log_file_create2(file, fd, reset, &dotlock);
if (ret < 0) {
if (dotlock != NULL)
file_dotlock_delete(&dotlock);
return -1;
}
return ret;
}
int mail_transaction_log_file_open(struct mail_transaction_log_file *file,
const char **reason_r)
{
struct mail_index *index = file->log->index;
unsigned int i;
bool ignore_estale;
int ret;
for (i = 0;; i++) {
if (!index->readonly) {
file->fd = nfs_safe_open(file->filepath,
O_RDWR | O_APPEND);
} else {
file->fd = nfs_safe_open(file->filepath, O_RDONLY);
}
if (file->fd == -1 && errno == EACCES) {
file->fd = nfs_safe_open(file->filepath, O_RDONLY);
index->readonly = TRUE;
}
if (file->fd == -1) {
if (errno == ENOENT) {
*reason_r = "File doesn't exist";
return 0;
}
log_file_set_syscall_error(file, "open()");
*reason_r = t_strdup_printf("open() failed: %m");
return -1;
}
ignore_estale = i < MAIL_INDEX_ESTALE_RETRY_COUNT;
if (mail_transaction_log_file_stat(file, ignore_estale) < 0)
ret = -1;
else if (mail_transaction_log_file_is_dupe(file)) {
/* probably our already opened .log file has been
renamed to .log.2 and we're trying to reopen it.
also possible that hit a race condition where .log
and .log.2 are linked. */
*reason_r = "File is already open";
return 0;
} else {
ret = mail_transaction_log_file_read_hdr(file,
ignore_estale);
}
if (ret > 0) {
/* success */
break;
}
if (ret == 0) {
/* corrupted */
if (index->readonly) {
/* don't delete */
} else {
i_unlink_if_exists(file->filepath);
}
*reason_r = "File is corrupted";
return 0;
}
if (errno != ESTALE ||
i == MAIL_INDEX_ESTALE_RETRY_COUNT) {
/* syscall error */
*reason_r = t_strdup_printf("fstat() failed: %m");
return -1;
}
/* ESTALE - try again */
if (file->buffer != NULL)
buffer_free(&file->buffer);
}
mail_transaction_log_file_add_to_list(file);
return 1;
}
static int
log_file_track_mailbox_sync_offset_hdr(struct mail_transaction_log_file *file,
const void *data, unsigned int trans_size,
const char **error_r)
{
const struct mail_transaction_header_update *u = data;
const struct mail_index_header *ihdr;
const unsigned int size = trans_size - sizeof(struct mail_transaction_header);
const unsigned int offset_pos =
offsetof(struct mail_index_header, log_file_tail_offset);
const unsigned int offset_size = sizeof(ihdr->log_file_tail_offset);
uint32_t tail_offset;
i_assert(offset_size == sizeof(tail_offset));
if (size < sizeof(*u) || size < sizeof(*u) + u->size) {
*error_r = "header update extends beyond record size";
mail_transaction_log_file_set_corrupted(file, "%s", *error_r);
return -1;
}
if (u->offset <= offset_pos &&
u->offset + u->size >= offset_pos + offset_size) {
memcpy(&tail_offset,
CONST_PTR_OFFSET(u + 1, offset_pos - u->offset),
sizeof(tail_offset));
if (tail_offset < file->saved_tail_offset) {
/* ignore shrinking tail offsets */
return 1;
} else if (tail_offset > file->sync_offset + trans_size) {
mail_transaction_log_file_set_corrupted(file,
"log_file_tail_offset %u goes past sync offset %"PRIuUOFF_T,