/
dfs.c
1092 lines (916 loc) · 26.9 KB
/
dfs.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
//
// Pete's RAM filesystem. No persistance, no links, slow linear searches.
// Basically, this was an afternoon's hack, so be nice.
//
// Blech. Wrote this first on a mac, easy. Getting to compile on linux much
// harder. Right now, mac is the #else of the linux ifdefs. To get to compile
// on linux, download all fuse source, cp hellofs.c into the examples subdir,
// modify the makefile to do everything for hellofs that it does for hello,
// for example, and then it works. Might also want to remove -Wmissing-declarations.
//
// In both cases, run by "./dfs /tmp/hello" (after you create the dir),
// kill by 'killall dfs'.
//
#define FUSE_USE_VERSION 26
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <search.h>
#include <fuse.h>
#include "utils.h"
#include "dfs.h"
#include "comm.h"
#include "log.h"
#include "chits.h"
//=============================================================================
// On Mac:
// (-lfuse_ino64 needed on snow leopard)
// gcc -O0 -std=c99 -o dfs dfs.c -lfuse_ino64
// mkdir /tmp/dfs
// ./dfs /tmp/dfs -oping_diskarb,volname=Dfs
// ./dfs /tmp/dfs -odebug
//
// On Linux:
// mkdir /tmp/dfs
// ./dfs /tmp/dfs -odebug
// gcc -O0 -std=c99 -D_FILE_OFFSET_BITS=64 -o dfs dfs.c -lfuse
//=============================================================================
static int extentSock;
Log opLog;
static void *pathRoot;
DfsFile *root;
static int debug = 1;
extern pthread_mutex_t replyLogserverMut;
extern pthread_cond_t replyLogserverCond;
pthread_mutex_t treeMut;
extern Msg *replyQueue;
int pending_change_id = -1;
// Needed by init
char *sname = "localhost";
char *xname = "localhost";
int sport = LOG_PORT;
int xport = EXTENT_PORT;
char *narrowing = NULL;
int remove_write = 0;
int remove_delete = 0;
int remove_create = 0;
extern int encryption;
extern char session_key[16];
// Auth vars
char *chit = 0;
chit_t *chit_struct = NULL;
//=============================================================================
static void *listener(void *arg)
{
int fd = *(int *)arg;
dfs_out("\n\tLISTEN PROC IN, sock %d!\n\n", fd);
Msg *m;
while (m = comm_read(1, fd)) {
switch (m->type) {
case DFS_MSG_PUSH_LOG:
/* Lock tree and replay log. Assumes we get the whole log. */
pthread_mutex_lock(&treeMut);
pthread_mutex_lock(&replyLogserverMut);
dfs_out("Push calling play log\n");
char *log;
size_t log_sz;
assert(tuple_unserialize_log(&log, &log_sz, m->data, m->len) == 0);
playLog(log, log_sz);
free(log);
pthread_mutex_unlock(&replyLogserverMut);
pthread_mutex_unlock(&treeMut);
break;
case MSG_REPLY:
/* Put on reply queue. Waits until main thread gets the item
and sets the replyQueue to NULL to prevent losing messages */
pthread_mutex_lock(&replyLogserverMut);
assert(replyQueue == NULL);
replyQueue = m;
pthread_cond_signal(&replyLogserverCond);
pthread_cond_wait(&replyLogserverCond, &replyLogserverMut);
pthread_mutex_unlock(&replyLogserverMut);
break;
default:
dfs_die("BAD MSG TYPE %d\n", m->type);
}
}
dfs_out("\n\tLISTEN PROC EXIT, sock %d!\n\n", fd);
return NULL;
}
//=============================================================================
static int file_compare(const void *node1, const void *node2) {
return strcmp(((const DfsFile *) node1)->path,
((const DfsFile *) node2)->path);
}
//=============================================================================
char *narrow_path(char *path) {
char *out_path = malloc(strlen(path) + strlen(narrowing) + 1);
printf("narrow in[%s]\n", path);
strcpy(out_path, narrowing);
strcat(out_path, path);
if (strlen(out_path) > 1 && out_path[strlen(out_path) - 1] == '/') {
printf("Removing ending\n");
out_path[strlen(out_path) - 1] = '\0';
}
printf("narrow out[%s]\n", out_path);
return out_path;
}
DfsFile *mkNode(const char *path, const char *name, DfsFile *parent, mode_t mode) {
DfsFile *f = (DfsFile *)calloc(1, sizeof(DfsFile));
dfs_out("mknode '%s'\n", path);
f->stat.st_mtime = f->stat.st_atime = f->stat.st_ctime = time(NULL);
f->stat.st_mode = mode;
f->stat.st_nlink = 1;
f->stat.st_uid = getuid();
f->stat.st_gid = getgid();
f->name = strdup(name);
strncpy(f->path, path, sizeof(f->path)-1);
f->parent = parent;
if (parent) {
parent->child = (DfsFile **)realloc(parent->child, (parent->num_children + 1) * sizeof(DfsFile *));
parent->child[parent->num_children++] = f;
}
tsearch(f, &pathRoot, file_compare);
return f;
}
static void freeNode(const char *path, DfsFile *f) {
DfsFile *p = f->parent;
dfs_out("mknode '%s'\n", path);
tdelete(path, &pathRoot, file_compare);
assert(!f->num_children);
free(f->data);
free(f->name);
if (p) {
int i;
for (i = 0; i < p->num_children; i++) {
if (p->child[i] == f) {
assert(p->num_children);
dfs_out("memcpy %x, %x, %d (i %d #%d)\n", (void *)&p->child[i], (void *)&p->child[i+1],
sizeof(DfsFile *) * (p->num_children - (i+1)), i, p->num_children);
memmove((void *)&p->child[i], (void *)&p->child[i+1], sizeof(DfsFile *) * (p->num_children - (i+1)));
break;
}
}
assert(i < p->num_children);
p->num_children--;
}
free(f);
}
void destroy_node(void *node) {
DfsFile *f = ((DfsFile *) node);
if (!f)
return;
dfs_out("Destroy Data\n");
free(f->data);
dfs_out("Destroy Name\n");
free(f->name);
dfs_out("Destroy Recipe\n");
free(f->recipe);
}
void destroy_tree() {
tdestroy(pathRoot, destroy_node);
pathRoot = NULL;
}
//=============================================================================
// Return dynamically allocated extent, must be free'd
Extent *get_extent(char *sig)
{
Extent *ex;
assert(sig);
char *serialized;
size_t serialized_sz;
assert(tuple_serialize_sig(&serialized, &serialized_sz, sig) == 0);
Msg *reply = comm_send_and_reply(0, extentSock, DFS_MSG_GET_EXTENT, serialized, serialized_sz, NULL);
free(serialized);
if (!reply || (reply->res != REPLY_OK))
return NULL;
assert(reply->len);
char *unserialized;
size_t unserialized_sz;
assert(tuple_unserialize_extent(&unserialized, &unserialized_sz, reply->data, reply->len) == 0);
ex = malloc(sizeof(Extent) + unserialized_sz);
assert(ex);
// verify sig
char *s = cry_hash_bytes(unserialized, unserialized_sz);
strcpy(ex->sig, s);
free(s);
memcpy(ex->data, unserialized, unserialized_sz);
ex->sz = unserialized_sz;
free(reply);
free(unserialized);
return ex;
}
// Like 'get_extent', but just return boolean 1 for found, or 0 for not.
int poll_extent(char *sig)
{
assert(sig);
char *serialized;
size_t serialized_sz;
assert(tuple_serialize_sig(&serialized, &serialized_sz, sig) == 0);
Msg *reply = comm_send_and_reply(0, extentSock, DFS_MSG_POLL_EXTENT, serialized, serialized_sz, NULL);
int ret = (reply && (reply->res == REPLY_OK));
free(reply);
free(serialized);
return ret;
}
// Puts sig into sigbuf. '0' on success.
char *put_extent(char *buf, long sz)
{
int i;
assert(buf && sz && (sz < (1024L * 1024L * 1024L)));
char *s = cry_hash_bytes(buf, sz);
if (poll_extent(s)) {
printf("Server already has EXTENT\n");
return s;
}
char *serialized;
size_t serialized_sz;
assert(tuple_serialize_sig_extent(s, &serialized, &serialized_sz, buf, sz) == 0);
Msg *reply = comm_send_and_reply(0, extentSock, DFS_MSG_PUT_EXTENT, serialized, serialized_sz, NULL);
free(serialized);
if (!reply) dfs_die("No get reply\n");
dfs_out("extent '%s' CREATED\n", s);
free(reply);
return s;
}
//=============================================================================
DfsFile *findFile(char *path)
{
if (!path[0] || !strcmp(path, "/"))
return root;
DfsFile **fh = tfind(path, &pathRoot, file_compare);
return fh ? *fh : NULL;
}
static int dfs_getattr(const char *path, struct stat *stbuf)
{
path = narrow_path(path);
DfsFile *f = findFile((char *)path);
dfs_out("GETATTR: '%s' (%ld)\n", path, f);
if (!f) return -ENOENT;
*stbuf = f->stat;
return 0;
}
static int dfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
DfsFile *f = findFile((char *)path);
int i;
dfs_out("READDIR: '%s'\n", path);
if (!f) {
pthread_mutex_unlock(&treeMut);
return -ENOENT;
}
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
for (i = 0; i < f->num_children; i++) {
filler(buf, f->child[i]->name, NULL, 0);
}
pthread_mutex_unlock(&treeMut);
return 0;
}
static int _dfs_open(const char *path, struct fuse_file_info *fi)
{
DfsFile *f;
dfs_out("\n\tFUSE OPEN '%s'\n\n", path);
if (!(f = findFile((char *)path))) {
return -ENOENT;
}
long flags = fi ? fi->flags : 0;
dfs_out("\tOPEN : '%s', flags %o, len %d, reclen %d, recipe %x, data %x\n", path, flags, f->len, f->recipelen, f->recipe, f->data);
if (f->stat.st_mode & S_IFDIR) {
return -EISDIR;
}
if (0 && !(fi->flags & f->stat.st_mode)) {
dfs_out("OPEN permissions problem: %o, %o\n", fi->flags, f->stat.st_mode);
return -EACCES;
}
// Why? don't remember....
fi->fh = (uint64_t)f->stat.st_mode;
if (f->len && !f->data) {
assert(f->recipe);
f->data = malloc(f->len);
assert(f->data);
char *data = f->data, *dataEnd = f->data + f->len;
char *sig = f->recipe, *sigEnd = f->recipe + f->recipelen;
while ((data < dataEnd) && (sig < sigEnd)) {
Extent *ex = get_extent(sig);
if (!ex) dfs_die("No get signature '%s'\n", sig);
memcpy(data, ex->data, ex->sz);
data += ex->sz;
sig += strlen(sig) + 1;
free(ex);
}
assert((data == dataEnd) && (sig == sigEnd));
}
return 0;
}
static int dfs_open(const char *path, struct fuse_file_info *fi) {
int out;
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
out = _dfs_open(path, fi);
pthread_mutex_unlock(&treeMut);
return out;
}
static int dfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
dfs_out("READ: '%s', sz %d, offset %d\n", path, size, offset);
DfsFile *f = findFile((char *)path);
if (!f) {
pthread_mutex_unlock(&treeMut);
return -ENOENT;
}
if (size && !f->len) {
pthread_mutex_unlock(&treeMut);
return 0;
}
if (f->stat.st_mode & S_IFDIR) {
pthread_mutex_unlock(&treeMut);
return -EISDIR;
}
len = f->len;
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, f->data + offset, size);
} else
size = 0;
f->stat.st_atime = time(NULL);
pthread_mutex_unlock(&treeMut);
return size;
}
static int dfs_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
dfs_out("WRITE: '%s', sz %d, offset %d\n", path, size, offset);
if (remove_write) {
pthread_mutex_unlock(&treeMut);
return -EACCES;
}
DfsFile *f = findFile((char *)path);
if (!f) {
pthread_mutex_unlock(&treeMut);
return -ENOENT;
}
if (f->stat.st_mode & S_IFDIR) {
pthread_mutex_unlock(&treeMut);
return -EISDIR;
}
// Handles issue where a collision can cause us to fail this check
if(!(!f->recipelen || f->data)) {
struct fuse_file_info fii;
fii.flags = 0;
_dfs_open(path, &fii);
}
//assert(!f->recipelen || f->data);
if ((size + offset) > f->len) {
f->data = (char *)realloc(f->data, size + offset);
f->stat.st_size = f->len = size + offset;
}
memcpy(f->data + offset, buf, size);
f->dirty = 1;
f->stat.st_mtime = f->stat.st_atime = time(NULL);
pthread_mutex_unlock(&treeMut);
return size;
}
int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
if (remove_create) {
pthread_mutex_unlock(&treeMut);
return -EACCES;
}
dfs_out("CREATE: '%s'\n", path);
DfsFile *f = findFile((char *)path);
DfsFile *dir;
char *dname, *fname;
if (f) {
pthread_mutex_unlock(&treeMut);
return -EEXIST;
}
if (!(fname = strrchr(path, '/'))) {
pthread_mutex_unlock(&treeMut);
return -EINVAL;
}
dname = strdup(path);
dname[fname - path] = 0;
fname++;
if (!(dir = findFile(dname))) {
free(dname);
pthread_mutex_unlock(&treeMut);
return -EINVAL;
}
f = mkNode(path, fname, dir, DEF_FILE_MODE);
dfs_out("CREATE OUT, now %d children in '%s'\n", dir->num_children, dname);
free(dname);
f->version++;
logFileVersion(f);
pthread_mutex_unlock(&treeMut);
return 0;
}
int _dfs_chmod(const char *path, mode_t mode)
{
/* Assumes treeMut is locked */
DfsFile *f;
dfs_out("CHMOD: '%s' %o\n", path, mode);
if (!(f = findFile((char *)path)))
return -ENOENT;
f->stat.st_mode = (f->stat.st_mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) | mode;
dfs_out("\tend mode: %o\n", f->stat.st_mode);
return 0;
}
int dfs_chmod(const char *path, mode_t mode) {
int out;
DfsFile *f;
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
out = _dfs_chmod(path, mode);
if (out) {
pthread_mutex_unlock(&treeMut);
return out;
}
f = findFile((char *)path);
logOther(LOG_CHMOD, path, mode, &f->stat);
pthread_mutex_unlock(&treeMut);
return out;
}
int _dfs_mkdir(const char *path, mode_t mode)
{
/* Assumes treeMut is locked */
//S_IFDIR
DfsFile *dir;
char *dname = strdup(path), *fname;
dfs_out("MKDIR: '%s', %o\n", path, mode);
if (dname[strlen(dname) - 1] == '/')
dname[strlen(dname) - 1] = 0;
if (!dname[0]) {
free(dname);
return -EEXIST;
}
if (dir = findFile((char *)dname)) {
free(dname);
return -EEXIST;
}
fname = strrchr(dname, '/');
dfs_out("STRRCHR '%s', '%s'\n", dname, fname ? fname : "''");
if (!fname) {
free(dname);
return -EINVAL;
}
*fname++ = 0;
dfs_out("dname %x ('%s'), fname %x ('%s')\n", dname, dname, fname, fname);
if (!(dir = findFile((char *)dname))) {
free(dname);
return -EINVAL;
}
dfs_out("MKDIR2: '%s', '%s'\n", dname, fname);
mkNode(path, fname, dir, S_IFDIR | mode);
free(dname);
return 0;
}
int dfs_mkdir(const char *path, mode_t mode) {
int out;
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
if (remove_create) {
pthread_mutex_unlock(&treeMut);
return -EACCES;
}
out = _dfs_mkdir(path, mode);
if (out) {
pthread_mutex_unlock(&treeMut);
return out;
}
logOther(LOG_MKDIR, path, mode, NULL);
pthread_mutex_unlock(&treeMut);
return out;
}
int _dfs_rmdir(const char *path)
{
/* Assumes treeMut is locked */
DfsFile *f;
if (!(f = findFile((char *)path)))
return -ENOENT;
if (!(f->stat.st_mode & S_IFDIR))
return -ENOTDIR;
if (f->num_children)
return -ENOTEMPTY;
freeNode(path, f);
return 0;
}
int dfs_rmdir(const char *path) {
int out;
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
if (remove_delete) {
pthread_mutex_unlock(&treeMut);
return -EACCES;
}
out = _dfs_rmdir(path);
if (out) {
pthread_mutex_unlock(&treeMut);
return out;
}
logOther(LOG_RMDIR, path, 0, NULL);
pthread_mutex_unlock(&treeMut);
return out;
}
int _dfs_unlink(const char *path)
{
/* Assumes treeMut is locked */
path = narrow_path(path);
DfsFile *f;
dfs_out("Unlink '%s'\n", path);
if (!(f = findFile((char *)path)))
return -ENOENT;
if (f->stat.st_mode & S_IFDIR)
return -EISDIR;
if (--f->stat.st_nlink)
return 0;
freeNode(path, f);
return 0;
}
int dfs_unlink(const char *path) {
int out;
pthread_mutex_lock(&treeMut);
out = _dfs_unlink(path);
if (remove_delete) {
pthread_mutex_unlock(&treeMut);
return -EACCES;
}
if (out) {
pthread_mutex_unlock(&treeMut);
return out;
}
logOther(LOG_UNLINK, path, 0, NULL);
pthread_mutex_unlock(&treeMut);
return out;
}
// If file dirty, chunkify, sent extents to extentserver, and send
// recipe to recipes server. In all cases, toss local file from hash table.
static int dfs_flush(const char *path, struct fuse_file_info *fi)
{
DfsFile *f;
pthread_mutex_lock(&treeMut);
path = narrow_path(path);
dfs_out("DFS_FLUSH '%s\n", path);
if (!(f = findFile((char *)path))) {
pthread_mutex_unlock(&treeMut);
return -EINVAL;
}
assert(f);
dfs_out("\tflush '%s': len %d, dirty %d, data %x\n", f->path, f->len, f->dirty, f->data);
// If no data, nothing to flush...
if (!f->data || !f->dirty) {
free(f->data);
f->data = NULL;
pthread_mutex_unlock(&treeMut);
return 0;
}
// Need to create extents. For now, just chop up into 4k blocks.
char *buf = f->data, *bufend = buf + f->len;
char *sig;
Extent *ex, *last;
int blocks = f->len / BLOCK_SIZE;
if (f->len % BLOCK_SIZE) blocks++;
f->recipelen = blocks * A_HASH_SIZE;
free(f->recipe);
f->recipe = malloc(f->recipelen);
char *curr = f->recipe;
while (buf < bufend) {
int sz = MIN(bufend - buf, BLOCK_SIZE);
char *hash;
hash = put_extent(buf, sz);
assert(hash);
dfs_out("PUT extent size %d, '%s'\n", sz, hash);
strcpy(curr, hash);
curr += A_HASH_SIZE;
free(hash);
buf += sz;
}
assert(curr == (f->recipe + f->recipelen));
free(f->data);
f->data = NULL;
f->version++;
f->dirty = 0;
logFileVersion(f);
pthread_mutex_unlock(&treeMut);
return 0;
}
static int dfs_truncate(const char *path, off_t sz)
{
DfsFile *f;
pthread_mutex_lock(&treeMut);
dfs_out("\n\tFUSE TRUNCATE\n\n");
path = narrow_path(path);
if (remove_write) {
pthread_mutex_unlock(&treeMut);
return -EACCES;
}
if (!(f = findFile((char *)path))) {
pthread_mutex_unlock(&treeMut);
return -ENOENT;
}
dfs_out("TRUNCATE to %d, was %d\n", sz, f->len);
if (sz < f->len) {
f->len = f->stat.st_size = sz;
f->dirty = 1;
}
pthread_mutex_unlock(&treeMut);
return 0;
}
static void * dfs_init(struct fuse_conn_info *conn)
{
pthread_mutex_lock(&treeMut);
dfs_out("INIT!\n");
root = mkNode("", "", NULL, DEF_DIR_MODE);
comm_register_msgtypes(sizeof(messages) / sizeof(messages[0]), messages);
if (!(extentSock = comm_client_socket(xname, xport)))
dfs_die("NO setup client socket to extent server at %d on '%s'\n", xport, xname);
if (sname && ((opLog.net_fd = comm_client_socket(sname, sport)) > 0)) {
// Here is where we auth
assert(chit);
// Send the server an OHAI
Msg *reply = comm_send_and_reply(0, opLog.net_fd, DFS_OHAI_SERVER, NULL);
char *reply_data = malloc(reply->len + 1);
memcpy(reply_data, reply->data, reply->len);
reply_data[reply->len] = '\0';
char server_nonce = reply_data[0];
char *server_pk = reply_data + 1;
printf("Server gave us a nonce [%d] and a pk[%s]. He liked our picture!\n", (int) server_nonce, server_pk);
// TODO Verify PK
// Make an AES session key
cry_create_nonce(16, session_key);
printf("Sym Key[%x%x%x%x]\n", *(session_key), *(session_key + 4), *(session_key + 8), *(session_key + 12));
// Encrypt session key with PK
char *session_key_encrypted;
int session_key_encrypted_sz;
cry_asym_encrypt(&session_key_encrypted, &session_key_encrypted_sz, session_key, 16, server_pk);
cry_sym_init(session_key);
int out_chit_len = strlen(chit) + 3;
char *out_chit = malloc(out_chit_len);
char client_nonce;
cry_create_nonce(1, &client_nonce);
printf("client nonce[%d]\n", (int)client_nonce);
out_chit[0] = client_nonce;
out_chit[1] = server_nonce;
strcpy(out_chit + 2, chit);
out_chit[out_chit_len - 1] = '\0';
char *out_chit_encrypted;
int out_chit_encrypted_sz;
cry_sym_encrypt(&out_chit_encrypted, &out_chit_encrypted_sz, out_chit, out_chit_len);
reply = comm_send_and_reply(0, opLog.net_fd, DFS_TAKE_CHIT_SERVER, session_key_encrypted,
session_key_encrypted_sz, out_chit_encrypted, out_chit_encrypted_sz, NULL);
char *out_nonce;
int out_nonce_sz;
cry_sym_init(session_key);
cry_sym_decrypt(&out_nonce, &out_nonce_sz, reply->data, reply->len);
printf("Nonce check[%d][%d]\n", (int)client_nonce, (int)(*out_nonce));
assert(*out_nonce == (char)(client_nonce + 1));
free(reply);
// create a new thread reading this socket
pthread_t tid;
pthread_create(&tid, NULL, listener, &opLog.net_fd);
// grab current FS from server
reply = comm_send_and_reply_mutex(1, &replyLogserverMut, &replyLogserverCond, opLog.net_fd, DFS_MSG_GET_LOG, NULL);
char *log;
size_t log_sz;
assert(tuple_unserialize_log(&log, &log_sz, reply->data, reply->len) == 0);
if (reply) {
if (reply->len) {
playLog(log, log_sz);
opLog.served = log_sz;
}
free(reply);
}
free(log);
} else {
dfs_die("NO setup client socket to log server at %d on '%s'\n", sport, sname);
}
pthread_mutex_unlock(&treeMut);
}
static struct fuse_operations dfs_oper = {
.getattr = dfs_getattr,
.readdir = dfs_readdir,
.create = dfs_create,
.open = dfs_open,
.read = dfs_read,
.write = dfs_write,
.unlink = dfs_unlink,
.chmod = dfs_chmod,
.mkdir = dfs_mkdir,
.rmdir = dfs_rmdir,
.flush = dfs_flush,
.truncate = dfs_truncate,
.init = dfs_init
};
int main(int argc, char *argv[])
{
int i, arg = 0, c;
while ((c = getopt(argc, argv, "ei:o:s:S:x:X:c:")) != -1) {
switch (c) {
case 's':
sname = optarg;
arg += 2;
break;
case 'S':
sport = atoi(optarg);
arg += 2;
break;
case 'e':
encryption = 1;
arg++;
break;
case 'x':
xname = optarg;
arg += 2;
break;
case 'c':
{
FILE *chit_fp;
chit_fp = fopen(optarg, "r");
assert(chit_fp);
fseek(chit_fp, 0, SEEK_END);
int chit_sz = ftell(chit_fp);
fseek(chit_fp, 0, SEEK_SET);
chit = malloc(chit_sz + 1);
fread(chit, chit_sz, 1, chit_fp);
chit[chit_sz] = '\0';
chit_struct = xcred_parse(strdup(chit));
attr_t *attr = chit_struct->attrs;
narrowing = malloc(1);
*narrowing = '\0';
while (attr) {
if (attr->tag == TAG_NARROW) {
// TODO: We may want to verify that the narrowings narrow
free(narrowing);
narrowing = strdup(attr->val_s);
} else if (attr->tag == TAG_REMOVE_RIGHT) {
printf("Remove[%d]\n", attr->val_l);
if (attr->val_l == RIGHT_CREATE) {
remove_create = 1;
} else if (attr->val_l == RIGHT_WRITE) {
remove_write = 1;
} else if (attr->val_l == RIGHT_DELETE) {
remove_delete = 1;
}
}
attr = attr->next;
}
printf("Read chit[%s]\n", chit);
printf("Narrowing[%s]\n", narrowing);
arg += 2;
}
break;
case 'X':
xport = atoi(optarg);
arg += 2;
break;
default:
fprintf(stderr, "USAGE: \n"
"\t[-s <logserver host>]\n"
"\t[-S <logserver port>]\n"
"\t[-x <extent server host>]\n"
"\t[-X <extent server port>]\n", argv[0]);
exit(1);
}
}
arg++;
int counter = 0;
for (i = arg+1; i < argc; ++i) {
argv[++counter] = argv[i];
}
return fuse_main(argc - arg, argv, &dfs_oper, NULL);
}
// called to reply records from logs returned from server
void playLog(char *buf, int len)
{
/* Assumes treeMut and replyLogserverMut are locked */
/* Find first ID of the new entry, go through whole log
and truncate if that ID or greater is found, then append
the new log. It is assumed that the server has successfully
managed any conflicts and any of that is taken care of. */
// If we aren't forcing, check if this entry is in the log
char *data = opLog.data;
char *end = opLog.data + opLog.used;
int first_version = ((LogHdr *)buf)->id;
/* If we already have this version then quit.
We only check the first record as if we asked
for the log we have an empty log. If we get
pushed then we recieve one log entry and we just
need to check if we have already committed it.
*/
if (pending_change_id == first_version) {
dfs_out("PlayLog: Server sent us what we are waiting to commit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
return;
}