-
Notifications
You must be signed in to change notification settings - Fork 271
/
mdbox-map.c
1500 lines (1290 loc) · 40 KB
/
mdbox-map.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) 2007-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "hash.h"
#include "ostream.h"
#include "mkdir-parents.h"
#include "unlink-old-files.h"
#include "mailbox-list-private.h"
#include "mdbox-storage.h"
#include "mdbox-file.h"
#include "mdbox-map-private.h"
#include <dirent.h>
#define MAX_BACKWARDS_LOOKUPS 10
#define DBOX_FORCE_PURGE_MIN_BYTES (1024*1024*10)
#define DBOX_FORCE_PURGE_MIN_RATIO 0.5
#define MAP_STORAGE(map) (&(map)->storage->storage.storage)
struct mdbox_map_transaction_context {
struct mdbox_map_atomic_context *atomic;
struct mail_index_transaction *trans;
bool changed:1;
bool committed:1;
};
static int mdbox_map_generate_uid_validity(struct mdbox_map *map);
void mdbox_map_set_corrupted(struct mdbox_map *map, const char *format, ...)
{
va_list args;
va_start(args, format);
mail_storage_set_critical(MAP_STORAGE(map),
"mdbox map %s corrupted: %s",
map->index->filepath,
t_strdup_vprintf(format, args));
va_end(args);
mdbox_storage_set_corrupted(map->storage);
}
struct mdbox_map *
mdbox_map_init(struct mdbox_storage *storage, struct mailbox_list *root_list)
{
struct mdbox_map *map;
const char *root, *index_root;
root = mailbox_list_get_root_forced(root_list, MAILBOX_LIST_PATH_TYPE_DIR);
index_root = mailbox_list_get_root_forced(root_list, MAILBOX_LIST_PATH_TYPE_INDEX);
map = i_new(struct mdbox_map, 1);
map->storage = storage;
map->set = storage->set;
map->path = i_strconcat(root, "/"MDBOX_GLOBAL_DIR_NAME, NULL);
map->index_path =
i_strconcat(index_root, "/"MDBOX_GLOBAL_DIR_NAME, NULL);
map->index = mail_index_alloc(map->index_path,
MDBOX_GLOBAL_INDEX_PREFIX);
mail_index_set_fsync_mode(map->index,
MAP_STORAGE(map)->set->parsed_fsync_mode, 0);
mail_index_set_lock_method(map->index,
MAP_STORAGE(map)->set->parsed_lock_method,
mail_storage_get_lock_timeout(MAP_STORAGE(map), UINT_MAX));
map->root_list = root_list;
map->map_ext_id = mail_index_ext_register(map->index, "map",
sizeof(struct mdbox_map_mail_index_header),
sizeof(struct mdbox_map_mail_index_record),
sizeof(uint32_t));
map->ref_ext_id = mail_index_ext_register(map->index, "ref", 0,
sizeof(uint16_t), sizeof(uint16_t));
return map;
}
void mdbox_map_deinit(struct mdbox_map **_map)
{
struct mdbox_map *map = *_map;
*_map = NULL;
if (map->view != NULL) {
mail_index_view_close(&map->view);
mail_index_close(map->index);
}
mail_index_free(&map->index);
i_free(map->index_path);
i_free(map->path);
i_free(map);
}
static int mdbox_map_mkdir_storage(struct mdbox_map *map)
{
if (mailbox_list_mkdir_root(map->root_list, map->path,
MAILBOX_LIST_PATH_TYPE_DIR) < 0) {
mail_storage_copy_list_error(MAP_STORAGE(map), map->root_list);
return -1;
}
if (strcmp(map->path, map->index_path) != 0 &&
mailbox_list_mkdir_root(map->root_list, map->index_path,
MAILBOX_LIST_PATH_TYPE_INDEX) < 0) {
mail_storage_copy_list_error(MAP_STORAGE(map), map->root_list);
return -1;
}
return 0;
}
static void mdbox_map_cleanup(struct mdbox_map *map)
{
unsigned int interval =
MAP_STORAGE(map)->set->mail_temp_scan_interval;
struct stat st;
if (stat(map->path, &st) < 0)
return;
/* check once in a while if there are temp files to clean up */
if (interval == 0) {
/* disabled */
} else if (st.st_atime > st.st_ctime + DBOX_TMP_DELETE_SECS) {
/* there haven't been any changes to this directory since we
last checked it. */
} else if (st.st_atime < ioloop_time - (time_t)interval) {
/* time to scan */
(void)unlink_old_files(map->path, DBOX_TEMP_FILE_PREFIX,
ioloop_time - DBOX_TMP_DELETE_SECS);
}
}
static int mdbox_map_open_internal(struct mdbox_map *map, bool create_missing)
{
enum mail_index_open_flags open_flags;
struct mailbox_permissions perm;
int ret = 0;
if (map->view != NULL) {
/* already opened */
return 1;
}
mailbox_list_get_root_permissions(map->root_list, &perm);
mail_index_set_permissions(map->index, perm.file_create_mode,
perm.file_create_gid,
perm.file_create_gid_origin);
open_flags = MAIL_INDEX_OPEN_FLAG_NEVER_IN_MEMORY |
mail_storage_settings_to_index_flags(MAP_STORAGE(map)->set);
if (create_missing) {
if ((ret = mdbox_map_mkdir_storage(map)) < 0)
return -1;
if (ret > 0) {
/* storage/ directory already existed.
the index should exist also. */
} else {
open_flags |= MAIL_INDEX_OPEN_FLAG_CREATE;
}
}
ret = mail_index_open(map->index, open_flags);
if (ret == 0 && create_missing) {
/* storage/ already existed, but indexes didn't. we'll need to
take extra steps to make sure we won't overwrite any m.*
files that may already exist. */
map->verify_existing_file_ids = TRUE;
open_flags |= MAIL_INDEX_OPEN_FLAG_CREATE;
ret = mail_index_open(map->index, open_flags);
}
if (ret < 0) {
mail_storage_set_index_error(MAP_STORAGE(map), map->index);
return -1;
}
if (ret == 0) {
/* index not found - for now just return failure */
i_assert(!create_missing);
return 0;
}
map->view = mail_index_view_open(map->index);
mdbox_map_cleanup(map);
if (mail_index_get_header(map->view)->uid_validity == 0) {
if (mdbox_map_generate_uid_validity(map) < 0) {
mail_storage_set_index_error(MAP_STORAGE(map), map->index);
mail_index_close(map->index);
return -1;
}
if (mdbox_map_refresh(map) < 0) {
mail_index_close(map->index);
return -1;
}
}
return 1;
}
int mdbox_map_open(struct mdbox_map *map)
{
return mdbox_map_open_internal(map, FALSE);
}
int mdbox_map_open_or_create(struct mdbox_map *map)
{
return mdbox_map_open_internal(map, TRUE) <= 0 ? -1 : 0;
}
int mdbox_map_refresh(struct mdbox_map *map)
{
struct mail_index_view_sync_ctx *ctx;
bool delayed_expunges, fscked;
int ret = 0;
/* some open files may have read partially written mails. now that
map syncing makes the new mails visible, we need to make sure the
partial data is flushed out of memory */
mdbox_files_sync_input(map->storage);
if (mail_index_refresh(map->view->index) < 0) {
mail_storage_set_index_error(MAP_STORAGE(map), map->index);
return -1;
}
if (mail_index_view_get_transaction_count(map->view) > 0) {
/* can't sync when there are transactions */
return 0;
}
ctx = mail_index_view_sync_begin(map->view,
MAIL_INDEX_VIEW_SYNC_FLAG_FIX_INCONSISTENT);
fscked = mail_index_reset_fscked(map->view->index);
if (mail_index_view_sync_commit(&ctx, &delayed_expunges) < 0) {
mail_storage_set_index_error(MAP_STORAGE(map), map->index);
ret = -1;
}
if (fscked)
mdbox_storage_set_corrupted(map->storage);
return ret;
}
bool mdbox_map_is_fscked(struct mdbox_map *map)
{
const struct mail_index_header *hdr;
if (map->view == NULL) {
/* map isn't opened yet. don't bother. */
return FALSE;
}
hdr = mail_index_get_header(map->view);
return (hdr->flags & MAIL_INDEX_HDR_FLAG_FSCKD) != 0;
}
static void
mdbox_map_get_ext_hdr(struct mdbox_map *map, struct mail_index_view *view,
struct mdbox_map_mail_index_header *hdr_r)
{
const void *data;
size_t data_size;
mail_index_get_header_ext(view, map->map_ext_id, &data, &data_size);
i_zero(hdr_r);
memcpy(hdr_r, data, I_MIN(data_size, sizeof(*hdr_r)));
}
uint32_t mdbox_map_get_rebuild_count(struct mdbox_map *map)
{
struct mdbox_map_mail_index_header hdr;
mdbox_map_get_ext_hdr(map, map->view, &hdr);
return hdr.rebuild_count;
}
static int
mdbox_map_lookup_seq(struct mdbox_map *map, uint32_t seq,
const struct mdbox_map_mail_index_record **rec_r)
{
const struct mdbox_map_mail_index_record *rec;
const void *data;
uint32_t uid;
mail_index_lookup_ext(map->view, seq, map->map_ext_id, &data, NULL);
rec = data;
if (rec == NULL || rec->file_id == 0) {
mail_index_lookup_uid(map->view, seq, &uid);
mdbox_map_set_corrupted(map, "file_id=0 for map_uid=%u", uid);
return -1;
}
*rec_r = rec;
return 0;
}
static int
mdbox_map_get_seq(struct mdbox_map *map, uint32_t map_uid, uint32_t *seq_r)
{
if (!mail_index_lookup_seq(map->view, map_uid, seq_r)) {
/* not found - try again after a refresh */
if (mdbox_map_refresh(map) < 0)
return -1;
if (!mail_index_lookup_seq(map->view, map_uid, seq_r))
return 0;
}
return 1;
}
int mdbox_map_lookup(struct mdbox_map *map, uint32_t map_uid,
uint32_t *file_id_r, uoff_t *offset_r)
{
const struct mdbox_map_mail_index_record *rec;
uint32_t seq;
int ret;
if (mdbox_map_open_or_create(map) < 0)
return -1;
if ((ret = mdbox_map_get_seq(map, map_uid, &seq)) <= 0)
return ret;
if (mdbox_map_lookup_seq(map, seq, &rec) < 0)
return -1;
*file_id_r = rec->file_id;
*offset_r = rec->offset;
return 1;
}
int mdbox_map_lookup_full(struct mdbox_map *map, uint32_t map_uid,
struct mdbox_map_mail_index_record *rec_r,
uint16_t *refcount_r)
{
uint32_t seq;
int ret;
if (mdbox_map_open_or_create(map) < 0)
return -1;
if ((ret = mdbox_map_get_seq(map, map_uid, &seq)) <= 0)
return ret;
return mdbox_map_lookup_seq_full(map, seq, rec_r, refcount_r);
}
int mdbox_map_lookup_seq_full(struct mdbox_map *map, uint32_t seq,
struct mdbox_map_mail_index_record *rec_r,
uint16_t *refcount_r)
{
const struct mdbox_map_mail_index_record *rec;
const uint16_t *ref16_p;
const void *data;
if (mdbox_map_lookup_seq(map, seq, &rec) < 0)
return -1;
*rec_r = *rec;
mail_index_lookup_ext(map->view, seq, map->ref_ext_id, &data, NULL);
if (data == NULL) {
mdbox_map_set_corrupted(map, "missing ref extension");
return -1;
}
ref16_p = data;
*refcount_r = *ref16_p;
return 1;
}
uint32_t mdbox_map_lookup_uid(struct mdbox_map *map, uint32_t seq)
{
uint32_t uid;
mail_index_lookup_uid(map->view, seq, &uid);
return uid;
}
unsigned int mdbox_map_get_messages_count(struct mdbox_map *map)
{
return mail_index_view_get_messages_count(map->view);
}
int mdbox_map_view_lookup_rec(struct mdbox_map *map,
struct mail_index_view *view, uint32_t seq,
struct dbox_mail_lookup_rec *rec_r)
{
const uint16_t *ref16_p;
const void *data;
i_zero(rec_r);
mail_index_lookup_uid(view, seq, &rec_r->map_uid);
mail_index_lookup_ext(view, seq, map->map_ext_id, &data, NULL);
if (data == NULL) {
mdbox_map_set_corrupted(map, "missing map extension");
return -1;
}
memcpy(&rec_r->rec, data, sizeof(rec_r->rec));
mail_index_lookup_ext(view, seq, map->ref_ext_id, &data, NULL);
if (data == NULL) {
mdbox_map_set_corrupted(map, "missing ref extension");
return -1;
}
ref16_p = data;
rec_r->refcount = *ref16_p;
return 0;
}
int mdbox_map_get_file_msgs(struct mdbox_map *map, uint32_t file_id,
ARRAY_TYPE(mdbox_map_file_msg) *recs)
{
const struct mail_index_header *hdr;
struct dbox_mail_lookup_rec rec;
struct mdbox_map_file_msg msg;
uint32_t seq;
if (mdbox_map_refresh(map) < 0)
return -1;
hdr = mail_index_get_header(map->view);
i_zero(&msg);
for (seq = 1; seq <= hdr->messages_count; seq++) {
if (mdbox_map_view_lookup_rec(map, map->view, seq, &rec) < 0)
return -1;
if (rec.rec.file_id == file_id) {
msg.map_uid = rec.map_uid;
msg.offset = rec.rec.offset;
msg.refcount = rec.refcount;
array_append(recs, &msg, 1);
}
}
return 0;
}
int mdbox_map_get_zero_ref_files(struct mdbox_map *map,
ARRAY_TYPE(seq_range) *file_ids_r)
{
const struct mail_index_header *hdr;
const struct mdbox_map_mail_index_record *rec;
const uint16_t *ref16_p;
const void *data;
uint32_t seq;
bool expunged;
int ret;
if ((ret = mdbox_map_open(map)) <= 0) {
/* no map / internal error */
return ret;
}
if (mdbox_map_refresh(map) < 0)
return -1;
hdr = mail_index_get_header(map->view);
for (seq = 1; seq <= hdr->messages_count; seq++) {
mail_index_lookup_ext(map->view, seq, map->ref_ext_id,
&data, &expunged);
if (data != NULL && !expunged) {
ref16_p = data;
if (*ref16_p != 0)
continue;
}
mail_index_lookup_ext(map->view, seq, map->map_ext_id,
&data, &expunged);
if (data != NULL && !expunged) {
rec = data;
seq_range_array_add(file_ids_r, rec->file_id);
}
}
return 0;
}
struct mdbox_map_atomic_context *mdbox_map_atomic_begin(struct mdbox_map *map)
{
struct mdbox_map_atomic_context *atomic;
atomic = i_new(struct mdbox_map_atomic_context, 1);
atomic->map = map;
return atomic;
}
static void
mdbox_map_sync_handle(struct mdbox_map *map,
struct mail_index_sync_ctx *sync_ctx)
{
struct mail_index_sync_rec sync_rec;
uint32_t seq1, seq2;
uoff_t offset1, offset2;
mail_index_sync_get_offsets(sync_ctx, &seq1, &offset1, &seq2, &offset2);
if (offset1 != offset2 || seq1 != seq2) {
/* something had crashed. need a full resync. */
i_warning("mdbox %s: Inconsistency in map index "
"(%u,%"PRIuUOFF_T" != %u,%"PRIuUOFF_T")",
map->path, seq1, offset1, seq2, offset2);
mdbox_storage_set_corrupted(map->storage);
}
while (mail_index_sync_next(sync_ctx, &sync_rec)) ;
}
int mdbox_map_atomic_lock(struct mdbox_map_atomic_context *atomic,
const char *reason)
{
int ret;
if (atomic->locked)
return 0;
if (mdbox_map_open_or_create(atomic->map) < 0)
return -1;
/* use syncing to lock the transaction log, so that we always see
log's head_offset = tail_offset */
ret = mail_index_sync_begin(atomic->map->index, &atomic->sync_ctx,
&atomic->sync_view, &atomic->sync_trans,
MAIL_INDEX_SYNC_FLAG_UPDATE_TAIL_OFFSET);
if (mail_index_reset_fscked(atomic->map->index))
mdbox_storage_set_corrupted(atomic->map->storage);
if (ret <= 0) {
i_assert(ret != 0);
mail_storage_set_index_error(MAP_STORAGE(atomic->map),
atomic->map->index);
return -1;
}
mail_index_sync_set_reason(atomic->sync_ctx, reason);
atomic->locked = TRUE;
/* reset refresh state so that if it's wanted to be done locked,
it gets the latest changes */
atomic->map_refreshed = FALSE;
mdbox_map_sync_handle(atomic->map, atomic->sync_ctx);
return 0;
}
bool mdbox_map_atomic_is_locked(struct mdbox_map_atomic_context *atomic)
{
return atomic->locked;
}
void mdbox_map_atomic_set_failed(struct mdbox_map_atomic_context *atomic)
{
atomic->success = FALSE;
atomic->failed = TRUE;
}
void mdbox_map_atomic_set_success(struct mdbox_map_atomic_context *atomic)
{
if (!atomic->failed)
atomic->success = TRUE;
}
void mdbox_map_atomic_unset_fscked(struct mdbox_map_atomic_context *atomic)
{
mail_index_unset_fscked(atomic->sync_trans);
}
int mdbox_map_atomic_finish(struct mdbox_map_atomic_context **_atomic)
{
struct mdbox_map_atomic_context *atomic = *_atomic;
int ret = 0;
*_atomic = NULL;
if (atomic->sync_ctx == NULL) {
/* not locked */
i_assert(!atomic->locked);
} else if (atomic->success) {
if (mail_index_sync_commit(&atomic->sync_ctx) < 0) {
mail_storage_set_index_error(MAP_STORAGE(atomic->map),
atomic->map->index);
ret = -1;
}
} else {
mail_index_sync_rollback(&atomic->sync_ctx);
}
i_free(atomic);
return ret;
}
struct mdbox_map_transaction_context *
mdbox_map_transaction_begin(struct mdbox_map_atomic_context *atomic,
bool external)
{
struct mdbox_map_transaction_context *ctx;
enum mail_index_transaction_flags flags =
MAIL_INDEX_TRANSACTION_FLAG_FSYNC;
bool success;
if (external)
flags |= MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL;
ctx = i_new(struct mdbox_map_transaction_context, 1);
ctx->atomic = atomic;
if (atomic->locked && atomic->map_refreshed) {
/* already refreshed within a lock, don't do it again */
success = TRUE;
} else {
success = mdbox_map_open(atomic->map) > 0 &&
mdbox_map_refresh(atomic->map) == 0;
}
if (success) {
atomic->map_refreshed = TRUE;
ctx->trans = mail_index_transaction_begin(atomic->map->view,
flags);
}
return ctx;
}
int mdbox_map_transaction_commit(struct mdbox_map_transaction_context *ctx,
const char *reason)
{
i_assert(!ctx->committed);
ctx->committed = TRUE;
if (!ctx->changed)
return 0;
if (mdbox_map_atomic_lock(ctx->atomic, reason) < 0)
return -1;
if (mail_index_transaction_commit(&ctx->trans) < 0) {
mail_storage_set_index_error(MAP_STORAGE(ctx->atomic->map),
ctx->atomic->map->index);
return -1;
}
mdbox_map_atomic_set_success(ctx->atomic);
return 0;
}
void mdbox_map_transaction_free(struct mdbox_map_transaction_context **_ctx)
{
struct mdbox_map_transaction_context *ctx = *_ctx;
*_ctx = NULL;
if (ctx->trans != NULL)
mail_index_transaction_rollback(&ctx->trans);
i_free(ctx);
}
int mdbox_map_update_refcount(struct mdbox_map_transaction_context *ctx,
uint32_t map_uid, int diff)
{
struct mdbox_map *map = ctx->atomic->map;
const void *data;
uint32_t seq;
int old_diff, new_diff;
if (unlikely(ctx->trans == NULL))
return -1;
if (!mail_index_lookup_seq(map->view, map_uid, &seq)) {
/* we can't refresh map here since view has a
transaction open. */
if (diff > 0) {
/* the message was probably just purged */
mail_storage_set_error(MAP_STORAGE(map), MAIL_ERROR_EXPUNGED,
"Some of the requested messages no longer exist.");
} else {
mdbox_map_set_corrupted(map,
"refcount update lost map_uid=%u", map_uid);
}
return -1;
}
mail_index_lookup_ext(map->view, seq, map->ref_ext_id, &data, NULL);
old_diff = data == NULL ? 0 : *((const uint16_t *)data);
ctx->changed = TRUE;
new_diff = mail_index_atomic_inc_ext(ctx->trans, seq,
map->ref_ext_id, diff);
if (old_diff + new_diff < 0) {
mdbox_map_set_corrupted(map, "map_uid=%u refcount too low",
map_uid);
return -1;
}
if (old_diff + new_diff >= 32768 && new_diff > 0) {
/* we're getting close to the 64k limit. fail early
to make it less likely that two processes increase
the refcount enough times to cross the limit */
mail_storage_set_error(MAP_STORAGE(map), MAIL_ERROR_LIMIT,
t_strdup_printf("Message has been copied too many times (%d + %d)",
old_diff, new_diff));
return -1;
}
return 0;
}
int mdbox_map_update_refcounts(struct mdbox_map_transaction_context *ctx,
const ARRAY_TYPE(uint32_t) *map_uids, int diff)
{
const uint32_t *uidp;
unsigned int i, count;
if (unlikely(ctx->trans == NULL))
return -1;
count = array_count(map_uids);
for (i = 0; i < count; i++) {
uidp = array_idx(map_uids, i);
if (mdbox_map_update_refcount(ctx, *uidp, diff) < 0)
return -1;
}
return 0;
}
int mdbox_map_remove_file_id(struct mdbox_map *map, uint32_t file_id)
{
struct mdbox_map_atomic_context *atomic;
struct mdbox_map_transaction_context *map_trans;
const struct mail_index_header *hdr;
const struct mdbox_map_mail_index_record *rec;
const void *data;
uint32_t seq;
int ret = 0;
/* make sure the map is refreshed, otherwise we might be expunging
messages that have already been moved to other files. */
/* we need a per-file transaction, otherwise we can't refresh the map */
atomic = mdbox_map_atomic_begin(map);
map_trans = mdbox_map_transaction_begin(atomic, TRUE);
hdr = mail_index_get_header(map->view);
for (seq = 1; seq <= hdr->messages_count; seq++) {
mail_index_lookup_ext(map->view, seq, map->map_ext_id,
&data, NULL);
if (data == NULL) {
mdbox_map_set_corrupted(map, "missing map extension");
ret = -1;
break;
}
rec = data;
if (rec->file_id == file_id) {
map_trans->changed = TRUE;
mail_index_expunge(map_trans->trans, seq);
}
}
if (ret == 0)
ret = mdbox_map_transaction_commit(map_trans, "removing file");
mdbox_map_transaction_free(&map_trans);
if (mdbox_map_atomic_finish(&atomic) < 0)
ret = -1;
return ret;
}
struct mdbox_map_append_context *
mdbox_map_append_begin(struct mdbox_map_atomic_context *atomic)
{
struct mdbox_map_append_context *ctx;
ctx = i_new(struct mdbox_map_append_context, 1);
ctx->atomic = atomic;
ctx->map = atomic->map;
ctx->first_new_file_id = (uint32_t)-1;
i_array_init(&ctx->file_appends, 64);
i_array_init(&ctx->files, 64);
i_array_init(&ctx->appends, 128);
if (mdbox_map_open_or_create(atomic->map) < 0)
ctx->failed = TRUE;
else {
/* refresh the map so we can try appending to the
latest files */
if (mdbox_map_refresh(atomic->map) == 0)
atomic->map_refreshed = TRUE;
else
ctx->failed = TRUE;
}
return ctx;
}
static time_t day_begin_stamp(unsigned int interval)
{
struct tm tm;
time_t stamp;
unsigned int unit = 1;
if (interval == 0)
return 0;
/* get the beginning of day/hour/minute depending on how large
the interval is */
tm = *localtime(&ioloop_time);
if (interval >= 60) {
tm.tm_sec = 0;
unit = 60;
if (interval >= 3600) {
tm.tm_min = 0;
unit = 3600;
if (interval >= 3600*24) {
tm.tm_hour = 0;
unit = 3600*24;
}
}
}
stamp = mktime(&tm);
if (stamp == (time_t)-1)
i_panic("mktime(today) failed");
return stamp - (interval - unit);
}
static bool dbox_try_open(struct dbox_file *file, bool want_altpath)
{
bool notfound;
if (want_altpath) {
if (dbox_file_open(file, ¬found) <= 0)
return FALSE;
} else {
if (dbox_file_open_primary(file, ¬found) <= 0)
return FALSE;
}
if (notfound)
return FALSE;
if (file->lock != NULL) {
/* already locked, we're possibly in the middle of purging it
in which case we really don't want to write there. */
return FALSE;
}
if (dbox_file_is_in_alt(file) != want_altpath) {
/* different alt location than what we want, can't use it */
return FALSE;
}
return TRUE;
}
static bool dbox_file_is_ok_at(struct dbox_file *file, uoff_t offset)
{
bool last;
int ret;
if (dbox_file_seek(file, offset) == 0)
return FALSE;
while ((ret = dbox_file_seek_next(file, &offset, &last)) > 0);
if (ret == 0 && !last)
return FALSE;
return TRUE;
}
static bool
mdbox_map_file_try_append(struct mdbox_map_append_context *ctx,
bool want_altpath,
const struct mdbox_map_mail_index_record *rec,
time_t stamp, uoff_t mail_size,
struct dbox_file_append_context **file_append_r,
struct ostream **output_r, bool *retry_later_r)
{
struct mdbox_map *map = ctx->map;
struct mdbox_storage *storage = map->storage;
struct dbox_file *file;
struct dbox_file_append_context *file_append;
struct stat st;
bool file_too_old = FALSE;
int ret;
*file_append_r = NULL;
*output_r = NULL;
*retry_later_r = FALSE;
file = mdbox_file_init(storage, rec->file_id);
if (!dbox_try_open(file, want_altpath)) {
dbox_file_unref(&file);
return TRUE;
}
if (file->create_time < stamp)
file_too_old = TRUE;
else if ((ret = dbox_file_try_lock(file)) <= 0) {
/* locking failed */
*retry_later_r = ret == 0;
} else if (stat(file->cur_path, &st) < 0) {
if (errno != ENOENT)
i_error("stat(%s) failed: %m", file->cur_path);
/* the file was unlinked between opening and locking it. */
} else if (st.st_size != rec->offset + rec->size &&
/* check if there's any garbage at the end of file.
note that there may be valid messages added by another
session before we locked it (but after we refreshed
map index). */
!dbox_file_is_ok_at(file, rec->offset + rec->size)) {
/* error message was already logged */
} else {
file_append = dbox_file_append_init(file);
if (dbox_file_get_append_stream(file_append, output_r) <= 0) {
/* couldn't append to this file */
} else if ((*output_r)->offset + mail_size > map->set->mdbox_rotate_size) {
/* file was too large after all */
} else {
/* success */
*file_append_r = file_append;
return TRUE;
}
dbox_file_append_rollback(&file_append);
}
/* failure */
dbox_file_unlock(file);
dbox_file_unref(&file);
return !file_too_old;
}
static bool
mdbox_map_is_appending(struct mdbox_map_append_context *ctx, uint32_t file_id)
{
struct dbox_file_append_context *const *file_appends;
unsigned int i, count;
/* there shouldn't be many files open, don't bother with anything
faster. */
file_appends = array_get(&ctx->file_appends, &count);
for (i = 0; i < count; i++) {
struct mdbox_file *mfile =
(struct mdbox_file *)file_appends[i]->file;
if (mfile->file_id == file_id)
return TRUE;
}
return FALSE;
}
static struct dbox_file_append_context *
mdbox_map_find_existing_append(struct mdbox_map_append_context *ctx,
uoff_t mail_size, bool want_altpath,
struct ostream **output_r)
{
struct mdbox_map *map = ctx->map;
struct dbox_file_append_context *const *file_appends, *append;
struct mdbox_file *mfile;
unsigned int i, count;
uoff_t append_offset;
/* first try to use files already used in this append */
file_appends = array_get(&ctx->file_appends, &count);
for (i = count; i > ctx->files_nonappendable_count; i--) {
append = file_appends[i-1];
if (dbox_file_is_in_alt(append->file) != want_altpath)
continue;
if (append->file->fd == -1) {
/* already closed it (below). we might be able to still
fit some small mail there, but that's too much
trouble */
continue;
}
append_offset = append->output->offset;
if (append_offset + mail_size <= map->set->mdbox_rotate_size &&
dbox_file_get_append_stream(append, output_r) > 0)
return append;
/* can't append to this file anymore. if we created this file,
close it so we don't waste fds. if we didn't, we can't close
it without also losing our lock too early. */
mfile = (struct mdbox_file *)append->file;
if (mfile->file_id == 0 && dbox_file_append_flush(append) == 0)
dbox_file_close(append->file);
}
ctx->files_nonappendable_count = count;
return NULL;
}
static int
mdbox_map_find_primary_files(struct mdbox_map_append_context *ctx,
ARRAY_TYPE(seq_range) *file_ids_r)
{
struct mdbox_storage *dstorage = ctx->map->storage;
struct mail_storage *storage = &dstorage->storage.storage;
DIR *dir;
struct dirent *d;
uint32_t file_id;
int ret = 0;
/* we want to quickly find the latest alt file, but we also want to
avoid accessing the alt storage as much as possible. typically most
of the older mails would be in alt storage, so we'll just put the
few m.* files in primary storage to checked_file_ids array. other
files are then known to exist in alt storage. */
dir = opendir(dstorage->storage_dir);
if (dir == NULL) {
mail_storage_set_critical(storage,
"opendir(%s) failed: %m", dstorage->storage_dir);
return -1;
}
for (errno = 0; (d = readdir(dir)) != NULL; errno = 0) {
if (strncmp(d->d_name, MDBOX_MAIL_FILE_PREFIX,
strlen(MDBOX_MAIL_FILE_PREFIX)) != 0)
continue;
if (str_to_uint32(d->d_name + strlen(MDBOX_MAIL_FILE_PREFIX),
&file_id) < 0)
continue;
seq_range_array_add(file_ids_r, file_id);
}
if (errno != 0) {
mail_storage_set_critical(storage,
"readdir(%s) failed: %m", dstorage->storage_dir);
ret = -1;
}
if (closedir(dir) < 0) {
mail_storage_set_critical(storage,