-
Notifications
You must be signed in to change notification settings - Fork 6
/
globus_gridftp_server_ceph.c
1119 lines (1020 loc) · 42.6 KB
/
globus_gridftp_server_ceph.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
/******************************************************************************
* GridFTP plugin for access to ceph object store
*
* @author Sebastien Ponce, [email protected]
*****************************************************************************/
#if defined(linux)
#define _LARGE_FILES
#define __USE_LARGEFILE64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <zlib.h>
#include <sys/xattr.h>
#include <limits.h>
#include "globus_gridftp_server.h"
#include "dsi_ceph.h"
#include "ceph_posix.h"
#define CA_MAXCKSUMLEN 32
#define CA_MAXCKSUMNAMELEN 15
static
globus_version_t local_version = {
0, /* major version number */
1, /* minor version number */
1157544130,
0 /* branch ID */
};
/*
* Utility function to get an integer value from the environment
*/
static int getconfigint(char *key) {
char *intStr = getenv(key);
if (NULL == intStr) {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: Invalid integer value '%s'\n",
"getconfigint", key);
}
return NULL == intStr ? 0 : atoi(intStr);
}
/*
* utility function to make errors
*/
static globus_result_t globus_l_gfs_make_error(const char *msg) {
char *err_str;
globus_result_t result;
GlobusGFSName(globus_l_gfs_make_error);
err_str = globus_common_create_string("%s error: %s", msg, strerror(errno));
result = GlobusGFSErrorGeneric(err_str);
globus_free(err_str);
return result;
}
/* fill the statbuf into globus_gfs_stat_t */
static void fill_stat_array(globus_gfs_stat_t * filestat, struct stat64 statbuf, char *name) {
filestat->mode = statbuf.st_mode;;
filestat->nlink = statbuf.st_nlink;
filestat->uid = statbuf.st_uid;
filestat->gid = statbuf.st_gid;
filestat->size = statbuf.st_size;
filestat->mtime = statbuf.st_mtime;
filestat->atime = statbuf.st_atime;
filestat->ctime = statbuf.st_ctime;
filestat->dev = statbuf.st_dev;
filestat->ino = statbuf.st_ino;
filestat->name = strdup(name);
}
/* free memory in stat_array from globus_gfs_stat_t->name */
static void free_stat_array(globus_gfs_stat_t * filestat,int count) {
int i;
for(i=0;i<count;i++) free(filestat[i].name);
}
/* free memory for the checksum list */
static void free_checksum_list(checksum_block_list_t *checksum_list) {
checksum_block_list_t *checksum_list_p;
checksum_block_list_t *checksum_list_pp;
checksum_list_p=checksum_list;
while(checksum_list_p->next!=NULL){
checksum_list_pp=checksum_list_p->next;
globus_free(checksum_list_p);
checksum_list_p=checksum_list_pp;
}
globus_free(checksum_list_p);
}
// comparison of 2 checksum_block_list_t* on their offset for the use of qsort
static int offsetComparison(const void *first, const void *second) {
checksum_block_list_t** f = (checksum_block_list_t**)first;
checksum_block_list_t** s = (checksum_block_list_t**)second;
long long int diff = (*f)->offset - (*s)->offset;
// Note that we cannot simply return diff as this function should return
// an int and the cast for values not fitting in 32 bits may screw things
if (0 == diff) return 0;
if (diff > 0) return 1;
return -1;
}
/* a replacement for zlib adler32_combine for SLC4 */
#define BASE 65521UL /* largest prime smaller than 65536 */
#define MOD(a) a %= BASE
static unsigned long adler32_combine_(unsigned int adler1,
unsigned int adler2,
globus_off_t len2) {
unsigned int sum1;
unsigned int sum2;
unsigned int rem;
/* the derivation of this formula is left as an exercise for the reader */
rem = (unsigned int)(len2 % BASE);
sum1 = adler1 & 0xffff;
sum2 = rem * sum1;
MOD(sum2);
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 >= BASE) sum1 -= BASE;
if (sum1 >= BASE) sum1 -= BASE;
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
if (sum2 >= BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}
static unsigned long adler32_0chunks(unsigned int len) {
return ((len%BASE) << 16) | 1;
}
static void ceph_logfunc_wrapper (char *format, va_list argp) {
// do the printing ourselves as we cannot call the variadic globus_gfs_log_message
int size = 1024;
char* logstr = (char*)malloc(size);
int written = vsnprintf(logstr, size, format, argp);
while (written >= size) {
size *=2;
logstr = (char*)realloc(logstr, size);
written = vsnprintf(logstr, size, format, argp);
}
// call log func with a single argument
(*globus_gfs_log_message)(GLOBUS_GFS_LOG_DUMP, "%s", logstr);
free(logstr);
}
/* a function to wrap all is needed to close a file */
static void globus_ceph_close(const char* func,
globus_l_gfs_ceph_handle_t* ceph_handle,
const char* error_msg) {
char* errorBuf = NULL;
ceph_handle->done = GLOBUS_TRUE;
ceph_posix_close(ceph_handle->fd);
if (error_msg) {
ceph_handle->cached_res = globus_l_gfs_make_error(error_msg);
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR, "%s: terminating transfer on error: %s\n", func, error_msg);
errorBuf = strdup(error_msg);
}
else {
ceph_handle->cached_res = GLOBUS_SUCCESS;
}
}
/*************************************************************************
* start
* -----
* This function is called when a new session is initialized, ie a user
* connectes to the server. This hook gives the dsi an opportunity to
* set internal state that will be threaded through to all other
* function calls associated with this session. And an oppertunity to
* reject the user.
*
* finished_info.info.session.session_arg should be set to an DSI
* defined data structure. This pointer will be passed as the void *
* user_arg parameter to all other interface functions.
*
* NOTE: at nice wrapper function should exist that hides the details
* of the finished_info structure, but it currently does not.
* The DSI developer should jsut follow this template for now
************************************************************************/
static void globus_l_gfs_ceph_start(globus_gfs_operation_t op,
globus_gfs_session_info_t *session_info) {
globus_l_gfs_ceph_handle_t *ceph_handle;
globus_gfs_finished_info_t finished_info;
char *func="globus_l_gfs_ceph_start";
GlobusGFSName(globus_l_gfs_ceph_start);
ceph_handle = (globus_l_gfs_ceph_handle_t *)
globus_malloc(sizeof(globus_l_gfs_ceph_handle_t));
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: started, uid: %u, gid: %u\n",
func, getuid(),getgid());
globus_mutex_init(&ceph_handle->mutex,NULL);
memset(&finished_info, '\0', sizeof(globus_gfs_finished_info_t));
finished_info.type = GLOBUS_GFS_OP_SESSION_START;
finished_info.result = GLOBUS_SUCCESS;
finished_info.info.session.session_arg = ceph_handle;
finished_info.info.session.username = session_info->username;
finished_info.info.session.home_dir = NULL; /* if null we will go to HOME directory */
ceph_handle->checksum_list=NULL;
ceph_handle->checksum_list_p=NULL;
globus_gridftp_server_operation_finished(op, GLOBUS_SUCCESS, &finished_info);
}
/*************************************************************************
* destroy
* -------
* This is called when a session ends, ie client quits or disconnects.
* The dsi should clean up all memory they associated with the session
* here.
************************************************************************/
static void globus_l_gfs_ceph_destroy(void *user_arg) {
globus_l_gfs_ceph_handle_t *ceph_handle;
ceph_handle = (globus_l_gfs_ceph_handle_t *) user_arg;
globus_mutex_destroy(&ceph_handle->mutex);
globus_free(ceph_handle);
}
/*************************************************************************
* stat
* ----
* This interface function is called whenever the server needs
* information about a given file or resource. It is called then an
* LIST is sent by the client, when the server needs to verify that
* a file exists and has the proper permissions, etc.
************************************************************************/
static void globus_l_gfs_ceph_stat(globus_gfs_operation_t op,
globus_gfs_stat_info_t *stat_info,
void *user_arg) {
globus_gfs_stat_t * stat_array;
int stat_count;
globus_l_gfs_ceph_handle_t * ceph_handle;
char * func="globus_l_gfs_ceph_stat";
struct stat64 statbuf;
int status=0;
globus_result_t result;
GlobusGFSName(globus_l_gfs_ceph_stat);
ceph_handle = (globus_l_gfs_ceph_handle_t *) user_arg;
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s: %s\n",
func, stat_info->pathname);
status = ceph_posix_stat64(stat_info->pathname, &statbuf);
if (status != 0) {
if (status == -EINVAL) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR, "%s: cannot get striper\n", __FUNCTION__);
}
result=globus_l_gfs_make_error("stat64");
globus_gridftp_server_finished_stat(op,result,NULL, 0);
return;
}
stat_array = (globus_gfs_stat_t *) globus_calloc(1, sizeof(globus_gfs_stat_t));
if (stat_array==NULL) {
result=GlobusGFSErrorGeneric("error: memory allocation failed");
globus_gridftp_server_finished_stat(op,result,NULL, 0);
return;
}
stat_count=1;
fill_stat_array(&(stat_array[0]), statbuf, stat_info->pathname);
globus_gridftp_server_finished_stat(op, GLOBUS_SUCCESS, stat_array, stat_count);
free_stat_array(stat_array, stat_count);
globus_free(stat_array);
return;
}
/*************************************************************************
* command
* -------
* This interface function is called when the client sends a 'command'.
* commands are such things as mkdir, remdir, delete. The complete
* enumeration is below.
*
* To determine which command is being requested look at:
* cmd_info->command
*
* GLOBUS_GFS_CMD_MKD = 1,
* GLOBUS_GFS_CMD_RMD,
* GLOBUS_GFS_CMD_DELE,
* GLOBUS_GFS_CMD_RNTO,
* GLOBUS_GFS_CMD_RNFR,
* GLOBUS_GFS_CMD_CKSM,
* GLOBUS_GFS_CMD_SITE_CHMOD,
* GLOBUS_GFS_CMD_SITE_DSI
************************************************************************/
static void globus_l_gfs_ceph_command(globus_gfs_operation_t op,
globus_gfs_command_info_t *cmd_info,
void *user_arg) {
globus_result_t result;
int cmd = cmd_info->command;
(void)user_arg;
GlobusGFSName(globus_l_gfs_ceph_command);
switch (cmd) {
/* Support DELE for GridPP FTS when the target already exists*/
case GLOBUS_GFS_CMD_DELE:
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "COMMAND is DELE\n");
errno = ENOENT;
result = GLOBUS_SUCCESS; // globus_l_gfs_make_error("File does not exist"); // ijj Lies
globus_gridftp_server_finished_command(op, result, GLOBUS_NULL);
return;
/*
* Support MKD because GridPP FTS thinks it needs to make a *directory* '/' for a non-existent target
* Target name sent as a Globus URL always contains a slash which tricks client into thinking it is
* dealing with a hierarchical pathname, not a Ceph object name
*/
case GLOBUS_GFS_CMD_MKD:
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "COMMAND is MKD\n");
result = GLOBUS_SUCCESS;
globus_gridftp_server_finished_command(op, result, GLOBUS_NULL);
return;
default:
break;
}
/* Complain if command is neither MKD nor DELE */
result=GlobusGFSErrorGeneric("error: commands other than MKD or DELE are denied");
globus_gridftp_server_finished_command(op, result, GLOBUS_NULL);
return;
}
int ceph_handle_open(char *path,
int flags,
int mode,
globus_l_gfs_ceph_handle_t *ceph_handle) {
int rc;
char * func="ceph_handle_open";
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: %s\n", func, path);
rc = ceph_posix_open(path, flags, mode);
ceph_handle->fileSize = 0;
return (rc);
}
/* receive from client */
static void globus_l_gfs_file_net_read_cb(globus_gfs_operation_t op,
globus_result_t result,
globus_byte_t *buffer,
globus_size_t nbytes,
globus_off_t offset,
globus_bool_t eof,
void *user_arg) {
globus_off_t start_offset;
globus_l_gfs_ceph_handle_t * ceph_handle;
ssize_t bytes_written;
unsigned long adler;
checksum_block_list_t** checksum_array;
checksum_block_list_t * checksum_list_pp;
unsigned long index;
unsigned long i;
unsigned long file_checksum;
char ckSumbuf[CA_MAXCKSUMLEN+1] = "0";
char * ckSumalg = "ADLER32"; /* we only support Adler32 for gridftp */
char * func = "globus_l_gfs_file_net_read_cb";
ceph_handle = (globus_l_gfs_ceph_handle_t *) user_arg;
globus_mutex_lock(&ceph_handle->mutex);
{
if (eof) ceph_handle->done = GLOBUS_TRUE;
ceph_handle->outstanding--;
if(result != GLOBUS_SUCCESS) {
ceph_handle->cached_res = result;
ceph_handle->done = GLOBUS_TRUE;
}
else if (nbytes > 0) {
start_offset = ceph_posix_lseek64(ceph_handle->fd, offset, SEEK_SET);
if (start_offset != offset) {
ceph_handle->cached_res = globus_l_gfs_make_error("seek");
ceph_handle->done = GLOBUS_TRUE;
} else {
bytes_written = ceph_posix_write(ceph_handle->fd, buffer, nbytes);
if (bytes_written < 0) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,"%s: write error, return code %d \n",func, -bytes_written);
ceph_handle->cached_res = GLOBUS_FAILURE;
ceph_handle->done = GLOBUS_TRUE;
} else {
/* fill the checksum list */
/* we will have a lot of checksums blocks in the list */
adler = adler32(0L, Z_NULL, 0);
adler = adler32(adler, buffer, nbytes);
ceph_handle->checksum_list_p->next=
(checksum_block_list_t *)globus_malloc(sizeof(checksum_block_list_t));
if (ceph_handle->checksum_list_p->next==NULL) {
ceph_handle->cached_res = GLOBUS_FAILURE;
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,"%s: malloc error \n",func);
ceph_handle->done = GLOBUS_TRUE;
globus_mutex_unlock(&ceph_handle->mutex);
return;
}
ceph_handle->checksum_list_p->next->next=NULL;
ceph_handle->checksum_list_p->offset=offset;
ceph_handle->checksum_list_p->size=bytes_written;
ceph_handle->checksum_list_p->csumvalue=adler;
ceph_handle->checksum_list_p=ceph_handle->checksum_list_p->next;
ceph_handle->number_of_blocks++;
/* end of the checksum section */
if((globus_size_t)bytes_written < nbytes) {
errno = ENOSPC;
ceph_handle->cached_res = globus_l_gfs_make_error("write");
ceph_handle->done = GLOBUS_TRUE;
free_checksum_list(ceph_handle->checksum_list);
} else {
globus_gridftp_server_update_bytes_written(op,offset,nbytes);
ceph_handle->fileSize += bytes_written;
}
}
}
}
globus_free(buffer);
/* if not done just register the next one */
if (!ceph_handle->done) globus_l_gfs_ceph_read_from_net(ceph_handle);
/* if done and there are no outstanding callbacks finish */
else if(ceph_handle->outstanding == 0) {
if (ceph_handle->number_of_blocks > 0) {
/* checksum calculation */
checksum_array=(checksum_block_list_t**)
globus_calloc(ceph_handle->number_of_blocks,sizeof(checksum_block_list_t*));
if (checksum_array == NULL) {
free_checksum_list(ceph_handle->checksum_list);
ceph_handle->fileSize = 0;
globus_ceph_close(func, ceph_handle, "Internal error (malloc failed)");
globus_mutex_unlock(&ceph_handle->mutex);
return;
}
checksum_list_pp=ceph_handle->checksum_list;
/* sorting of the list to the array */
index = 0;
/* the latest block is always empty and has next pointer as NULL */
while (checksum_list_pp->next != NULL) {
checksum_array[index] = checksum_list_pp;
checksum_list_pp=checksum_list_pp->next;
index++;
}
qsort(checksum_array, index, sizeof(checksum_block_list_t*), offsetComparison);
/* combine checksums, while making sure that we deal with missing chunks */
globus_off_t chkOffset = 0;
/* check whether first chunk is missing */
if (checksum_array[0]->offset != 0) {
/* first chunk is missing. Consider it full of 0s */
chkOffset = checksum_array[0]->offset;
file_checksum = adler32_combine_(adler32_0chunks(chkOffset),
checksum_array[0]->csumvalue,
checksum_array[0]->size);
} else {
file_checksum = checksum_array[0]->csumvalue;
}
chkOffset += checksum_array[0]->size;
/* go over all received chunks */
for (i = 1; i < ceph_handle->number_of_blocks; i++) {
/* check the continuity with previous chunk */
if (checksum_array[i]->offset != chkOffset) {
// not continuous, either a chunk is missing or we have overlapping chunks
if (checksum_array[i]->offset > chkOffset) {
// a chunk is missing, consider it full of 0s
globus_off_t doff = checksum_array[i]->offset - chkOffset;
file_checksum = adler32_combine_(file_checksum, adler32_0chunks(doff), doff);
chkOffset = checksum_array[i]->offset;
} else {
// overlapping chunks. This is not supported, fail the transfer
free_checksum_list(ceph_handle->checksum_list);
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,"%s: Overlapping chunks detected while handling 0x%x-0x%x. The overlap starts at 0x%x\n",
func,
checksum_array[i]->offset,
checksum_array[i]->offset+checksum_array[i]->size,
chkOffset);
globus_ceph_close(func, ceph_handle, "overlapping chunks detected when computing checksum");
globus_mutex_unlock(&ceph_handle->mutex);
globus_gridftp_server_finished_transfer(op, ceph_handle->cached_res);
return;
}
}
/* now handle the next chunk */
file_checksum=adler32_combine_(file_checksum,
checksum_array[i]->csumvalue,
checksum_array[i]->size);
chkOffset += checksum_array[i]->size;
}
sprintf(ckSumbuf, "%lx", file_checksum);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "%s: checksum for fd %d : AD 0x%lx\n",
func,ceph_handle->fd, file_checksum);
globus_free(checksum_array);
free_checksum_list(ceph_handle->checksum_list);
/* set extended attributes */
if (ceph_posix_fsetxattr(ceph_handle->fd,"user.checksum.type",
ckSumalg, strlen(ckSumalg), 0)) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,"%s: unable to store checksum type as xattr\n", func);
}
else if (ceph_posix_fsetxattr(ceph_handle->fd,"user.checksum.value",
ckSumbuf, strlen(ckSumbuf), 0)) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,"%s: unable to store checksum value as xattr\n", func);
}
}
globus_ceph_close(func, ceph_handle, NULL);
globus_gridftp_server_finished_transfer(op, ceph_handle->cached_res);
}
}
globus_mutex_unlock(&ceph_handle->mutex);
}
static void globus_l_gfs_ceph_read_from_net
(globus_l_gfs_ceph_handle_t *ceph_handle) {
globus_byte_t * buffer;
globus_result_t result;
char * func="globus_l_gfs_ceph_read_from_net";
GlobusGFSName(globus_l_gfs_ceph_read_from_net);
/* in the read case this number will vary */
globus_gridftp_server_get_optimal_concurrency(ceph_handle->op,
&ceph_handle->optimal_count);
while(ceph_handle->outstanding < ceph_handle->optimal_count) {
buffer=globus_malloc(ceph_handle->block_size);
if (buffer == NULL) {
if (ceph_handle->outstanding == 0) {
globus_ceph_close(func, ceph_handle, "internal error (malloc failed)");
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return;
}
result= globus_gridftp_server_register_read(ceph_handle->op,
buffer,
ceph_handle->block_size,
globus_l_gfs_file_net_read_cb,
ceph_handle);
if(result != GLOBUS_SUCCESS) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,
"%s: register read has finished with a bad result\n",
func);
globus_free(buffer);
ceph_handle->cached_res = result;
if (ceph_handle->outstanding == 0) {
globus_ceph_close(func, ceph_handle, "register read has finished with a bad result");
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return;
}
ceph_handle->outstanding++;
}
}
/*************************************************************************
* recv
* ----
* This interface function is called when the client requests that a
* file be transfered to the server.
*
* To receive a file the following functions will be used in roughly
* the presented order. They are doced in more detail with the
* gridftp server documentation.
*
* globus_gridftp_server_begin_transfer();
* globus_gridftp_server_register_read();
* globus_gridftp_server_finished_transfer();
*
************************************************************************/
static void globus_l_gfs_ceph_recv(globus_gfs_operation_t op,
globus_gfs_transfer_info_t *transfer_info,
void *user_arg) {
globus_l_gfs_ceph_handle_t * ceph_handle;
globus_result_t result;
char * func="globus_l_gfs_ceph_recv";
char * pathname;
int flags;
GlobusGFSName(globus_l_gfs_ceph_recv);
ceph_handle = (globus_l_gfs_ceph_handle_t *) user_arg;
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: started\n",func);
pathname=strdup(transfer_info->pathname);
if(pathname==NULL) {
result=GlobusGFSErrorGeneric("error: strdup failed");
globus_gridftp_server_finished_transfer(op, result);
return;
}
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: pathname: %s \n",func,pathname);
struct stat64 sbuf;
int rc = ceph_posix_stat64(pathname, &sbuf);
int mode = 0666;
flags = O_WRONLY | O_CREAT;
if (rc == 0) { // File exists
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s : File %s exists - about to truncate\n", __FUNCTION__, pathname);
/* if (transfer_info->alloc_size < sbuf.st_size) */
rc = ceph_posix_truncate(pathname,mode, flags, 0);
if (rc != 0) {
/* globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s : Cannot truncate %s\n", __FUNCTION__, pathname); // reported in ceph_posix_truncate */
free(pathname);
result = globus_l_gfs_make_error("open/truncate");
globus_gridftp_server_finished_transfer(op, result);
return;
}
}
/* try to open */
if(transfer_info->truncate) flags |= O_TRUNC;
ceph_handle->fd = ceph_handle_open(pathname, flags, 0644, ceph_handle);
if (ceph_handle->fd < 0) {
errno = EACCES;
result=globus_l_gfs_make_error("open/create");
free(pathname);
globus_gridftp_server_finished_transfer(op, result);
return;
}
/* reset all the needed variables in the handle */
ceph_handle->cached_res = GLOBUS_SUCCESS;
ceph_handle->outstanding = 0;
ceph_handle->done = GLOBUS_FALSE;
ceph_handle->blk_length = 0;
ceph_handle->blk_offset = 0;
ceph_handle->op = op;
globus_gridftp_server_get_block_size(op, &ceph_handle->block_size);
int blksize = getconfigint("GRIDFTP_CEPH_WRITE_SIZE");
if (blksize > 0) {
ceph_handle->block_size = blksize;
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: Invalid %s block_size: %ld\n",
func, "GRIDFTP_CEPH_WRITE_SIZE", blksize);
}
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: block size: %ld\n",
func,ceph_handle->block_size);
/* here we will save all checksums for the file blocks */
/* malloc memory for the first element in the checksum list */
/* we should always have at least one block for a file */
ceph_handle->checksum_list=
(checksum_block_list_t *)globus_malloc(sizeof(checksum_block_list_t));
if (ceph_handle->checksum_list==NULL) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,"%s: malloc error \n",func);
globus_gridftp_server_finished_transfer(op, GLOBUS_FAILURE);
return;
}
ceph_handle->checksum_list->next=NULL;
ceph_handle->checksum_list_p=ceph_handle->checksum_list;
ceph_handle->number_of_blocks=0;
globus_gridftp_server_begin_transfer(op, 0, ceph_handle);
globus_mutex_lock(&ceph_handle->mutex);
{
globus_l_gfs_ceph_read_from_net(ceph_handle);
}
globus_mutex_unlock(&ceph_handle->mutex);
free(pathname);
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: finished\n",func);
return;
}
/*************************************************************************
* send
* ----
* This interface function is called when the client requests to receive
* a file from the server.
*
* To send a file to the client the following functions will be used in roughly
* the presented order. They are doced in more detail with the
* gridftp server documentation.
*
* globus_gridftp_server_begin_transfer();
* globus_gridftp_server_register_write();
* globus_gridftp_server_finished_transfer();
*
************************************************************************/
static void globus_l_gfs_ceph_send(globus_gfs_operation_t op,
globus_gfs_transfer_info_t *transfer_info,
void *user_arg) {
globus_l_gfs_ceph_handle_t * ceph_handle;
char * func="globus_l_gfs_ceph_send";
char * pathname;
int i;
globus_bool_t done;
globus_result_t result;
GlobusGFSName(globus_l_gfs_ceph_send);
ceph_handle = (globus_l_gfs_ceph_handle_t *) user_arg;
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: started\n",func);
pathname=strdup(transfer_info->pathname);
if (pathname == NULL) {
result = GlobusGFSErrorGeneric("error: strdup failed");
globus_gridftp_server_finished_transfer(op, result);
return;
}
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: pathname: %s\n",func,pathname);
/* Check whether the file exists before going any further */
struct stat64 sbuf;
int rc = ceph_posix_stat64(pathname, &sbuf);
if (rc != 0) {
result = globus_l_gfs_make_error("open");
free(pathname);
globus_gridftp_server_finished_transfer(op, result);
return;
}
/* mode is ignored */
ceph_handle->fd = ceph_handle_open(pathname, O_RDONLY,
0, ceph_handle);
if (ceph_handle->fd < 0) {
result = globus_l_gfs_make_error("open");
free(pathname);
globus_gridftp_server_finished_transfer(op, result);
return;
}
/* reset all the needed variables in the handle */
ceph_handle->cached_res = GLOBUS_SUCCESS;
ceph_handle->outstanding = 0;
ceph_handle->done = GLOBUS_FALSE;
ceph_handle->blk_length = 0;
ceph_handle->blk_offset = 0;
ceph_handle->op = op;
globus_gridftp_server_get_optimal_concurrency(op, &ceph_handle->optimal_count);
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: optimal_concurrency: %u\n",
func,ceph_handle->optimal_count);
globus_gridftp_server_get_block_size(op, &ceph_handle->block_size);
int blksize = getconfigint("GRIDFTP_CEPH_READ_SIZE");
if (blksize > 0) {
ceph_handle->block_size = blksize;
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: Invalid %s block_size: %ld\n",
func, "GRIDFTP_CEPH_READ_SIZE", blksize);
}
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: block_size: %ld\n",
func,ceph_handle->block_size);
/* here we will save all checksums for the file blocks */
/* malloc memory for the first element in the checksum list */
/* we should always have at least one block for a file */
ceph_handle->checksum_list=
(checksum_block_list_t *)globus_malloc(sizeof(checksum_block_list_t));
if (ceph_handle->checksum_list==NULL) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,"%s: malloc error \n",func);
globus_gridftp_server_finished_transfer(op, GLOBUS_FAILURE);
return;
}
ceph_handle->checksum_list->next=NULL;
ceph_handle->checksum_list_p=ceph_handle->checksum_list;
ceph_handle->number_of_blocks=0;
globus_gridftp_server_begin_transfer(op, 0, ceph_handle);
done = GLOBUS_FALSE;
globus_mutex_lock(&ceph_handle->mutex);
{
for(i = 0; i < ceph_handle->optimal_count && !done; i++) {
done = globus_l_gfs_ceph_send_next_to_client(ceph_handle);
}
}
globus_mutex_unlock(&ceph_handle->mutex);
free(pathname);
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: finished\n",func);
}
static globus_bool_t globus_l_gfs_ceph_send_next_to_client
(globus_l_gfs_ceph_handle_t *ceph_handle) {
globus_result_t result;
globus_result_t res;
globus_off_t read_length;
globus_off_t nbread;
globus_off_t start_offset;
globus_byte_t * buffer;
unsigned long adler;
checksum_block_list_t** checksum_array;
checksum_block_list_t * checksum_list_pp;
unsigned long index;
unsigned long i;
unsigned long file_checksum;
char ckSumbuf[CA_MAXCKSUMLEN+1];
char ckSumbufdisk[CA_MAXCKSUMLEN+1];
char ckSumnamedisk[CA_MAXCKSUMNAMELEN+1];
char useCksum;
int xattr_len;
char * func = "globus_l_gfs_ceph_send_next_to_client";
GlobusGFSName(globus_l_gfs_ceph_send_next_to_client);
if (ceph_handle->blk_length == 0) {
/* check the next range to read */
globus_gridftp_server_get_read_range(ceph_handle->op,
&ceph_handle->blk_offset,
&ceph_handle->blk_length);
if(ceph_handle->blk_length == 0) {
globus_ceph_close(func, ceph_handle, NULL);
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return ceph_handle->done;
}
}
if (ceph_handle->blk_length == -1 ||
(globus_size_t)ceph_handle->blk_length > ceph_handle->block_size)
read_length = ceph_handle->block_size;
else read_length = ceph_handle->blk_length;
start_offset = ceph_posix_lseek64(ceph_handle->fd,
ceph_handle->blk_offset,
SEEK_SET);
/* verify that it worked */
if (start_offset != ceph_handle->blk_offset) {
globus_ceph_close(func, ceph_handle, "failed to seek");
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return ceph_handle->done;
}
buffer = globus_malloc(read_length);
if (buffer == NULL) {
globus_ceph_close(func, ceph_handle, "internal error (malloc failed)");
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return ceph_handle->done;
}
nbread = ceph_posix_read(ceph_handle->fd, buffer, read_length);
if (nbread>0) {
/* fill the checksum list */
adler = adler32(0L, Z_NULL, 0);
adler = adler32(adler, buffer, nbread);
ceph_handle->checksum_list_p->next=
(checksum_block_list_t *)globus_malloc(sizeof(checksum_block_list_t));
if (ceph_handle->checksum_list_p->next==NULL) {
globus_free(buffer);
globus_ceph_close(func, ceph_handle, "internal error (malloc failed)");
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return ceph_handle->done;
}
ceph_handle->checksum_list_p->next->next=NULL;
ceph_handle->checksum_list_p->offset=ceph_handle->blk_offset;
ceph_handle->checksum_list_p->size=nbread;
ceph_handle->checksum_list_p->csumvalue=adler;
ceph_handle->checksum_list_p=ceph_handle->checksum_list_p->next;
ceph_handle->number_of_blocks++;
}
if(nbread == 0) { /* eof */
result = GLOBUS_SUCCESS;
globus_free(buffer);
/* checksum calculation */
checksum_array=
(checksum_block_list_t**)globus_calloc(ceph_handle->number_of_blocks,
sizeof(checksum_block_list_t*));
if (checksum_array==NULL){
free_checksum_list(ceph_handle->checksum_list);
globus_ceph_close(func, ceph_handle, "internal error (malloc failed)");
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return ceph_handle->done;
}
checksum_list_pp=ceph_handle->checksum_list;
/* sorting of the list to the array */
index = 0;
/* the latest block is always empty and has next pointer as NULL */
while (checksum_list_pp->next != NULL) {
checksum_array[index] = checksum_list_pp;
checksum_list_pp=checksum_list_pp->next;
index++;
}
qsort(checksum_array, index, sizeof(checksum_block_list_t*), offsetComparison);
/* combine here */
/* ************* */
file_checksum=checksum_array[0]->csumvalue;
for (i=1;i<ceph_handle->number_of_blocks;i++) {
file_checksum=adler32_combine_(file_checksum,checksum_array[i]->csumvalue,
checksum_array[i]->size);
}
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,"%s: checksum for fd %d : AD 0x%lx\n",
func,ceph_handle->fd,file_checksum);
globus_free(checksum_array);
free_checksum_list(ceph_handle->checksum_list);
/* get extended attributes */
useCksum=1;
xattr_len = ceph_posix_fgetxattr(ceph_handle->fd,
"user.checksum.type",
ckSumnamedisk,
CA_MAXCKSUMNAMELEN);
if (xattr_len < 0) {
/* no error messages */
useCksum = 0;
} else {
ckSumnamedisk[xattr_len] = '\0';
xattr_len = ceph_posix_fgetxattr(ceph_handle->fd,
"user.checksum.value",
ckSumbufdisk,CA_MAXCKSUMLEN);
if (xattr_len < 0) {
/* error */
ceph_handle->cached_res =
globus_error_put (globus_object_construct (GLOBUS_ERROR_TYPE_BAD_DATA));
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
char errorBuf[1024];
sprintf(errorBuf, "error detected while reading checksum for fd: %d, errono=%d\n", ceph_handle->fd, -xattr_len);
globus_ceph_close(func, ceph_handle, errorBuf);
return ceph_handle->done;
} else {
ckSumbufdisk[xattr_len] = '\0';
if (strncmp(ckSumnamedisk,"ADLER32",CA_MAXCKSUMNAMELEN) != 0) {
useCksum=1; /* for gridftp we know only ADLER32 */
}
}
}
if (useCksum) { /* we have disks and on the fly checksums here */
sprintf(ckSumbuf, "%lx", file_checksum);
if (strncmp(ckSumbufdisk,ckSumbuf,CA_MAXCKSUMLEN)==0) {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: checksums OK! \n",func);
} else {
char errorBuf[1024];
sprintf(errorBuf, "checksum error detected reading fd: %d (recorded checksum: 0x%s calculated checksum: 0x%s)\n",
ceph_handle->fd,
ckSumbufdisk,
ckSumbuf);
/* to do something in error case */
globus_ceph_close(func, ceph_handle, errorBuf);
ceph_handle->cached_res =
globus_error_put (globus_object_construct (GLOBUS_ERROR_TYPE_BAD_DATA));
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return ceph_handle->done;
}
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,
"%s: ADLER32 checksum has not been found in extended attributes\n",
func);
}
globus_ceph_close(func, ceph_handle, NULL);
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,"%s: finished (eof)\n",func);
return ceph_handle->done;
}
if (nbread < 0) { /* error */
globus_free(buffer);
globus_ceph_close(func, ceph_handle, "error reading from disk");
if (ceph_handle->outstanding == 0) {
globus_gridftp_server_finished_transfer(ceph_handle->op,
ceph_handle->cached_res);
}
return ceph_handle->done;
}
if (read_length>=nbread) {
/* if we have a file with size less than block_size we do not have use parrallel connections (one will be enough) */
ceph_handle->optimal_count--;
}
read_length = nbread;
if (ceph_handle->blk_length != -1) {
ceph_handle->blk_length -= read_length;
}
/* start offset? */