forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.c
1762 lines (1515 loc) · 43.8 KB
/
events.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
//! @file events.c
//!
//! incremental, orthogonal, paginated loom snapshots
//!
//! ### components
//!
//! - page: 16KB chunk of the loom.
//! - north segment (u3e_image, north.bin): low contiguous loom pages,
//! (in practice, the home road heap). indexed from low to high:
//! in-order on disk. in a file-backed mapping by default.
//! - south segment (u3e_image, south.bin): high contiguous loom pages,
//! (in practice, the home road stack). indexed from high to low:
//! reversed on disk.
//! - patch memory (memory.bin): new or changed pages since the last snapshot
//! - patch control (u3e_control control.bin): patch metadata, watermarks,
//! and indices/mugs for pages in patch memory.
//!
//! ### initialization (u3e_live())
//!
//! - with the loom already mapped, all pages are marked dirty in a bitmap.
//! - if snapshot is missing or partial, empty segments are created.
//! - if a patch is present, it's applied (crash recovery).
//! - snapshot segments are mapped or copied onto the loom;
//! all included pages are marked clean and protected (read-only).
//!
//! #### page faults (u3e_fault())
//!
//! - stores into protected pages generate faults (currently SIGSEGV,
//! handled outside this module).
//! - faults are handled by dirtying the page and switching protections to
//! read/write.
//! - a guard page is initially placed in the approximate middle of the free
//! space between the heap and stack at the time of the first page fault.
//! when a fault is detected in the guard page, the guard page is recentered
//! in the free space of the current road. if the guard page cannot be
//! recentered, then memory exhaustion has occurred.
//!
//! ### updates (u3e_save())
//!
//! - all updates to a snapshot are made through a patch.
//! - high/low watermarks for the north/south segments are established,
//! and dirty pages below/above them are added to the patch.
//! - modifications have been caught by the fault handler.
//! - newly-used pages are automatically included (preemptively dirtied).
//! - unused, innermost pages are reclaimed (segments are truncated to the
//! high/low watermarks; the last page in each is always adjacent to the
//! contiguous free space).
//! - patch pages are written to memory.bin, metadata to control.bin.
//! - the patch is applied to the snapshot segments, in-place.
//! - segments are fsync'd; patch files are deleted.
//! - memory protections (and file-backed mappings) are re-established.
//!
//! ### invariants
//!
//! definitions:
//! - a clean page is PROT_READ and 0 in the bitmap
//! - a dirty page is (PROT_READ|PROT_WRITE) and 1 in the bitmap
//! - the guard page is PROT_NONE and 1 in the bitmap
//!
//! assumptions:
//! - all memory access patterns are outside-in, a page at a time
//! - ad-hoc exceptions are supported by calling u3e_ward()
//!
//! - there is a single guard page, between the segments
//! - dirty pages only become clean by being:
//! - loaded from a snapshot during initialization
//! - present in a snapshot after save
//! - clean pages only become dirty by being:
//! - modified (and caught by the fault handler)
//! - orphaned due to segment truncation (explicitly dirtied)
//! - at points of quiescence (initialization, after save)
//! - all pages of the north and south segments are clean
//! - all other pages are dirty
//!
//! ### limitations
//!
//! - loom page size is fixed (16 KB), and must be a multiple of the
//! system page size.
//! - update atomicity is crucial:
//! - patch application must either completely succeed or
//! leave on-disk segments (memory image) intact.
//! - unapplied patches can be discarded (triggering event replay),
//! but once patch application begins it must succeed.
//! - may require integration into the overall signal-handling regime.
//! - any errors are handled with assertions; error messages are poor;
//! failed/partial writes are not retried.
//!
//! ### enhancements
//!
//! - use platform specific page fault mechanism (mach rpc, userfaultfd, &c).
//! - parallelism (conflicts with demand paging)
//!
#include "events.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "log.h"
#include "manage.h"
#include "options.h"
#include "retrieve.h"
#include "types.h"
/* _ce_len: byte length of pages
** _ce_len_words: word length of pages
** _ce_page: byte length of a single page
** _ce_ptr: void pointer to a page
*/
#define _ce_len(i) ((size_t)(i) << (u3a_page + 2))
#define _ce_len_words(i) ((size_t)(i) << u3a_page)
#define _ce_page _ce_len(1)
#define _ce_ptr(i) ((void *)((c3_c*)u3_Loom + _ce_len(i)))
/// Snapshotting system.
u3e_pool u3e_Pool;
static c3_l
_ce_mug_page(void* ptr_v)
{
// XX trailing zeros
// return u3r_mug_bytes(ptr_v, _ce_page);
return u3r_mug_words(ptr_v, _ce_len_words(1));
}
#ifdef U3_SNAPSHOT_VALIDATION
/* Image check.
*/
struct {
c3_w nor_w;
c3_w sou_w;
c3_w mug_w[u3a_pages];
} u3K;
/* u3e_check(): compute a checksum on all memory within the watermarks.
*/
void
u3e_check(c3_c* cap_c)
{
c3_w nor_w = 0;
c3_w sou_w = 0;
{
u3_post low_p, hig_p;
u3m_water(&low_p, &hig_p);
nor_w = (low_p + (_ce_len_words(1) - 1)) >> u3a_page;
sou_w = u3P.pag_w - (hig_p >> u3a_page);
}
/* compute checksum over active pages.
*/
{
c3_w i_w, sum_w, mug_w;
sum_w = 0;
for ( i_w = 0; i_w < nor_w; i_w++ ) {
mug_w = _ce_mug_page(_ce_ptr(i_w));
if ( strcmp(cap_c, "boot") ) {
u3_assert(mug_w == u3K.mug_w[i_w]);
}
sum_w += mug_w;
}
for ( i_w = 0; i_w < sou_w; i_w++ ) {
mug_w = _ce_mug_page(_ce_ptr((u3P.pag_w - (i_w + 1))));
if ( strcmp(cap_c, "boot") ) {
u3_assert(mug_w == u3K.mug_w[(u3P.pag_w - (i_w + 1))]);
}
sum_w += mug_w;
}
u3l_log("%s: sum %x (%x, %x)", cap_c, sum_w, nor_w, sou_w);
}
}
#endif
/* _ce_flaw_mmap(): remap non-guard page after fault.
*/
static inline c3_i
_ce_flaw_mmap(c3_w pag_w)
{
// NB: must be static, since the stack is grown via page faults, and
// we're already in a page fault handler.
//
static c3_y con_y[_ce_page];
// save contents of page, to be restored after the mmap
//
memcpy(con_y, _ce_ptr(pag_w), _ce_page);
// map the dirty page into the ephemeral file
//
if ( MAP_FAILED == mmap(_ce_ptr(pag_w),
_ce_page,
(PROT_READ | PROT_WRITE),
(MAP_FIXED | MAP_SHARED),
u3P.eph_i, _ce_len(pag_w)) )
{
fprintf(stderr, "loom: fault mmap failed (%u): %s\r\n",
pag_w, strerror(errno));
return 1;
}
// restore contents of page
//
memcpy(_ce_ptr(pag_w), con_y, _ce_page);
return 0;
}
/* _ce_flaw_mprotect(): protect page after fault.
*/
static inline c3_i
_ce_flaw_mprotect(c3_w pag_w)
{
if ( 0 != mprotect(_ce_ptr(pag_w), _ce_page, (PROT_READ | PROT_WRITE)) ) {
fprintf(stderr, "loom: fault mprotect (%u): %s\r\n",
pag_w, strerror(errno));
return 1;
}
return 0;
}
#ifdef U3_GUARD_PAGE
/* _ce_ward_protect(): protect the guard page.
*/
static inline c3_i
_ce_ward_protect(void)
{
if ( 0 != mprotect(_ce_ptr(u3P.gar_w), _ce_page, PROT_NONE) ) {
fprintf(stderr, "loom: failed to protect guard page (%u): %s\r\n",
u3P.gar_w, strerror(errno));
return 1;
}
return 0;
}
/* _ce_ward_post(): set the guard page.
*/
static inline c3_i
_ce_ward_post(c3_w nop_w, c3_w sop_w)
{
u3P.gar_w = nop_w + ((sop_w - nop_w) / 2);
return _ce_ward_protect();
}
/* _ce_ward_clip(): hit the guard page.
*/
static inline u3e_flaw
_ce_ward_clip(c3_w nop_w, c3_w sop_w)
{
c3_w old_w = u3P.gar_w;
if ( !u3P.gar_w || ((nop_w < u3P.gar_w) && (sop_w > u3P.gar_w)) ) {
fprintf(stderr, "loom: ward bogus (>%u %u %u<)\r\n",
nop_w, u3P.gar_w, sop_w);
return u3e_flaw_sham;
}
if ( sop_w <= (nop_w + 1) ) {
return u3e_flaw_meme;
}
if ( _ce_ward_post(nop_w, sop_w) ) {
return u3e_flaw_base;
}
u3_assert( old_w != u3P.gar_w );
return u3e_flaw_good;
}
#endif /* ifdef U3_GUARD_PAGE */
/* u3e_fault(): handle a memory fault.
*/
u3e_flaw
u3e_fault(u3_post low_p, u3_post hig_p, u3_post off_p)
{
c3_w pag_w = off_p >> u3a_page;
c3_w blk_w = pag_w >> 5;
c3_w bit_w = pag_w & 31;
#ifdef U3_GUARD_PAGE
c3_w gar_w = u3P.gar_w;
if ( pag_w == gar_w ) {
u3e_flaw fal_e = _ce_ward_clip(low_p >> u3a_page, hig_p >> u3a_page);
if ( u3e_flaw_good != fal_e ) {
return fal_e;
}
if ( !(u3P.dit_w[blk_w] & (1 << bit_w)) ) {
fprintf(stderr, "loom: strange guard (%d)\r\n", pag_w);
return u3e_flaw_sham;
}
if ( _ce_flaw_mprotect(pag_w) ) {
return u3e_flaw_base;
}
return u3e_flaw_good;
}
#endif
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
fprintf(stderr, "loom: strange page (%d): %x\r\n", pag_w, off_p);
return u3e_flaw_sham;
}
u3P.dit_w[blk_w] |= (1 << bit_w);
if ( u3P.eph_i ) {
if ( _ce_flaw_mmap(pag_w) ) {
return u3e_flaw_base;
}
}
else if ( _ce_flaw_mprotect(pag_w) ) {
return u3e_flaw_base;
}
return u3e_flaw_good;
}
typedef enum {
_ce_img_good = 0,
_ce_img_fail = 1,
_ce_img_size = 2
} _ce_img_stat;
/* _ce_image_stat(): measure image.
*/
static _ce_img_stat
_ce_image_stat(u3e_image* img_u, c3_w* pgs_w)
{
struct stat buf_u;
if ( -1 == fstat(img_u->fid_i, &buf_u) ) {
fprintf(stderr, "loom: stat %s: %s\r\n", img_u->nam_c, strerror(errno));
u3_assert(0);
return _ce_img_fail;
}
else {
c3_z siz_z = buf_u.st_size;
c3_z pgs_z = (siz_z + (_ce_page - 1)) >> (u3a_page + 2);
if ( !siz_z ) {
*pgs_w = 0;
return _ce_img_good;
}
else if ( siz_z != _ce_len(pgs_z) ) {
fprintf(stderr, "loom: %s corrupt size %zu\r\n", img_u->nam_c, siz_z);
return _ce_img_size;
}
else if ( pgs_z > UINT32_MAX ) {
fprintf(stderr, "loom: %s overflow %zu\r\n", img_u->nam_c, siz_z);
return _ce_img_fail;
}
else {
*pgs_w = (c3_w)pgs_z;
return _ce_img_good;
}
}
}
/* _ce_ephemeral_open(): open or create ephemeral file
*/
static c3_o
_ce_ephemeral_open(c3_i* eph_i)
{
c3_i mod_i = O_RDWR | O_CREAT;
c3_c ful_c[8193];
if ( u3C.eph_c == 0 ) {
snprintf(ful_c, 8192, "%s", u3P.dir_c);
c3_mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb", u3P.dir_c);
c3_mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/chk", u3P.dir_c);
c3_mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/chk/limbo.bin", u3P.dir_c);
u3C.eph_c = strdup(ful_c);
}
if ( -1 == (*eph_i = c3_open(u3C.eph_c, mod_i, 0666)) ) {
fprintf(stderr, "loom: ephemeral c3_open %s: %s\r\n", u3C.eph_c,
strerror(errno));
return c3n;
}
if ( ftruncate(*eph_i, _ce_len(u3P.pag_w)) < 0 ) {
fprintf(stderr, "loom: ephemeral ftruncate %s: %s\r\n", u3C.eph_c,
strerror(errno));
return c3n;
}
return c3y;
}
/* _ce_image_open(): open or create image.
*/
static _ce_img_stat
_ce_image_open(u3e_image* img_u, c3_c* ful_c)
{
c3_i mod_i = O_RDWR | O_CREAT;
c3_c pax_c[8192];
snprintf(pax_c, 8192, "%s/%s.bin", ful_c, img_u->nam_c);
if ( -1 == (img_u->fid_i = c3_open(pax_c, mod_i, 0666)) ) {
fprintf(stderr, "loom: c3_open %s: %s\r\n", pax_c, strerror(errno));
return _ce_img_fail;
}
return _ce_image_stat(img_u, &img_u->pgs_w);
}
/* _ce_patch_write_control(): write control block file.
*/
static void
_ce_patch_write_control(u3_ce_patch* pat_u)
{
ssize_t ret_i;
c3_w len_w = sizeof(u3e_control) +
(pat_u->con_u->pgs_w * sizeof(u3e_line));
if ( len_w != (ret_i = write(pat_u->ctl_i, pat_u->con_u, len_w)) ) {
if ( 0 < ret_i ) {
fprintf(stderr, "loom: patch ctl partial write: %zu\r\n", (size_t)ret_i);
}
else {
fprintf(stderr, "loom: patch ctl write: %s\r\n", strerror(errno));
}
u3_assert(0);
}
}
/* _ce_patch_read_control(): read control block file.
*/
static c3_o
_ce_patch_read_control(u3_ce_patch* pat_u)
{
c3_w len_w;
u3_assert(0 == pat_u->con_u);
{
struct stat buf_u;
if ( -1 == fstat(pat_u->ctl_i, &buf_u) ) {
u3_assert(0);
return c3n;
}
len_w = (c3_w) buf_u.st_size;
}
pat_u->con_u = c3_malloc(len_w);
if ( (len_w != read(pat_u->ctl_i, pat_u->con_u, len_w)) ||
(len_w != sizeof(u3e_control) +
(pat_u->con_u->pgs_w * sizeof(u3e_line))) )
{
c3_free(pat_u->con_u);
pat_u->con_u = 0;
return c3n;
}
return c3y;
}
/* _ce_patch_create(): create patch files.
*/
static void
_ce_patch_create(u3_ce_patch* pat_u)
{
c3_c ful_c[8193];
snprintf(ful_c, 8192, "%s", u3P.dir_c);
c3_mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb", u3P.dir_c);
c3_mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/chk/control.bin", u3P.dir_c);
if ( -1 == (pat_u->ctl_i = c3_open(ful_c, O_RDWR | O_CREAT | O_EXCL, 0600)) ) {
fprintf(stderr, "loom: patch c3_open control.bin: %s\r\n", strerror(errno));
u3_assert(0);
}
snprintf(ful_c, 8192, "%s/.urb/chk/memory.bin", u3P.dir_c);
if ( -1 == (pat_u->mem_i = c3_open(ful_c, O_RDWR | O_CREAT | O_EXCL, 0600)) ) {
fprintf(stderr, "loom: patch c3_open memory.bin: %s\r\n", strerror(errno));
u3_assert(0);
}
}
/* _ce_patch_delete(): delete a patch.
*/
static void
_ce_patch_delete(void)
{
c3_c ful_c[8193];
snprintf(ful_c, 8192, "%s/.urb/chk/control.bin", u3P.dir_c);
if ( unlink(ful_c) ) {
fprintf(stderr, "loom: failed to delete control.bin: %s\r\n",
strerror(errno));
}
snprintf(ful_c, 8192, "%s/.urb/chk/memory.bin", u3P.dir_c);
if ( unlink(ful_c) ) {
fprintf(stderr, "loom: failed to remove memory.bin: %s\r\n",
strerror(errno));
}
}
/* _ce_patch_verify(): check patch data mug.
*/
static c3_o
_ce_patch_verify(u3_ce_patch* pat_u)
{
c3_w pag_w, mug_w;
c3_y buf_y[_ce_page];
c3_zs ret_zs;
c3_o sou_o = c3n; // south seen
if ( U3P_VERLAT != pat_u->con_u->ver_w ) {
fprintf(stderr, "loom: patch version mismatch: have %"PRIc3_w", need %u\r\n",
pat_u->con_u->ver_w,
U3P_VERLAT);
return c3n;
}
if ( pat_u->con_u->sou_w > 1 ) {
fprintf(stderr, "loom: patch strange south size: %u\r\n",
pat_u->con_u->sou_w);
return c3n;
}
for ( c3_z i_z = 0; i_z < pat_u->con_u->pgs_w; i_z++ ) {
pag_w = pat_u->con_u->mem_u[i_z].pag_w;
mug_w = pat_u->con_u->mem_u[i_z].mug_w;
if ( _ce_page !=
(ret_zs = pread(pat_u->mem_i, buf_y, _ce_page, _ce_len(i_z))) )
{
if ( 0 < ret_zs ) {
fprintf(stderr, "loom: patch partial read: %"PRIc3_zs"\r\n", ret_zs);
}
else {
fprintf(stderr, "loom: patch read: fail %s\r\n", strerror(errno));
}
return c3n;
}
{
c3_w nug_w = _ce_mug_page(buf_y);
if ( mug_w != nug_w ) {
fprintf(stderr, "loom: patch mug mismatch"
" %"PRIc3_w"/%"PRIc3_z"; (%"PRIxc3_w", %"PRIxc3_w")\r\n",
pag_w, i_z, mug_w, nug_w);
return c3n;
}
#if 0
else {
u3l_log("verify: patch %"PRIc3_w"/%"PRIc3_z", %"PRIxc3_w"\r\n", pag_w, i_z, mug_w);
}
#endif
}
if ( pag_w >= pat_u->con_u->nor_w ) {
if ( c3n == sou_o ) {
sou_o = c3y;
}
else {
fprintf(stderr, "loom: patch multiple south pages\r\n");
return c3n;
}
}
}
return c3y;
}
/* _ce_patch_free(): free a patch.
*/
static void
_ce_patch_free(u3_ce_patch* pat_u)
{
c3_free(pat_u->con_u);
close(pat_u->ctl_i);
close(pat_u->mem_i);
c3_free(pat_u);
}
/* _ce_patch_open(): open patch, if any.
*/
static u3_ce_patch*
_ce_patch_open(void)
{
u3_ce_patch* pat_u;
c3_c ful_c[8193];
c3_i ctl_i, mem_i;
snprintf(ful_c, 8192, "%s", u3P.dir_c);
c3_mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb", u3P.dir_c);
c3_mkdir(ful_c, 0700);
snprintf(ful_c, 8192, "%s/.urb/chk/control.bin", u3P.dir_c);
if ( -1 == (ctl_i = c3_open(ful_c, O_RDWR)) ) {
return 0;
}
snprintf(ful_c, 8192, "%s/.urb/chk/memory.bin", u3P.dir_c);
if ( -1 == (mem_i = c3_open(ful_c, O_RDWR)) ) {
close(ctl_i);
_ce_patch_delete();
return 0;
}
pat_u = c3_malloc(sizeof(u3_ce_patch));
pat_u->ctl_i = ctl_i;
pat_u->mem_i = mem_i;
pat_u->con_u = 0;
if ( c3n == _ce_patch_read_control(pat_u) ) {
close(pat_u->ctl_i);
close(pat_u->mem_i);
c3_free(pat_u);
_ce_patch_delete();
return 0;
}
if ( c3n == _ce_patch_verify(pat_u) ) {
_ce_patch_free(pat_u);
_ce_patch_delete();
return 0;
}
return pat_u;
}
/* _ce_patch_write_page(): write a page of patch memory.
*/
static void
_ce_patch_write_page(u3_ce_patch* pat_u,
c3_w pgc_w,
c3_w* mem_w)
{
c3_zs ret_zs;
if ( _ce_page !=
(ret_zs = pwrite(pat_u->mem_i, mem_w, _ce_page, _ce_len(pgc_w))) )
{
if ( 0 < ret_zs ) {
fprintf(stderr, "loom: patch partial write: %"PRIc3_zs"\r\n", ret_zs);
}
else {
fprintf(stderr, "loom: patch write: fail: %s\r\n", strerror(errno));
}
fprintf(stderr, "info: you probably have insufficient disk space");
u3_assert(0);
}
}
/* _ce_patch_count_page(): count a page, producing new counter.
*/
static c3_w
_ce_patch_count_page(c3_w pag_w,
c3_w pgc_w)
{
c3_w blk_w = (pag_w >> 5);
c3_w bit_w = (pag_w & 31);
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
pgc_w += 1;
}
return pgc_w;
}
/* _ce_patch_save_page(): save a page, producing new page counter.
*/
static c3_w
_ce_patch_save_page(u3_ce_patch* pat_u,
c3_w pag_w,
c3_w pgc_w)
{
c3_w blk_w = (pag_w >> 5);
c3_w bit_w = (pag_w & 31);
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
c3_w* mem_w = _ce_ptr(pag_w);
pat_u->con_u->mem_u[pgc_w].pag_w = pag_w;
pat_u->con_u->mem_u[pgc_w].mug_w = _ce_mug_page(mem_w);
#if 0
fprintf(stderr, "loom: save page %d %x\r\n",
pag_w, pat_u->con_u->mem_u[pgc_w].mug_w);
#endif
_ce_patch_write_page(pat_u, pgc_w, mem_w);
pgc_w += 1;
}
return pgc_w;
}
/* _ce_patch_compose(): make and write current patch.
*/
static u3_ce_patch*
_ce_patch_compose(c3_w nor_w, c3_w sou_w)
{
c3_w pgs_w = 0;
#ifdef U3_SNAPSHOT_VALIDATION
u3K.nor_w = nor_w;
u3K.sou_w = sou_w;
#endif
/* Count dirty pages.
*/
{
c3_w i_w;
for ( i_w = 0; i_w < nor_w; i_w++ ) {
pgs_w = _ce_patch_count_page(i_w, pgs_w);
}
for ( i_w = 0; i_w < sou_w; i_w++ ) {
pgs_w = _ce_patch_count_page((u3P.pag_w - (i_w + 1)), pgs_w);
}
}
if ( !pgs_w ) {
return 0;
}
else {
u3_ce_patch* pat_u = c3_malloc(sizeof(u3_ce_patch));
c3_w i_w, pgc_w;
_ce_patch_create(pat_u);
pat_u->con_u = c3_malloc(sizeof(u3e_control) + (pgs_w * sizeof(u3e_line)));
pat_u->con_u->ver_w = U3P_VERLAT;
pgc_w = 0;
for ( i_w = 0; i_w < nor_w; i_w++ ) {
pgc_w = _ce_patch_save_page(pat_u, i_w, pgc_w);
}
for ( i_w = 0; i_w < sou_w; i_w++ ) {
pgc_w = _ce_patch_save_page(pat_u, (u3P.pag_w - (i_w + 1)), pgc_w);
}
u3_assert( pgc_w == pgs_w );
pat_u->con_u->nor_w = nor_w;
pat_u->con_u->sou_w = sou_w;
pat_u->con_u->pgs_w = pgc_w;
_ce_patch_write_control(pat_u);
return pat_u;
}
}
/* _ce_patch_sync(): make sure patch is synced to disk.
*/
static void
_ce_patch_sync(u3_ce_patch* pat_u)
{
if ( -1 == c3_sync(pat_u->ctl_i) ) {
fprintf(stderr, "loom: control file sync failed: %s\r\n",
strerror(errno));
u3_assert(!"loom: control sync");
}
if ( -1 == c3_sync(pat_u->mem_i) ) {
fprintf(stderr, "loom: patch file sync failed: %s\r\n",
strerror(errno));
u3_assert(!"loom: patch sync");
}
}
/* _ce_image_sync(): make sure image is synced to disk.
*/
static c3_o
_ce_image_sync(u3e_image* img_u)
{
if ( -1 == c3_sync(img_u->fid_i) ) {
fprintf(stderr, "loom: image (%s) sync failed: %s\r\n",
img_u->nam_c, strerror(errno));
return c3n;
}
return c3y;
}
/* _ce_image_resize(): resize image, truncating if it shrunk.
*/
static void
_ce_image_resize(u3e_image* img_u, c3_w pgs_w)
{
c3_z off_z = _ce_len(pgs_w);
off_t off_i = (off_t)off_z;
if ( img_u->pgs_w > pgs_w ) {
if ( off_z != (size_t)off_i ) {
fprintf(stderr, "loom: image (%s) truncate: "
"offset overflow (%" PRId64 ") for page %u\r\n",
img_u->nam_c, (c3_ds)off_i, pgs_w);
u3_assert(0);
}
if ( ftruncate(img_u->fid_i, off_i) ) {
fprintf(stderr, "loom: image (%s) truncate: %s\r\n",
img_u->nam_c, strerror(errno));
u3_assert(0);
}
}
img_u->pgs_w = pgs_w;
}
/* _ce_patch_apply(): apply patch to images.
*/
static void
_ce_patch_apply(u3_ce_patch* pat_u)
{
c3_zs ret_zs;
c3_w i_w;
// resize images
//
_ce_image_resize(&u3P.nor_u, pat_u->con_u->nor_w);
_ce_image_resize(&u3P.sou_u, pat_u->con_u->sou_w);
// seek to begining of patch
//
if ( -1 == lseek(pat_u->mem_i, 0, SEEK_SET) ) {
fprintf(stderr, "loom: patch apply seek: %s\r\n", strerror(errno));
u3_assert(0);
}
// write patch pages into the appropriate image
//
for ( i_w = 0; i_w < pat_u->con_u->pgs_w; i_w++ ) {
c3_w pag_w = pat_u->con_u->mem_u[i_w].pag_w;
c3_y buf_y[_ce_page];
c3_i fid_i;
c3_z off_z;
if ( pag_w < pat_u->con_u->nor_w ) {
fid_i = u3P.nor_u.fid_i;
off_z = _ce_len(pag_w);
}
// NB: this assumes that there never more than one south page,
// as enforced by _ce_patch_verify()
//
else {
fid_i = u3P.sou_u.fid_i;
off_z = 0;
}
if ( _ce_page != (ret_zs = read(pat_u->mem_i, buf_y, _ce_page)) ) {
if ( 0 < ret_zs ) {
fprintf(stderr, "loom: patch apply partial read: %"PRIc3_zs"\r\n",
ret_zs);
}
else {
fprintf(stderr, "loom: patch apply read: %s\r\n", strerror(errno));
}
u3_assert(0);
}
else {
if ( _ce_page !=
(ret_zs = pwrite(fid_i, buf_y, _ce_page, off_z)) )
{
if ( 0 < ret_zs ) {
fprintf(stderr, "loom: patch apply partial write: %"PRIc3_zs"\r\n",
ret_zs);
}
else {
fprintf(stderr, "loom: patch apply write: %s\r\n", strerror(errno));
}
fprintf(stderr, "info: you probably have insufficient disk space");
u3_assert(0);
}
}
#if 0
u3l_log("apply: %d, %x", pag_w, _ce_mug_page(buf_y));
#endif
}
}
/* _ce_loom_track_sane(): quiescent page state invariants.
*/
static c3_o
_ce_loom_track_sane(void)
{
c3_w blk_w, bit_w, max_w, i_w = 0;
c3_o san_o = c3y;
max_w = u3P.nor_u.pgs_w;
for ( ; i_w < max_w; i_w++ ) {
blk_w = i_w >> 5;
bit_w = i_w & 31;
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
fprintf(stderr, "loom: insane north %u\r\n", i_w);
san_o = c3n;
}
}
max_w = u3P.pag_w - u3P.sou_u.pgs_w;
for ( ; i_w < max_w; i_w++ ) {
blk_w = i_w >> 5;
bit_w = i_w & 31;
if ( !(u3P.dit_w[blk_w] & (1 << bit_w)) ) {
fprintf(stderr, "loom: insane open %u\r\n", i_w);
san_o = c3n;
}
}
max_w = u3P.pag_w;
for ( ; i_w < max_w; i_w++ ) {
blk_w = i_w >> 5;
bit_w = i_w & 31;
if ( u3P.dit_w[blk_w] & (1 << bit_w) ) {
fprintf(stderr, "loom: insane south %u\r\n", i_w);
san_o = c3n;
}
}
return san_o;
}
/* _ce_loom_track_north(): [pgs_w] clean, followed by [dif_w] dirty.
*/
void
_ce_loom_track_north(c3_w pgs_w, c3_w dif_w)
{
c3_w blk_w, bit_w, i_w = 0, max_w = pgs_w;
for ( ; i_w < max_w; i_w++ ) {
blk_w = i_w >> 5;
bit_w = i_w & 31;
u3P.dit_w[blk_w] &= ~(1 << bit_w);
}
max_w += dif_w;
for ( ; i_w < max_w; i_w++ ) {
blk_w = i_w >> 5;
bit_w = i_w & 31;
u3P.dit_w[blk_w] |= (1 << bit_w);
}
}
/* _ce_loom_track_south(): [pgs_w] clean, preceded by [dif_w] dirty.
*/
void
_ce_loom_track_south(c3_w pgs_w, c3_w dif_w)
{
c3_w blk_w, bit_w, i_w = u3P.pag_w - 1, max_w = u3P.pag_w - pgs_w;
for ( ; i_w >= max_w; i_w-- ) {
blk_w = i_w >> 5;
bit_w = i_w & 31;
u3P.dit_w[blk_w] &= ~(1 << bit_w);
}
max_w -= dif_w;
for ( ; i_w >= max_w; i_w-- ) {
blk_w = i_w >> 5;
bit_w = i_w & 31;
u3P.dit_w[blk_w] |= (1 << bit_w);
}
}
/* _ce_loom_protect_north(): protect/track pages from the bottom of memory.
*/
static void
_ce_loom_protect_north(c3_w pgs_w, c3_w old_w)
{
c3_w dif_w = 0;
if ( pgs_w ) {
if ( 0 != mprotect(_ce_ptr(0), _ce_len(pgs_w), PROT_READ) ) {
fprintf(stderr, "loom: pure north (%u pages): %s\r\n",
pgs_w, strerror(errno));
u3_assert(0);
}
}
if ( old_w > pgs_w ) {
dif_w = old_w - pgs_w;