forked from sponce/gridFTPCephPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobus_gridftp_server_ceph.c
More file actions
2025 lines (1614 loc) · 70 KB
/
globus_gridftp_server_ceph.c
File metadata and controls
2025 lines (1614 loc) · 70 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
/******************************************************************************
* GridFTP plugin for access to ceph object store
*
* @author Sebastien Ponce, sebastien.ponce@cern.ch
*****************************************************************************/
#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 <stdlib.h>
#include "globus_gridftp_server.h"
#include "dsi_ceph.h"
#include "ceph_posix.h"
#include "gridftp_checkaccess.h"
#include "assert.h"
// Adler32 checksum length is 8 characters
#define CA_MAXCKSUMLEN 8
#define CA_MAXCKSUMNAMELEN 15
char *authdbFilename;
char* authdbProg;
#define ERRORMSGSIZE 256
char errorstr[ERRORMSGSIZE];
static
globus_version_t local_version = {
0, /* major version number */
1, /* minor version number */
1157544130,
0 /* branch ID */
};
char *cleanup_pathname(char *instr) {
char *out;
if (instr[0] == '/') { // Zap a leading forward slash
instr++;
}
if (strchr(instr, '/') == NULL) { // If no other slashes, add one at end
out = (char *)malloc(strlen(instr) + 2);
strcpy(out, instr);
strcat(out, "/");
} else {
out = (char *)malloc(strlen(instr)+1);
strcpy(out, instr);
}
return out;
}
static char* VO_Role;
/*
* Utility function to get an integer value from the environment
*/
static int getconfigint(const 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;
}
}
int checkFileExists(const char* filename) {
struct stat64 statbuf;
return !stat64(filename, &statbuf);
}
char* checkFileFromConf(const char* confKey, const char* defaultVal) {
char* returnVal = NULL;
char *testVal;
const char* confVal = getenv(confKey);
if (confVal == NULL) {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Cannot find configuration - check setting of %s in /etc/gridftp.conf\n",
__FUNCTION__, confKey);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Setting file location to default, %s\n",
__FUNCTION__, defaultVal);
testVal = (char *)defaultVal;
} else {
testVal = (char *)confVal;
}
if (!checkFileExists(testVal)) {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Problem accessing file %s: %s\n", __FUNCTION__, testVal, strerror(errno));
} else {
returnVal = strdup(testVal);
}
return returnVal;
}
void set_finished_info(
globus_gfs_finished_info_t* finished_info,
globus_gfs_session_info_t *session_info,
globus_l_gfs_ceph_handle_t* ceph_handle,
globus_result_t result) {
memset(finished_info, '\0', sizeof (globus_gfs_finished_info_t));
finished_info->type = GLOBUS_GFS_OP_SESSION_START; // Always this value
finished_info->result = result;
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 */
}
static assembly_t *create_assembly_buffer(
globus_off_t *buffer_start,
int buffer_start_val,
globus_off_t *buffer_end,
int buffer_end_val,
int rebuff_size,
int start_offset) {
assembly_t * assembly_buff = (assembly_t *)malloc(sizeof(assembly_t));
*buffer_start = buffer_start_val; //* ceph_handle->overflow_start */ = rebuff_size;
*buffer_end = buffer_end_val; // ceph_handle->overflow_end = start_offset /* ceph_handle->overflow_start */ + rebuff_size-1;
assembly_buff->buffer = (globus_byte_t *)malloc(rebuff_size * sizeof(globus_byte_t));
assembly_buff->nbytes = 0;
return assembly_buff; // ceph_handle->overflow_buff = obuff;
}
/*************************************************************************
* 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);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "%s: host_id = %s, mapped rolename = %s\n",
func, session_info->host_id, session_info->username);
authdbProg = checkFileFromConf("GRIDFTP_CEPH_AUTHDB_PROG", "/usr/bin/xrdacctest");
if (authdbProg == NULL) {
set_finished_info(&finished_info, session_info, ceph_handle, GLOBUS_FAILURE);
globus_gridftp_server_operation_finished(op, GLOBUS_FAILURE, &finished_info);
return;
}
authdbFilename = checkFileFromConf("GRIDFTP_CEPH_AUTHDB_FILE", "/etc/grid-security/authdb");
if (authdbFilename == NULL) {
set_finished_info(&finished_info, session_info, ceph_handle, GLOBUS_FAILURE);
globus_gridftp_server_operation_finished(op, GLOBUS_FAILURE, &finished_info);
return;
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s: GRIDFTP_CEPH_AUTHDB_FILE = %s\n",
func, authdbFilename);
}
const char* radosUserKey = "GRIDFTP_CEPH_RADOS_USER";
const char* radosUserId = getenv(radosUserKey);
if (radosUserId == NULL) {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Cannot find configuration - check setting of %s in /etc/gridftp.conf\n",
__FUNCTION__, radosUserKey);
globus_gridftp_server_operation_finished(op, GLOBUS_FAILURE, &finished_info);
return;
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s: RADOS USER ID = %s\n",
func, radosUserId);
}
ceph_posix_set_radosUserId(radosUserId);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "%s: session_info->username = %s\n",
func, session_info->username);
VO_Role = strdup(session_info->username);
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s: VO_Role = %s\n",
func, VO_Role);
set_finished_info(&finished_info, session_info, ceph_handle, GLOBUS_SUCCESS);
ceph_handle->checksum_list = NULL;
ceph_handle->checksum_list_p = NULL;
// const char* confSize = "GRIDFTP_CEPH_MODE_E_WRITE_SIZE";
// int rebuff_size = getconfigint(confSize);
// globus_gfs_log_message(GLOBUS_GFS_LOG_INFO, "%s: GRIDFTP_CEPH_MODE_E_WRITE_SIZE = %d\n",
// func, rebuff_size);
//
// const int lowpower = 20, highpower = 30, defaultpower = 29;
//
// if (rebuff_size >= lowpower && rebuff_size <= highpower) {
// rebuff_size = 1 << rebuff_size;
// globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
// "%s: buffer size set to %d bytes\n", __FUNCTION__, rebuff_size);
//
// } else {
// rebuff_size = 1 << defaultpower;
// globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
// "%s: Invalid setting for %s, Range is %d to %d. Defaulting to 2^%d bytes (%d)\n",
// func, confSize, lowpower, highpower, defaultpower, rebuff_size);
// }
//
// assembly_t * abuff = (assembly_t *)malloc(sizeof(assembly_t));
//
// ceph_handle->rebuff_size = rebuff_size;
// ceph_handle->active_start = 0;
// ceph_handle->active_end = rebuff_size-1;
//
// abuff->buffer = (globus_byte_t *)malloc(rebuff_size * sizeof(globus_byte_t));
// abuff->nbytes = 0;
//
// ceph_handle->active_buff = abuff;
//
// assembly_t * obuff = (assembly_t *)malloc(sizeof(assembly_t));
//
// ceph_handle->overflow_start = rebuff_size;
// ceph_handle->overflow_end = ceph_handle->overflow_start + rebuff_size-1;
//
// obuff->buffer = (globus_byte_t *)malloc(rebuff_size * sizeof(globus_byte_t));
// obuff->nbytes = 0;
//
//
// ceph_handle->overflow_buff = obuff;
//
// ceph_handle->nblocks_in_range = 0;
// ceph_handle->nblocks_in_overflow = 0;
globus_gridftp_server_operation_finished(op, GLOBUS_SUCCESS, &finished_info);
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,"%s: leaving.\n", func);
}
/*************************************************************************
* 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);
char* pathname_to_test = cleanup_pathname(stat_info->pathname);
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s: %s\n",
func, stat_info->pathname);
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,
"%s: prog= %s, authdb= %s, VO_Role= %s, op= %s, pathname= %s\n",
func, authdbProg, authdbFilename, VO_Role, "rd", pathname_to_test);
int allowed = checkAccess(authdbProg, authdbFilename, VO_Role, "rd", pathname_to_test);
if (!allowed) {
result = GlobusGFSErrorGeneric("globus_l_gfs_ceph_stat: authorization error: 'MLST' operation not allowed");
globus_gridftp_server_finished_stat(op, result, NULL, 0);
return;
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Authorization.success: 'MLST' operation allowed\n", func);
}
if (!strcmp("/", stat_info->pathname)) { // Make the root directory "/" - FTS needs some hand-holding
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Looks like a stat on '/' for %s.\n", __FUNCTION__, stat_info->pathname);
// Sometimes, the FTS client will send a 'MLST /' command when the target doesn't exist
// Return some fake stat information
//
stat_count = 1;
statbuf.st_uid = 0;
statbuf.st_gid = 0;
statbuf.st_mode = __S_IFDIR|0666;
statbuf.st_size = 0;
statbuf.st_atime = 0;
statbuf.st_ctime = 0;
statbuf.st_mtime = 0;
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);
} else { // It's a proper objectname
status = ceph_posix_stat64(stat_info->pathname, &statbuf);
if (status != 0) {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Return from stat64 = %d\n", __FUNCTION__, status);
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) {
GlobusGFSName(globus_l_gfs_ceph_command);
globus_result_t result;
int allowed;
char errormessage[ERRORMSGSIZE];
char* pathname_to_test = cleanup_pathname(cmd_info->pathname);
switch (cmd_info->command) {
/* Support DELE for GridPP FTS when the target already exists*/
case GLOBUS_GFS_CMD_DELE:
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: DELE %s\n", __FUNCTION__, cmd_info->pathname);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: checkAccess (VORole = %s, op = %s, pathname = %s\n",
__FUNCTION__, VO_Role, "wr", pathname_to_test);
allowed = checkAccess(authdbProg, authdbFilename, VO_Role, "wr", pathname_to_test);
if (!allowed) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,
"%s: Authorization failure: cannot DELE %s\n", __FUNCTION__, cmd_info->pathname);
snprintf(errormessage, ERRORMSGSIZE, "Authorization error: DELE operation for role %s not allowed on %s",
VO_Role, cmd_info->pathname);
result = GlobusGFSErrorGeneric(errormessage);
errno = ENOENT;
globus_gridftp_server_finished_command(op, result, GLOBUS_NULL);
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: Authorization success: DELE operation allowed\n", __FUNCTION__);
int status = ceph_posix_delete(cmd_info->pathname);
if (status != 0) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,
"DELE return code is %d\n", status); // Log the actual failure reason...
//errno = -status; // Return error code to client
snprintf(errormessage, ERRORMSGSIZE, "%s", strerror(-status));
result = GlobusGFSErrorGeneric(errormessage);
globus_gridftp_server_finished_command(op, result, errormessage /* GLOBUS_NULL */);
} else {
errno = 0;
globus_gridftp_server_finished_command(op, GLOBUS_SUCCESS, 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
* We fool the FTS client (which isn't aware that the Ceph object store doesn't support directories)
* into thinking that is has created the directory it wants to see, and can then continue with the rest of the
* FTS transfer
*
*/
case GLOBUS_GFS_CMD_MKD:
/*
*
* Check if FTS responds sensibly when it tries to make a parent directory? Or should it have
* already been denied 'wr' access?
*/
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: MKD %s\n", __FUNCTION__, cmd_info->pathname);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: checkAccess (VORole = %s, op = %s, pathname = %s\n",
__FUNCTION__, VO_Role, "wr", pathname_to_test);
allowed = checkAccess(authdbProg, authdbFilename, VO_Role, "wr", pathname_to_test);
if (!allowed) {
snprintf(errormessage, ERRORMSGSIZE,
"Authorization error: MKD operation for role %s not allowed on %s",
VO_Role, cmd_info->pathname);
result = GlobusGFSErrorGeneric(errormessage);
globus_gridftp_server_finished_command(op, result, errormessage);
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: acc.success: MKD operation allowed\n", __FUNCTION__);
/*
* The core of GrdiFTP will return a '257 MKD <pathname> Pathname: Created Successfully message'
* We don't need to do anything server-side.
*/
;
globus_gridftp_server_finished_command(op, GLOBUS_SUCCESS, GLOBUS_NULL);
}
return;
/*
* Support CKSM command. This DSI only stores checksums using the ADLER32 algorithm.
* To-do: Check whether objects stored by XROOTD DSI have a checksum stored
*
*/
case GLOBUS_GFS_CMD_CKSM:
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: CKSM %s\n", __FUNCTION__, cmd_info->pathname);
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"%s: checkAccess (VORole = %s, op = %s, pathname = %s\n",
__FUNCTION__, VO_Role, "rd", pathname_to_test);
allowed = checkAccess(authdbProg, authdbFilename, VO_Role, "rd", pathname_to_test);
if (!allowed) {
(void)snprintf(errormessage, ERRORMSGSIZE,
"Authorization error: CKSM operation for role %s on %s not allowed.", VO_Role, cmd_info->pathname);
result = GlobusGFSErrorGeneric(errormessage);
globus_gridftp_server_finished_command(op, GLOBUS_FAILURE, errormessage);
} else {
globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
"authz .success: CKSM operation allowed\n");
// cksm_alg = cmd_info->cksm_alg; // In case we support checksums other than ADLER32 in the future...
// /** offset for cksm command */
// cksm_offset = cmd_info->cksm_offset;
// /** length of data to read for cksm command -1 means full file */
// cksm_length = cmd_info->cksm_length;
char *checksum = ceph_posix_get_checksum(cmd_info->pathname); // TO-DO: Should check for error status
globus_result_t result;
if (checksum == NULL) { // [0] == '-') {
errno = ENOENT;
result = globus_l_gfs_make_error("checksum: open");
} else {
errno = 0;
result = GLOBUS_SUCCESS;
}
globus_gridftp_server_finished_command(op, result, checksum);
}
return;
default:
break;
}
/* Complain if command is neither CKSM, DELE, or MKD */
globus_gridftp_server_finished_command(op,
GlobusGFSErrorGeneric("error: commands other than CKSM, DELE, or MKD are denied"), GLOBUS_NULL);
return;
}
unsigned long add_checksum_to_list(
globus_l_gfs_ceph_handle_t *ceph_handle,
globus_byte_t *buffer,
globus_off_t offset,
globus_size_t nbytes) {
unsigned long adler;
/* 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) {
return GLOBUS_FALSE;
}
ceph_handle->checksum_list_p->next->next = NULL;
ceph_handle->checksum_list_p->offset = offset;
ceph_handle->checksum_list_p->size = nbytes;
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 */
return GLOBUS_TRUE;
}
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);
}
/* combine checksums, while making sure that we deal with missing chunks */
unsigned long get_file_checksum(
globus_l_gfs_ceph_handle_t *ceph_handle,
checksum_block_list_t** checksum_array,
globus_off_t chkOffset,
unsigned long file_checksum) {
// unsigned long file_checksum;
unsigned long i;
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 {
return 0; // Should have a break here instead - single 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;
}
return file_checksum;
}
checksum_block_list_t** checksum_list_to_array(
globus_l_gfs_ceph_handle_t *ceph_handle) {
checksum_block_list_t** checksum_array =(checksum_block_list_t**)
globus_calloc(ceph_handle->number_of_blocks, sizeof(checksum_block_list_t*));
if (checksum_array == NULL) {
return NULL;
}
checksum_block_list_t *checksum_list_pp = ceph_handle->checksum_list;
/* sorting of the list to the array */
int 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);
return checksum_array;
}
#define BUFFER_WRITE 1
#define BUFFER_CONTINUE 2
#define BUFFER_OUT_OF_RANGE 3
int build_buffer(
globus_l_gfs_ceph_handle_t * ceph_handle,
globus_byte_t *buffer, globus_off_t offset,
globus_size_t nbytes, globus_bool_t eof) {
if (offset < ceph_handle->active_start) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR, "%s: Offset %lld < active_start of %lld \n",
__FUNCTION__, offset, ceph_handle->active_start );
return BUFFER_OUT_OF_RANGE;
}
assembly_t *dest_buff;
if (offset + nbytes - 1 <= ceph_handle->active_end) { // In ACTIVE range
dest_buff = ceph_handle->active_buff;
int index = offset - ceph_handle->active_start;
memcpy(dest_buff->buffer+index, buffer, nbytes);
dest_buff->nbytes += nbytes;
++ceph_handle->nblocks_in_range;
if (dest_buff->nbytes == ceph_handle->rebuff_size || eof) {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP,
"%s: Returning BUFFER_WRITE (%lld, %lld), n_inrange = %d, n_overflow = %d\n",
__FUNCTION__,
ceph_handle->active_start, ceph_handle->active_end,
ceph_handle->nblocks_in_range, ceph_handle->nblocks_in_overflow);
return BUFFER_WRITE;
} else {
return BUFFER_CONTINUE;
}
} else if (offset >= ceph_handle->overflow_start) {
if (offset + nbytes - 1 <= ceph_handle->overflow_end) { // in OVERFLOW range
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "Overflow offset %lld\n", offset);
dest_buff = ceph_handle->overflow_buff;
int index = offset - ceph_handle->overflow_start; // dest_buff->offset;
memcpy(dest_buff->buffer+index, buffer, nbytes);
dest_buff->nbytes += nbytes;
++ceph_handle->nblocks_in_overflow;
if (dest_buff->nbytes == ceph_handle->rebuff_size) {
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR, "%s: OVERFLOW BUFFER FULL!! SHOULD NOT HAPPEN! -- Returning BUFFER_WRITE\n", __FUNCTION__);
return BUFFER_WRITE;
} else {
return BUFFER_CONTINUE;
}
} else { // Way past OVERFLOW end
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR,
"%s: Packet too early: Offset %lld + nbytes-1 %lld (%lld) > overflow_end of %lld \n",
__FUNCTION__, offset, (nbytes - 1), (offset + nbytes - 1), ceph_handle->overflow_end);
return BUFFER_OUT_OF_RANGE;
}
}
}
void flip_buffer(globus_l_gfs_ceph_handle_t * ceph_handle) {
assembly_t * temp_buff = ceph_handle->active_buff;
temp_buff->nbytes = 0; // We've just written data stored here
ceph_handle->active_start = ceph_handle->overflow_start;
ceph_handle->active_end = ceph_handle->overflow_end;
ceph_handle->overflow_start += ceph_handle->rebuff_size;
ceph_handle->overflow_end += ceph_handle->rebuff_size;
ceph_handle->active_buff = ceph_handle->overflow_buff; // So we can start with the too-early blocks we've received
ceph_handle->overflow_buff = temp_buff;
// globus_gfs_log_message(GLOBUS_GFS_LOG_ALL, "%s: ACTIVE BUFFER: active_start = %lld, active_end = %lld \n",
// __FUNCTION__, ceph_handle->active_start, ceph_handle->active_end);
//
// globus_gfs_log_message(GLOBUS_GFS_LOG_ALL, "%s: OVERFLOW BUFFER: overflow_start = %lld, overflow_end = %lld \n",
// __FUNCTION__, ceph_handle->overflow_start, ceph_handle->overflow_end);
assert(ceph_handle->active_end == ceph_handle->overflow_start-1);
ceph_handle->nblocks_in_range = ceph_handle->nblocks_in_overflow;
ceph_handle->nblocks_in_overflow = 0;
}
globus_byte_t *get_buffer(globus_l_gfs_ceph_handle_t * ceph_handle) {
return ceph_handle->active_buff->buffer;
}
globus_size_t get_nbytes(globus_l_gfs_ceph_handle_t * ceph_handle) {
return ceph_handle->active_buff->nbytes;
}
globus_off_t get_offset(globus_l_gfs_ceph_handle_t * ceph_handle) {
return ceph_handle->active_start;
}
/* 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";
static globus_size_t last_nbytes = 0;
static int reported_nbytes = 0;
if (!strcmp(getdebug(), "1")) {
// globus_gfs_log_message(GLOBUS_GFS_LOG_INFO,
// "%s\n\tEOF = %s, offset = %lld, nbytes = %d\n",
// __FUNCTION__, eof ? "TRUE" : "FALSE", offset, nbytes);
if (nbytes != last_nbytes) {
globus_gfs_log_message(GLOBUS_GFS_LOG_DUMP, "%s: nbytes: %d, was previously %d\n", __FUNCTION__, nbytes, last_nbytes);
last_nbytes = nbytes;
}
}
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) {