-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmulti_loopback.c
More file actions
1482 lines (1155 loc) · 38.8 KB
/
Copy pathmulti_loopback.c
File metadata and controls
1482 lines (1155 loc) · 38.8 KB
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
/*
SafeFS
(c) 2016 2016 INESC TEC. Written by J. Paulo and R. Pontes
*/
#include <assert.h>
#include "multi_loopback.h"
#include "utils.h"
#include "timestamps/timestamps.h"
int NDEVS;
// struct with encode algorithms
char **devices_path;
GThreadPool *thread_pool;
#define MAX_THREADS 10
static struct multi_driver m_driver;
int DRIVER;
GSList *multi_write_list = NULL, *multi_read_list = NULL;
void threads_func(gpointer data, gpointer user_data) {
struct op_info *inf = (struct op_info *)data;
switch (inf->op_type) {
case READ_OP:
if (DRIVER == ERASURE) {
DEBUG_MSG("1-Reading CONTENT off %lld and size %lld\n", inf->magicblockoffset, inf->magicblocksize);
inf->op_res = pread(inf->fd, inf->buf, inf->magicblocksize, inf->magicblockoffset);
DEBUG_MSG("Content actually read was %ld\n", inf->op_res);
if (inf->op_res > 0) {
inf->op_res = inf->size;
}
} else {
DEBUG_MSG("2-Reading CONTENT off %lld and size %lld\n", inf->offset, inf->size);
inf->op_res = pread(inf->fd, inf->buf, inf->size, inf->offset);
}
break;
case WRITE_OP:
if (DRIVER == ERASURE) {
DEBUG_MSG("1-Writing CONTENT off %lld and size %lld\n", inf->magicblockoffset, inf->magicblocksize);
pwrite(inf->fd, inf->buf, inf->magicblocksize, inf->magicblockoffset);
inf->op_res = inf->size;
} else {
DEBUG_MSG("2-Writing CONTENT off %lld and size %lld\n", inf->offset, inf->size);
int res = pwrite(inf->fd, inf->buf, inf->size, inf->offset);
DEBUG_MSG("result is %d\n", res);
inf->op_res = res;
}
break;
case FLUSH_OP:
inf->op_res = close(dup(inf->fd));
break;
case RELEASE_OP:
inf->op_res = close(inf->fd);
break;
case FSYNC_OP:
inf->op_res = fsync(inf->fd);
break;
case TRUNCATE_OP:
inf->op_res = truncate(inf->path, inf->size);
break;
case FTRUNCATE_OP:
inf->op_res = ftruncate(inf->fd, inf->size);
break;
case MKNOD_OP:
if (S_ISFIFO(inf->mode)) {
inf->op_res = mkfifo(inf->path, inf->mode);
} else {
inf->op_res = mknod(inf->path, inf->mode, inf->rdev);
}
break;
case MKDIR_OP:
inf->op_res = mkdir(inf->path, inf->mode);
break;
case UNLINK_OP:
inf->op_res = unlink(inf->path);
break;
case RMDIR_OP:
inf->op_res = rmdir(inf->path);
break;
case CREATE_OP:
inf->op_res = open(inf->path, inf->flags, inf->mode);
break;
case OPEN_OP:
inf->op_res = open(inf->path, inf->flags);
break;
case OPENDIR_OP:
inf->d->dp = opendir(inf->path);
if (inf->d->dp == NULL) {
inf->op_res = -errno;
} else {
inf->op_res = 0;
inf->d->offset = 0;
inf->d->entry = NULL;
}
break;
case RELEASEDIR_OP:
inf->op_res = closedir(inf->d->dp);
break;
case SYMLINK_OP:
inf->op_res = symlink(inf->frompath, inf->topath);
break;
case RENAME_OP:
inf->op_res = rename(inf->frompath, inf->topath);
break;
case LINK_OP:
inf->op_res = link(inf->frompath, inf->topath);
break;
case CHMOD_OP:
inf->op_res = chmod(inf->path, inf->mode);
break;
case CHOWN_OP:
inf->op_res = lchown(inf->path, inf->uid, inf->gid);
break;
default:
ERROR_MSG("OP_TYPE Unknown\n");
break;
}
if (inf->op_res == -1) {
inf->op_error = -errno;
}
pthread_mutex_lock(inf->lock);
*inf->ops_done = *inf->ops_done + 1;
pthread_cond_signal(inf->cond);
pthread_mutex_unlock(inf->lock);
}
int wait_for_all_requests(struct op_info *inf, pthread_mutex_t *op_lock, pthread_cond_t *wait_ops) {
DEBUG_MSG("waitrequests\n");
int res;
int i = 0;
pthread_mutex_lock(op_lock);
while (*inf[0].ops_done < NDEVS) {
pthread_cond_wait(wait_ops, op_lock);
}
pthread_mutex_unlock(op_lock);
for (i = 0; i < NDEVS; i++) {
if (inf[i].op_res == -1) {
inf[0].op_error = inf[i].op_error;
return -1;
}
if (i > 0 && (inf[i].op_type == READ_OP || inf[i].op_type == WRITE_OP)) {
if (res != inf[i].op_res) {
inf[0].op_error = inf[i].op_error;
return -1;
}
}
if (i == 0) {
res = inf[i].op_res;
}
}
return res;
}
static int loopback_getattr(const char *path, struct stat *stbuf) {
int res;
DEBUG_MSG("getattr\n");
// Check only the status from one pen since this is replicated in all
char newpath[PATHSIZE];
strcpy(newpath, devices_path[0]);
replace_path(path, newpath);
res = lstat(newpath, stbuf);
if (res == -1) {
return -errno;
}
if (DRIVER == ERASURE) {
uint64_t size = m_driver.get_file_size(path);
DEBUG_MSG("Size found is %lld\n", size);
stbuf->st_size = size;
}
return 0;
}
static int loopback_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) {
int res;
DEBUG_MSG("fgetattr\n");
struct mpath_aux *mp = (struct mpath_aux *)(uintptr_t)fi->fh;
// Check only the status from one pen since this is replicated in all
res = fstat(mp->devs_fd[0], stbuf);
if (res == -1) {
return -errno;
}
if (DRIVER == ERASURE) {
uint64_t size = m_driver.get_file_size(path);
DEBUG_MSG("Size found is %lld\n", size);
stbuf->st_size = size;
}
return 0;
}
static int loopback_readlink(const char *path, char *buf, size_t size) {
int res;
DEBUG_MSG("readlink\n");
char newpath[PATHSIZE];
int psize;
char auxbuf[PATHSIZE];
strcpy(newpath, devices_path[0]);
replace_path(path, newpath);
res = readlink(newpath, auxbuf, size - 1);
if (res == -1) {
return -errno;
}
psize = strlen(devices_path[0]);
strcpy(buf, &auxbuf[psize]);
buf[res - psize] = '\0';
return 0;
}
static int loopback_opendir(const char *path, struct fuse_file_info *fi) {
int res;
DEBUG_MSG("opendir\n");
struct mpath_aux *mp = malloc(sizeof(struct mpath_aux));
mp->ldp = malloc(sizeof(struct loopback_dirp) * NDEVS);
int i;
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newpath[NDEVS];
// create folder in each device
for (i = 0; i < NDEVS; i++) {
devices_newpath[i] = malloc(PATHSIZE);
strcpy(devices_newpath[i], devices_path[i]);
replace_path(path, devices_newpath[i]);
inf[i].path = devices_newpath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = OPENDIR_OP;
inf[i].d = &(mp->ldp[i]);
if (inf[i].d == NULL) {
free(mp->ldp);
free(mp);
return -ENOMEM;
}
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newpath[i]);
}
if (res == -1) {
free(mp->ldp);
free(mp);
return inf[0].op_error;
}
fi->fh = (unsigned long)mp;
return 0;
}
static int loopback_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
struct fuse_file_info *fi) {
DEBUG_MSG("readdir\n");
struct mpath_aux *mp = (struct mpath_aux *)(uintptr_t)fi->fh;
struct loopback_dirp *d = &(mp->ldp[0]);
if (offset != d->offset) {
seekdir(d->dp, offset);
d->entry = NULL;
d->offset = offset;
}
while (1) {
struct stat st;
off_t nextoff;
if (!d->entry) {
d->entry = readdir(d->dp);
if (!d->entry) {
break;
}
}
memset(&st, 0, sizeof(st));
st.st_ino = d->entry->d_ino;
st.st_mode = d->entry->d_type << 12;
nextoff = telldir(d->dp);
if (filler(buf, d->entry->d_name, &st, nextoff)) {
break;
}
d->entry = NULL;
d->offset = nextoff;
}
return 0;
}
static int loopback_releasedir(const char *path, struct fuse_file_info *fi) {
DEBUG_MSG("releasedir\n");
int res;
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
struct mpath_aux *mp = (struct mpath_aux *)(uintptr_t)fi->fh;
int i;
// create folder in each device
for (i = 0; i < NDEVS; i++) {
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = RELEASEDIR_OP;
inf[i].d = &(mp->ldp[i]);
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
free(mp->ldp);
free(mp);
if (res == -1) {
DEBUG_MSG("releasedirerror path %s %d\n", path, inf[0].op_error);
return inf[0].op_error;
}
return 0;
}
static int loopback_mknod(const char *path, mode_t mode, dev_t rdev) {
int res = 0;
DEBUG_MSG("mknod\n");
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newpath[NDEVS];
// mknod in each device
int i;
for (i = 0; i < NDEVS; i++) {
devices_newpath[i] = malloc(PATHSIZE);
strcpy(devices_newpath[i], devices_path[i]);
replace_path(path, devices_newpath[i]);
inf[i].path = devices_newpath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = MKNOD_OP;
inf[i].mode = mode;
inf[i].rdev = rdev;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newpath[i]);
}
if (res == -1) {
return inf[0].op_error;
}
return 0;
}
static int loopback_mkdir(const char *path, mode_t mode) {
int res;
DEBUG_MSG("mkdir\n");
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newpath[NDEVS];
// create folder in each device
int i;
for (i = 0; i < NDEVS; i++) {
devices_newpath[i] = malloc(PATHSIZE);
strcpy(devices_newpath[i], devices_path[i]);
replace_path(path, devices_newpath[i]);
inf[i].path = devices_newpath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = MKDIR_OP;
inf[i].mode = mode;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newpath[i]);
}
if (res == -1) {
return inf[0].op_error;
}
return 0;
}
static int loopback_unlink(const char *path) {
int res = 0;
DEBUG_MSG("unlink path %s\n", path);
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newpath[NDEVS];
// remove file in each device
int i;
for (i = 0; i < NDEVS; i++) {
devices_newpath[i] = malloc(PATHSIZE);
strcpy(devices_newpath[i], devices_path[i]);
replace_path(path, devices_newpath[i]);
inf[i].path = devices_newpath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = UNLINK_OP;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newpath[i]);
}
if (res == -1) {
DEBUG_MSG("unlink path error %s\n", path, inf[0].op_error);
return inf[0].op_error;
}
return 0;
}
static int loopback_rmdir(const char *path) {
int res = 0;
int i;
DEBUG_MSG("rmdir\n");
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newpath[NDEVS];
// remove folder in each device
for (i = 0; i < NDEVS; i++) {
devices_newpath[i] = malloc(PATHSIZE);
strcpy(devices_newpath[i], devices_path[i]);
replace_path(path, devices_newpath[i]);
inf[i].path = devices_newpath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = RMDIR_OP;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newpath[i]);
}
if (res == -1) {
return inf[0].op_error;
}
return 0;
}
static int loopback_symlink(const char *from, const char *to) {
DEBUG_MSG("symlink\n");
int res;
int i;
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newfrompath[NDEVS];
char *devices_newtopath[NDEVS];
for (i = 0; i < NDEVS; i++) {
devices_newfrompath[i] = malloc(PATHSIZE);
strcpy(devices_newfrompath[i], devices_path[i]);
replace_path(from, devices_newfrompath[i]);
devices_newtopath[i] = malloc(PATHSIZE);
strcpy(devices_newtopath[i], devices_path[i]);
replace_path(to, devices_newtopath[i]);
inf[i].frompath = devices_newfrompath[i];
inf[i].topath = devices_newtopath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = SYMLINK_OP;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newfrompath[i]);
free(devices_newtopath[i]);
}
if (res == -1) {
return inf[0].op_error;
}
return 0;
}
static int loopback_rename(const char *from, const char *to) {
int res = 0;
DEBUG_MSG("rename\n");
int i;
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newfrompath[NDEVS];
char *devices_newtopath[NDEVS];
for (i = 0; i < NDEVS; i++) {
devices_newfrompath[i] = malloc(PATHSIZE);
strcpy(devices_newfrompath[i], devices_path[i]);
replace_path(from, devices_newfrompath[i]);
devices_newtopath[i] = malloc(PATHSIZE);
strcpy(devices_newtopath[i], devices_path[i]);
replace_path(to, devices_newtopath[i]);
inf[i].frompath = devices_newfrompath[i];
inf[i].topath = devices_newtopath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = RENAME_OP;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
DEBUG_MSG("Exit wait\n");
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newfrompath[i]);
free(devices_newtopath[i]);
}
if (res == -1) {
return inf[0].op_error;
}
if (DRIVER == ERASURE) {
DEBUG_MSG("Going to rename\n");
m_driver.rename((char *)from, (char *)to);
}
return 0;
}
static int loopback_link(const char *from, const char *to) {
DEBUG_MSG("link\n");
int res;
int i;
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
char *devices_newfrompath[NDEVS];
char *devices_newtopath[NDEVS];
for (i = 0; i < NDEVS; i++) {
devices_newfrompath[i] = malloc(PATHSIZE);
strcpy(devices_newfrompath[i], devices_path[i]);
replace_path(from, devices_newfrompath[i]);
devices_newtopath[i] = malloc(PATHSIZE);
strcpy(devices_newtopath[i], devices_path[i]);
replace_path(to, devices_newtopath[i]);
inf[i].frompath = devices_newfrompath[i];
inf[i].topath = devices_newtopath[i];
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = LINK_OP;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newfrompath[i]);
free(devices_newtopath[i]);
}
if (res == -1) {
return inf[0].op_error;
}
return 0;
}
static int loopback_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
DEBUG_MSG("create\n");
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
int res;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
struct mpath_aux *mp = malloc(sizeof(struct mpath_aux));
mp->devs_fd = malloc(sizeof(unsigned long) * NDEVS);
char *devices_newpath[NDEVS];
int i;
for (i = 0; i < NDEVS; i++) {
devices_newpath[i] = malloc(PATHSIZE);
strcpy(devices_newpath[i], devices_path[i]);
replace_path(path, devices_newpath[i]);
inf[i].path = devices_newpath[i];
inf[i].flags = fi->flags;
inf[i].mode = mode;
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = CREATE_OP;
DEBUG_MSG("sending create op %s\n", inf[i].path);
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newpath[i]);
}
if (res == -1) {
DEBUG_MSG("create error\n", inf[0].op_error);
free(mp->devs_fd);
free(mp);
return inf[0].op_error;
}
for (i = 0; i < NDEVS; i++) {
mp->devs_fd[i] = inf[i].op_res;
}
fi->fh = (unsigned long)mp;
if (DRIVER == ERASURE) {
DEBUG_MSG("GOING TO REMOVE keys if exist\n");
m_driver.create((char *)path);
}
return 0;
}
static int loopback_open(const char *path, struct fuse_file_info *fi) {
DEBUG_MSG("open\n");
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
int res;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
struct mpath_aux *mp = malloc(sizeof(struct mpath_aux));
mp->devs_fd = malloc(sizeof(unsigned long) * NDEVS);
char *devices_newpath[NDEVS];
int i;
for (i = 0; i < NDEVS; i++) {
devices_newpath[i] = malloc(PATHSIZE);
strcpy(devices_newpath[i], devices_path[i]);
replace_path(path, devices_newpath[i]);
inf[i].path = devices_newpath[i];
inf[i].flags = fi->flags;
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = OPEN_OP;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
for (i = 0; i < NDEVS; i++) {
free(devices_newpath[i]);
}
if (res == -1) {
DEBUG_MSG("open returned error %d\n", inf[0].op_error);
free(mp->devs_fd);
free(mp);
return inf[0].op_error;
}
for (i = 0; i < NDEVS; i++) {
mp->devs_fd[i] = inf[i].op_res;
}
fi->fh = (unsigned long)mp;
return 0;
}
static int loopback_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
// struct timespec tstart={0,0}, tend={0,0};
// clock_gettime(CLOCK_MONOTONIC, &tstart);
struct timeval tstart, tend;
gettimeofday(&tstart, NULL);
DEBUG_MSG("read size %lld and offset %lld\n", size, offset);
int res = 0;
int i;
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
unsigned char *magicblocks[NDEVS];
struct mpath_aux *mp = (struct mpath_aux *)(uintptr_t)fi->fh;
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
off_t driver_offset = 0;
uint64_t driver_size = 0;
if (DRIVER == ERASURE) {
DEBUG_MSG("Going to get driver_offset and drive-size for offset %lld\n", offset);
m_driver.get_driver_offset(path, offset, &driver_offset);
m_driver.get_driver_size(path, offset, &driver_size);
DEBUG_MSG("Driver-offset is %lld\n", driver_offset);
DEBUG_MSG("Driver-size is %lld\n", driver_size);
}
for (i = 0; i < NDEVS; i++) {
// TODO: newpath is only here for debug purposes
char newpath[PATHSIZE];
if (DRIVER != ERASURE) {
magicblocks[i] = malloc(size);
} else {
magicblocks[i] = malloc(driver_size);
}
strcpy(newpath, devices_path[i]);
replace_path(path, newpath);
DEBUG_MSG("Reading CONTENT at %s off %lld and size %lld\n", newpath, offset, size);
inf[i].fd = mp->devs_fd[i];
inf[i].buf = (char *)magicblocks[i];
inf[i].size = size;
inf[i].offset = offset;
inf[i].magicblocksize = -1;
inf[i].magicblockoffset = -1;
if (DRIVER == ERASURE) {
inf[i].magicblocksize = driver_size;
inf[i].magicblockoffset = driver_offset;
}
inf[i].cond = &wait_ops;
inf[i].lock = &op_lock;
inf[i].ops_done = &ops_done;
inf[i].op_type = READ_OP;
g_thread_pool_push(thread_pool, &inf[i], NULL);
}
res = wait_for_all_requests(inf, &op_lock, &wait_ops);
if (res > 0) {
int decode_size = size;
if (DRIVER == ERASURE) {
decode_size = driver_size;
}
DEBUG_MSG("Call result is %d\n", res);
m_driver.decode((unsigned char *)buf, magicblocks, decode_size, NDEVS);
}
for (i = 0; i < NDEVS; i++) {
free(magicblocks[i]);
}
pthread_mutex_destroy(&op_lock);
pthread_cond_destroy(&wait_ops);
// clock_gettime(CLOCK_MONOTONIC, &tend);
gettimeofday(&tend, NULL);
store(&multi_read_list, tstart, tend);
DEBUG_MSG("Exiting read operation %d\n", res);
return res;
}
static int loopback_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
// struct timespec tstart={0,0}, tend={0,0};
// clock_gettime(CLOCK_MONOTONIC, &tstart);
struct timeval tstart, tend;
gettimeofday(&tstart, NULL);
DEBUG_MSG("write\n");
int res;
int i = 0;
pthread_mutex_t op_lock;
pthread_cond_t wait_ops;
int ops_done = 0;
struct mpath_aux *mp = (struct mpath_aux *)(uintptr_t)fi->fh;
unsigned char *magicblocks[NDEVS];
struct op_info inf[NDEVS];
// Initialize mutex and condition variable
pthread_mutex_init(&op_lock, 0);
pthread_cond_init(&wait_ops, 0);
if (DRIVER != ERASURE) {
for (i = 0; i < NDEVS; i++) {
magicblocks[i] = malloc(size);
}
}
unsigned char block[size];
memcpy(block, buf, size);
m_driver.encode(path, magicblocks, block, offset, size, NDEVS);
off_t magicblockoffset = -1;
uint64_t magicblocksize = -1;
if (DRIVER == ERASURE) {
get_erasure_block_offset(path, offset, &magicblockoffset);
get_erasure_block_size(path, offset, &magicblocksize);
}
// this must be assync in the future
// Use a thread pool
for (i = 0; i < NDEVS; i++) {
// TODO: newpath is only here for debug purposes
char newpath[PATHSIZE];
strcpy(newpath, devices_path[i]);
replace_path(path, newpath);
inf[i].fd = mp->devs_fd[i];
inf[i].buf = (char *)magicblocks[i];
inf[i].size = size;
inf[i].offset = offset;
inf[i].magicblockoffset = magicblockoffset;
inf[i].magicblocksize = magicblocksize;
inf[i].cond = &wait_ops;