-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacon.c
3197 lines (2951 loc) · 69.1 KB
/
pacon.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
/*
* written by Yubo
*/
#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include "pacon.h"
#include "kv/dmkv.h"
#include "./lib/cJSON.h"
#include "./lib/cuckoo/hash_table.h"
#define SERI_TYPE 0 // 0 is memcpy, 1 is json
#define BUFFER_SIZE 64
// opt type for commi
#define CHECKPOINT ":0"
#define MKDIR ":1"
#define CREATE ":2"
#define RM ":3"
#define RMDIR ":4"
#define READDIR ":5"
#define OWRITE ":6" // data size is larger than the INLINE_MAX, write it back to DFS
#define FSYNC ":7"
#define WRITE ":8"
#define BARRIER ":9"
#define RENAME ":A"
#define FLUSHDIR ":B"
// opt type for permission check
#define READDIR_PC 0
#define MKDIR_PC 1
#define CREATE_PC 2
#define RM_PC 3
#define RMDIR_PC 4
#define LINK_PC 5
#define OWRITE_PC 6 // data size is larger than the INLINE_MAX, write it back to DFS
#define FSYNC_PC 7
#define READ_PC 8
#define WRITE_PC 9
#define RW_PC 10
#define CREATEW_PC 11
#define RENAME_PC 12
#define DEFAULT_FILEMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH
#define DEFAULT_DIRMORE S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IXOTH
#define SECTOR_SIZE 512
#define TIMESTAMP_SIZE 11
static struct dmkv *kv_handle;
static char mount_path[MOUNT_PATH_MAX];
static int client_num = 0;
static int current_barrier_id = 0;
/**************** get and set stat flags *****************/
enum statflags
{
STAT_type, // 0: dir, 1: file
STAT_inline, // 0: no inline data, 1: has inlien
STAT_commit, // 0: not yet be committed, 1: already be committed
STAT_file_created, // 0: not be created in DFS, 1: already be created in DFS
STAT_existedin_dfs, // 0: a new item after pacon run, 1: it was created before pacon run
STAT_child_check, // 0: haven't check its children, 1: already checked its children
STAT_rm,
};
void set_stat_flag(struct pacon_stat *p_st, int flag_type, int val)
{
if (val == 0)
{
// set 0
p_st->flags &= ~(1UL << flag_type);
} else {
// set 1
p_st->flags |= 1UL << flag_type;
}
}
int get_stat_flag(struct pacon_stat *p_st, int flag_type)
{
if (((p_st->flags >> flag_type) & 1) == 1 )
{
return 1;
} else {
return 0;
}
}
void set_fstat_flag(struct pacon_file *p_st, int flag_type, int val)
{
if (val == 0)
{
// set 0
p_st->flags &= ~(1UL << flag_type);
} else {
// set 1
p_st->flags |= 1UL << flag_type;
}
}
int get_fstat_flag(struct pacon_file *p_st, int flag_type)
{
if (((p_st->flags >> flag_type) & 1) == 1 )
{
return 1;
} else {
return 0;
}
}
/*********** serialization and deserialization ***********/
int seri_val(struct pacon_stat *s, char *val)
{
//int size = sizeof(struct pacon_stat);
memcpy(val, s, PSTAT_SIZE);
return PSTAT_SIZE;
}
int deseri_val(struct pacon_stat *s, char *val)
{
//int size = sizeof(struct pacon_stat);
memcpy(s, val, PSTAT_SIZE);
return 0;
}
// seri pacom_stat and inline_data to val
int seri_inline_data(struct pacon_stat *s, char *inline_data, char *val)
{
//int size = sizeof(struct pacon_stat);
if (s != NULL)
{
memcpy(val, s, PSTAT_SIZE);
}
memcpy(val + PSTAT_SIZE, inline_data, strlen(inline_data)+1);
//memcpy(val + PSTAT_SIZE, inline_data, strlen(inline_data));
return PSTAT_SIZE + INLINE_MAX;
}
// deseri val to pacon_stat and inline_data
int deseri_inline_data(struct pacon_stat *s, char *inline_data, char *val)
{
//int size = sizeof(struct pacon_stat);
if (s != NULL)
{
memcpy(s, val, PSTAT_SIZE);
}
strcpy(inline_data, val + PSTAT_SIZE);
//memcpy(inline_data, val + PSTAT_SIZE, strlen(val) - PSTAT_SIZE);
return PSTAT_SIZE + INLINE_MAX;
}
/*********** parent dir check cache ***********/
/* only cache the result of parent check */
int add_to_dir_check_table(char *parent_dir)
{
int flags = 0;
pdirht_put(parent_dir, flags);
return 0;
}
/* 0 is hit, -1 is miss */
int dir_check_local(char *parent_dir)
{
int ret;
ret = pdirht_get(parent_dir);
return ret;
}
/************ some tool funcs ************/
/*
* ret = 1 means recursive sub file/dir, 0 means not recursive sub file/dir
* recursive = 0 means not recursive, =1 means recursive search all sub dir
*/
int child_cmp(char *path, char *p_path, int recursive)
{
int len = strlen(path);
int p_len = strlen(p_path);
if (len <= p_len)
return 0; // not the child dentry
int i;
if (recursive == 0)
{
for (i = 0; i < p_len; ++i)
{
if (path[i] == p_path[i])
continue;
if (path[i] != p_path[i])
return 0;
}
if (p_len > 1 && path[i] != '/') // just name prefix is the same and not the root
return 0;
i++;
for (; i < len; ++i)
{
if (path[i] == '/')
return 0;
}
return 1;
}
if (recursive == 1)
{
for (i = 0; i < p_len; ++i)
{
if (path[i] == p_path[i])
continue;
if (path[i] != p_path[i])
return 0;
}
if (path[i] != '/')
return 0;
return 1;
}
return -1;
}
/*
* ret = 1 means it is sub file/dir, 0 means it is not sub file/dir, 2 mean they are the same path
*/
int child_cmp_new(char *path, char *p_path, int recursive)
{
int len = strlen(path);
int p_len = strlen(p_path);
if (len < p_len)
return 0; // not the child dentry
int i;
for (i = 0; i < p_len; ++i)
{
if (path[i] == p_path[i])
continue;
if (path[i] != p_path[i])
return 0;
}
if (path[i] != '/' && p_len != len)
return 0;
if (p_len != len)
return 1;
if (p_len == len)
return 2;
}
int init_pcheck_info(char *mount_path)
{
return 0;
}
int set_root_path(struct pacon *pacon, char *r_path)
{
int p_len = strlen(r_path);
if (p_len >= MOUNT_PATH_MAX -1)
{
printf("set root path: root dir too long\n");
return -1;
}
int i;
for (i = 0; i < p_len; ++i)
{
pacon->mount_path[i] = r_path[i];
}
pacon->mount_path[i] == '\0';
printf("set root dir to %s\n", pacon->mount_path);
return 0;
}
/* similar to rmdir, but do not need to remove dir on DFS */
int flush_dir(struct pacon *pacon, char *path)
{
int ret;
add_to_mq(pacon, path, FLUSHDIR, time(NULL));
ret = add_to_local_rpc(pacon, path, FLUSHDIR, time(NULL));
if (ret == -1)
{
printf("flush dir error: %s\n", path);
return -1;
}
return ret;
}
int flush_file(struct pacon *pacon, char *path)
{
int ret;
struct stat buf;
while (stat(path, &buf) != 0)
ret = dmkv_del(pacon->kv_handle, path);
if (ret != 0)
{
printf("flush file error\n");
return -1;
}
return 0;
}
/*
* Policy:
* 1.
*/
int evict_metadata(struct pacon *pacon)
{
int ret;
// get evict lock
char *val;
uint64_t cas;
uint64_t cas_temp;
char *evict_lock = "evict_lock";
retry:
val = dmkv_get_cas(pacon->kv_handle, evict_lock, &cas);
if (val == NULL)
{
printf("get evict lock error: not existed\n");
return -1;
}
if (val[0] == '0')
{
ret = dmkv_cas(pacon->kv_handle, evict_lock, "1", strlen("1"), cas);
if (ret == 1)
goto retry;
} else if (val[0] == '1') {
goto retry;
} else {
printf("evict lock val error\n");
return -1;
}
// find a entry to be evicted
char *record_val;
char *evict_record = "evict_record";
record_val = dmkv_get(pacon->kv_handle, evict_record);
if (record_val == NULL)
{
printf("get evict record val error\n");
return -1;
}
int rr = atoi(record_val);
int c;
DIR *dir;
struct dirent *entry;
find_again:
dir = pacon_opendir(pacon, pacon->mount_path);
entry = pacon_readdir(dir);
c = 0;
int found = 0;
while (entry != NULL)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
{
entry = pacon_readdir(dir);
continue;
}
if (c == rr)
{
char evict_path[PATH_MAX];
sprintf(evict_path, "%s%s%s", pacon->mount_path, "/", entry->d_name);
if (entry->d_type == DT_DIR)
{
ret = flush_dir(pacon, evict_path);
if (ret != 0 )
{
printf("evict metadata: flush dir error\n");
return -1;
}
} else {
ret = flush_file(pacon, evict_path);
if (ret != 0)
{
printf("evict metadata: flush file error\n");
return -1;
}
}
rr++;
found = 1;
break;
}
c++;
entry = pacon_readdir(dir);
}
pacon_closedir(pacon, dir);
if (found == 0)
{
rr = 0;
goto find_again;
}
char new_evict_record[128];
sprintf(new_evict_record, "%d", rr);
ret = dmkv_set(pacon->kv_handle, evict_record, new_evict_record, strlen(new_evict_record));
if (ret != 0)
{
printf("update evict record error\n");
return -1;
}
// free evict lock, doesn't need cas, becasue only the item has already been locked
ret = dmkv_set(pacon->kv_handle, evict_lock, "0", strlen("0"));
if (ret != 0)
{
printf("free evict lock error\n");
return -1;
}
return 0;
}
int test_entry_exist(struct pacon *pacon, char *path)
{
int ret;
char *val;
struct pacon_stat st_tmp;
val = dmkv_get(pacon->kv_handle, path);
if (val != NULL)
deseri_val(&st_tmp, val);
else
return -1;
int existed = get_stat_flag(&st_tmp, STAT_rm);
if (existed == 1)
return 0;
else
return 1;
}
void flush_dir_new(struct pacon *pacon, char *path)
{
int ret;
char dir_new[PATH_MAX];
DIR *pd;
struct dirent *entry;
pd = opendir(path);
while ((entry = readdir(pd)) != NULL)
{
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
int c_len = strlen(entry->d_name);
int p_len = strlen(path);
memcpy(dir_new, path, p_len);
dir_new[p_len] = '/';
memcpy(dir_new + p_len + 1, entry->d_name, c_len);
dir_new[p_len+1+c_len] = '\0';
if (test_entry_exist(pacon, dir_new) == 1)
dmkv_del(pacon->kv_handle, dir_new);
if (entry->d_type == DT_DIR)
{
flush_dir_new(pacon, dir_new);
}
}
closedir(pd);
}
int flush_file_new(struct pacon *pacon, char *path)
{
int ret;
struct stat buf;
if (stat(path, &buf) != 0)
return 0;
if (test_entry_exist(pacon, path) == 1)
{
ret = dmkv_del(pacon->kv_handle, path);
if (ret != 0)
{
printf("flush file error\n");
return -1;
}
}
return 0;
}
/*
* Policy:
* 1. select an entry under the root dir by the DFS interface
* 2. evict the entries under/of it
*/
int evict_metadata_new(struct pacon *pacon)
{
int ret;
// get evict lock
char *val;
uint64_t cas;
uint64_t cas_temp;
char *evict_lock = "evict_lock";
retry:
val = dmkv_get_cas(pacon->kv_handle, evict_lock, &cas);
if (val == NULL)
{
printf("get evict lock error: not existed\n");
return -1;
}
if (val[0] == '0')
{
ret = dmkv_cas(pacon->kv_handle, evict_lock, "1", strlen("1"), cas);
if (ret == 1)
goto retry;
} else if (val[0] == '1') {
goto retry;
} else {
printf("evict lock val error\n");
return -1;
}
// find a entry to be evicted
char *record_val;
char *evict_record = "evict_record";
record_val = dmkv_get(pacon->kv_handle, evict_record);
if (record_val == NULL)
{
printf("get evict record val error\n");
return -1;
}
int rr = atoi(record_val);
int c;
DIR *dir;
struct dirent *entry;
int r1;
find_again:
r1 = 0;
dir = opendir(pacon->mount_path);
entry = readdir(dir);
c = 0;
int found = 0;
while (entry != NULL)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
{
r1++;
if (r1 >= 3)
{
closedir(dir);
goto find_again;
}
entry = readdir(dir);
continue;
}
if (c == rr)
{
char evict_path[PATH_MAX];
sprintf(evict_path, "%s%s%s", pacon->mount_path, "/", entry->d_name);
if (entry->d_type == DT_DIR)
{
flush_dir_new(pacon, evict_path);
} else {
ret = flush_file_new(pacon, evict_path);
if (ret != 0)
{
printf("evict metadata: flush file error\n");
return -1;
}
}
rr++;
found = 1;
break;
}
c++;
entry = readdir(dir);
}
closedir(dir);
if (found == 0)
{
rr = 0;
goto find_again;
}
char new_evict_record[128];
sprintf(new_evict_record, "%d", rr);
ret = dmkv_set(pacon->kv_handle, evict_record, new_evict_record, strlen(new_evict_record));
if (ret != 0)
{
printf("update evict record error\n");
return -1;
}
// free evict lock, doesn't need cas, becasue only the item has already been locked
ret = dmkv_set(pacon->kv_handle, evict_lock, "0", strlen("0"));
if (ret != 0)
{
printf("free evict lock error\n");
return -1;
}
return 0;
}
int evict_metadata_explicit(struct pacon *pacon)
{
int ret;
// get evict lock
char *val;
uint64_t cas;
uint64_t cas_temp;
char *evict_lock = "evict_lock";
retry:
val = dmkv_get_cas(pacon->kv_handle, evict_lock, &cas);
if (val == NULL)
{
printf("get evict lock error: not existed\n");
return -1;
}
if (val[0] == '0')
{
ret = dmkv_cas(pacon->kv_handle, evict_lock, "1", strlen("1"), cas);
if (ret == 1)
goto retry;
} else if (val[0] == '1') {
goto retry;
} else {
printf("evict lock val error\n");
return -1;
}
// find a entry to be evicted
char *record_val;
char *evict_record = "evict_record";
record_val = dmkv_get(pacon->kv_handle, evict_record);
if (record_val == NULL)
{
printf("get evict record val error\n");
return -1;
}
int rr = atoi(record_val);
int c;
DIR *dir;
struct dirent *entry;
dir = pacon_opendir(pacon, pacon->mount_path);
entry = pacon_readdir(dir);
c = 0;
int found = 0;
while (entry != NULL)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
{
entry = pacon_readdir(dir);
continue;
}
if (c == rr)
{
char evict_path[PATH_MAX];
sprintf(evict_path, "%s%s%s", pacon->mount_path, "/", entry->d_name);
if (entry->d_type == DT_DIR)
{
flush_dir_new(pacon, evict_path);
} else {
ret = flush_file_new(pacon, evict_path);
if (ret != 0)
{
printf("evict metadata: flush file error\n");
return -1;
}
}
rr++;
found = 1;
break;
}
c++;
entry = pacon_readdir(dir);
}
pacon_closedir(pacon, dir);
char new_evict_record[128];
sprintf(new_evict_record, "%d", rr);
ret = dmkv_set(pacon->kv_handle, evict_record, new_evict_record, strlen(new_evict_record));
if (ret != 0)
{
printf("update evict record error\n");
return -1;
}
// free evict lock, doesn't need cas, becasue only the item has already been locked
ret = dmkv_set(pacon->kv_handle, evict_lock, "0", strlen("0"));
if (ret != 0)
{
printf("free evict lock error\n");
return -1;
}
return 0;
}
int load_to_pacon(struct pacon *pacon, char *path)
{
int ret;
ret = child_cmp_new(path, pacon->mount_path, 1);
if (ret == 0)
{
printf("path doesn't belong to the workspace\n");
return -1;
}
struct stat buf;
ret = stat(path, &buf);
if (ret != 0)
{
printf("load to pacon: target dir not existed on DFS\n");
return -1;
}
if (SERI_TYPE == 0)
{
struct pacon_stat p_st;
p_st.flags = 0;
if ( S_ISDIR(buf.st_mode) )
{
// it is a dir
set_stat_flag(&p_st, STAT_type, 0);
} else {
// it is a file
set_stat_flag(&p_st, STAT_type, 1);
set_stat_flag(&p_st, STAT_file_created, 1);
}
set_stat_flag(&p_st, STAT_existedin_dfs, 1);
p_st.mode = buf.st_mode;
p_st.ctime = buf.st_ctime;
p_st.atime = buf.st_atime;
p_st.mtime = buf.st_mtime;
p_st.size = buf.st_size;
p_st.uid = buf.st_uid;
p_st.gid = buf.st_gid;
p_st.nlink = buf.st_nlink;
p_st.open_counter = 0;
char val[PSTAT_SIZE];
seri_val(&p_st, val);
evict_ok:
ret = dmkv_add(pacon->kv_handle, path, val, PSTAT_SIZE);
// memory is insufficient
if (ret == -2)
{
evict_metadata(pacon);
goto evict_ok;
}
} else{
cJSON *j_body;
j_body = cJSON_CreateObject();
cJSON_AddNumberToObject(j_body, "flags", 0);
cJSON_AddNumberToObject(j_body, "mode", buf.st_mode);
cJSON_AddNumberToObject(j_body, "ctime", buf.st_ctime);
cJSON_AddNumberToObject(j_body, "atime", buf.st_atime);
cJSON_AddNumberToObject(j_body, "mtime", buf.st_mtime);
cJSON_AddNumberToObject(j_body, "size", buf.st_size);
cJSON_AddNumberToObject(j_body, "uid", buf.st_uid);
cJSON_AddNumberToObject(j_body, "gid", buf.st_gid);
cJSON_AddNumberToObject(j_body, "nlink", buf.st_nlink);
//cJSON_AddNumberToObject(j_body, "opt", 0);
//set_opt_flag(md, OP_mkdir, 1);
char *value = cJSON_Print(j_body);
ret = dmkv_add(pacon->kv_handle, path, value, PSTAT_SIZE);
}
return 0;
}
void get_local_addr(char *ip)
{
FILE *fp;
fp = fopen("./local_config", "r");
if (fp == NULL)
{
printf("cannot open config file\n");
return -1;
}
char temp[24];
fgets(temp, 24, fp);
int i;
for (i = 0; i < 24; ++i)
{
if ((temp[i] >= '0' && temp[i] <= '9') || temp[i] == '.')
{
ip[i] = temp[i];
} else {
break;
}
}
ip[i] = '\0';
}
/*
* 1. inital kv
* 2. build namespace & inital commit queue
*/
int init_pacon(struct pacon *pacon)
{
int ret;
struct dmkv *kv = (struct dmkv *)malloc(sizeof(struct dmkv));
ret = dmkv_init(kv);
if (ret != 0)
{
printf("init dmkv fail\n");
return -1;
}
//kv_handle = kv;
pacon->kv_handle = kv;
// inital permission check info
ret = init_pcheck_info(pacon->mount_path);
if (ret != 0)
{
printf("inital permission check info error\n");
return -1;
}
int i;
for (i = 0; i < MOUNT_PATH_MAX-1; ++i)
{
if (pacon->mount_path != '\0')
{
mount_path[i] = pacon->mount_path[i];
} else {
break;
}
}
mount_path[i] = '\0';
// init mq
void *context = zmq_ctx_new();
void *publisher = zmq_socket(context, ZMQ_PUB);
/* set it unlimit */
int q_len = 0;
int rc = zmq_setsockopt(publisher, ZMQ_SNDHWM, &q_len, sizeof(q_len));
rc = zmq_connect(publisher, "ipc:///run/pacon_commit");
if (rc != 0)
{
printf("init commit mq error\n");
return -1;
}
pacon->publisher = publisher;
pacon->context = context;
// init local rpc
void *context_local_rpc = zmq_ctx_new();
void *local_rpc_req = zmq_socket(context_local_rpc, ZMQ_REQ);
//int q_len = 0;
rc = zmq_setsockopt(local_rpc_req, ZMQ_SNDHWM, &q_len, sizeof(q_len));
rc = zmq_connect(local_rpc_req, "tcp://127.0.0.1:5555");
if (rc != 0)
{
printf("init local rpc error\n");
return -1;
}
pacon->local_rpc_req = local_rpc_req;
pacon->context_local_rpc = context_local_rpc;
// init the root dir of the consistent area
// get the root dir stat from DFS
ret = load_to_pacon(pacon, pacon->mount_path);
if (ret != 0)
{
printf("inital root dir fail\n");
return -1;
}
if (ROOT_EMPTY == 1)
add_to_dir_check_table(pacon->mount_path);
// init fsync log: 1. check the path 2. create the log file
if (access(FSYNC_LOG_PATH, F_OK) != 0)
{
if (mkdir(FSYNC_LOG_PATH, DEFAULT_DIRMORE) != 0)
{
printf("create fsync log dir error\n");
return -1;
}
}
//char fsync_logfile_path[128];
char local_ip[17];
get_local_addr(local_ip);
int pid = (int)getpid();
char pid_s[45];
sprintf(pid_s, "%d", pid);
char filename[64];
strcpy(filename, local_ip);
filename[strlen(local_ip)] = '.';
strcpy(filename + strlen(local_ip) + 1, pid_s);
strcpy(pacon->fsync_logfile_path, FSYNC_LOG_PATH);
strcpy(pacon->fsync_logfile_path + strlen(FSYNC_LOG_PATH), filename);
int fd;
fd = open(pacon->fsync_logfile_path, O_RDWR | O_DIRECT);
if (fd == -1)
{
fd = creat(pacon->fsync_logfile_path, DEFAULT_FILEMODE);
if (fd == -1)
{
printf("create fsync log file error\n");
return -1;
}
}
pacon->fsync_log_fd = fd;
pacon->log_size = 0;
// init permission info
pacon->perm_info = NULL;
pacon->df_dir_mode = S_IFDIR | 0755;
pacon->df_f_mode = S_IFREG | 0644;
// init joint cr info
pacon->cr_num = 0;
// init evict lock, 0 is unlock, 1 is lock
char *evict_lock = "evict_lock";
char *lock_val = "0";
ret = dmkv_add(pacon->kv_handle, evict_lock, lock_val, strlen(evict_lock));
/*if (ret != 0)
{
printf("init evict lock error\n");
return -1;
}*/
char *evict_record = "evict_record";
char *record_val = "?";
ret = dmkv_add(pacon->kv_handle, evict_record, record_val, strlen(record_val));
/*if (ret != 0)
{
printf("init evict record error\n");
return -1;
}*/
// init share memory, for async rpc
if (ASYNC_RPC == 1)
{
int shmid;
void* shm;
shmid = shmget((key_t)SHMKEY, sizeof(struct rmdir_record), 0666 | IPC_CREAT);
if (shmid == -1)
{
printf("shmget error\n");
return -1;
}
shm = shmat(shmid, (void*)0, 0);
if (shm == (void*) -1)
{
printf("shmat error\n");
return -1;
}
pacon->rmdir_record = (struct rmdir_record *)shm;
pacon->rmdir_record->rmdir_num = 0;
pacon->rmdir_record->shmid_count = 1;
}
return 0;
if (SLEEP_AFTER_INIT_SEC > 0)
{
printf("initialing pacon.......\n");
sleep(SLEEP_AFTER_INIT_SEC);
}
}
int free_pacon(struct pacon *pacon)
{
int ret;
dmkv_free(pacon->kv_handle);
zmq_close(pacon->publisher);
zmq_ctx_destroy(pacon->context);
zmq_close(pacon->local_rpc_req);
zmq_ctx_destroy(pacon->context_local_rpc);
if (ASYNC_RPC == 1)
{
ret = shmdt(pacon->rmdir_record);
if (ret == -1)
{
printf("free share memory error\n");
return -1;
}
}
return 0;
}
// 0: not in rmdir list, -1: in rmdir list
int check_rmdir_list(struct pacon *pacon, char *path)
{
int i;
struct rmdir_record *rmdir_record;
for (i = 1; i <= pacon->rmdir_record->shmid_count; ++i)
{
if (i == 1)
{
rmdir_record = pacon->rmdir_record;
} else {
int shmid;
void* shm;
shmid = shmget((key_t)i, sizeof(struct rmdir_record), 0666 | IPC_CREAT);
if (shmid == -1)
{
printf("shmget error\n");
return -1;
}
shm = shmat(shmid, (void*)0, 0);
if (shm == (void*) -1)
{
printf("shmat error\n");