-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathmfu_flist_copy.c
3161 lines (2647 loc) · 105 KB
/
mfu_flist_copy.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
#define _GNU_SOURCE
#ifndef __G_MACROS_H__
#define __G_MACROS_H__
#endif
#include <dirent.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h> /* asctime / localtime */
#include <regex.h>
/* These headers are needed to query the Lustre MDS for stat
* information. This information may be incomplete, but it
* is faster than a normal stat, which requires communication
* with the MDS plus every OST a file is striped across. */
//#define LUSTRE_STAT
#ifdef LUSTRE_STAT
#include <lustre/lustre_user.h>
#endif /* LUSTRE_STAT */
#include <pwd.h> /* for getpwent */
#include <grp.h> /* for getgrent */
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <linux/fs.h>
#include <linux/fiemap.h>
/* define PRI64 */
#include <inttypes.h>
#include <libgen.h> /* dirname */
#include "libcircle.h"
#include "dtcmp.h"
#include "mfu.h"
#include "mfu_flist_internal.h"
#include "strmap.h"
#ifdef LUSTRE_SUPPORT
#include <lustre/lustre_user.h>
#include <sys/ioctl.h>
#endif
#ifdef GPFS_SUPPORT
#include <gpfs.h>
#endif
/****************************************
* Define types
***************************************/
typedef struct {
int64_t total_dirs; /* sum of all directories */
int64_t total_files; /* sum of all files */
int64_t total_links; /* sum of all symlinks */
int64_t total_size; /* sum of all file sizes */
int64_t total_bytes_copied; /* total bytes written */
time_t time_started; /* time when dcp command started */
time_t time_ended; /* time when dcp command ended */
double wtime_started; /* time when dcp command started */
double wtime_ended; /* time when dcp command ended */
} mfu_copy_stats_t;
/* cache open file descriptor to avoid
* opening / closing the same file */
typedef struct {
char* name; /* name of open file (NULL if none) */
int read; /* whether file is open for read-only (1) or write (0) */
int fd; /* file descriptor */
} mfu_copy_file_cache_t;
/****************************************
* Define globals
***************************************/
/** Where we should keep statistics related to this file copy. */
static mfu_copy_stats_t mfu_copy_stats;
/** Cache most recent open file descriptor to avoid opening / closing the same file */
static mfu_copy_file_cache_t mfu_copy_src_cache;
static mfu_copy_file_cache_t mfu_copy_dst_cache;
static int mfu_copy_open_file(const char* file, int read_flag,
mfu_copy_file_cache_t* cache, mfu_copy_opts_t* mfu_copy_opts)
{
int newfd = -1;
/* see if we have a cached file descriptor */
char* name = cache->name;
if (name != NULL) {
/* we have a cached file descriptor */
int fd = cache->fd;
if (strcmp(name, file) == 0 && cache->read == read_flag) {
/* the file we're trying to open matches name and read/write mode,
* so just return the cached descriptor */
return fd;
} else {
/* the file we're trying to open is different,
* close the old file and delete the name */
mfu_close(name, fd);
mfu_free(&cache->name);
}
}
/* open the new file */
if (read_flag) {
int flags = O_RDONLY;
if (mfu_copy_opts->synchronous) {
flags |= O_DIRECT;
}
newfd = mfu_open(file, flags);
} else {
int flags = O_WRONLY | O_CREAT;
if (mfu_copy_opts->synchronous) {
flags |= O_DIRECT;
}
newfd = mfu_open(file, flags, DCOPY_DEF_PERMS_FILE);
}
/* cache the file descriptor */
if (newfd != -1) {
cache->name = MFU_STRDUP(file);
cache->fd = newfd;
cache->read = read_flag;
#ifdef LUSTRE_SUPPORT
/* Zero is an invalid ID for grouplock. */
if (mfu_copy_opts->grouplock_id != 0) {
errno = 0;
int rc = ioctl(newfd, LL_IOC_GROUP_LOCK, mfu_copy_opts->grouplock_id);
if (rc) {
MFU_LOG(MFU_LOG_ERR, "Failed to obtain grouplock with ID %d "
"on file `%s', ignoring this error (errno=%d %s)",
mfu_copy_opts->grouplock_id, file, errno, strerror(errno));
} else {
MFU_LOG(MFU_LOG_INFO, "Obtained grouplock with ID %d "
"on file `%s', fd %d", mfu_copy_opts->grouplock_id,
file, newfd);
}
}
#endif
}
return newfd;
}
/* copy all extended attributes from op->operand to dest_path,
* returns 0 on success and -1 on failure */
static int mfu_copy_xattrs(
mfu_flist flist,
uint64_t idx,
const char* dest_path)
{
/* assume that we'll succeed */
int rc = 0;
#if DCOPY_USE_XATTRS
/* get source file name */
const char* src_path = mfu_flist_file_get_name(flist, idx);
/* start with a reasonable buffer, we'll allocate more as needed */
size_t list_bufsize = 1204;
char* list = (char*) MFU_MALLOC(list_bufsize);
/* get list, if list_size == ERANGE, try again */
ssize_t list_size;
int got_list = 0;
/* get current estimate for list size */
while(! got_list) {
errno = 0;
list_size = llistxattr(src_path, list, list_bufsize);
if(list_size < 0) {
if(errno == ERANGE) {
/* buffer is too small, free our current buffer
* and call it again with size==0 to get new size */
mfu_free(&list);
list_bufsize = 0;
}
else if(errno == ENOTSUP) {
/* this is common enough that we silently ignore it */
break;
}
else {
/* this is a real error */
MFU_LOG(MFU_LOG_ERR, "Failed to get list of extended attributes on `%s' llistxattr() (errno=%d %s)",
src_path, errno, strerror(errno)
);
rc = -1;
break;
}
}
else {
if(list_size > 0 && list_bufsize == 0) {
/* called llistxattr with size==0 and got back positive
* number indicating size of buffer we need to allocate */
list_bufsize = (size_t) list_size;
list = (char*) MFU_MALLOC(list_bufsize);
}
else {
/* got our list, it's size is in list_size, which may be 0 */
got_list = 1;
}
}
}
/* iterate over list and copy values to new object lgetxattr/lsetxattr */
if(got_list) {
char* name = list;
while(name < list + list_size) {
/* start with a reasonable buffer,
* allocate something bigger as needed */
size_t val_bufsize = 1024;
void* val = (void*) MFU_MALLOC(val_bufsize);
/* lookup value for name */
ssize_t val_size;
int got_val = 0;
while(! got_val) {
errno = 0;
val_size = lgetxattr(src_path, name, val, val_bufsize);
if(val_size < 0) {
if(errno == ERANGE) {
/* buffer is too small, free our current buffer
* and call it again with size==0 to get new size */
mfu_free(&val);
val_bufsize = 0;
}
else if(errno == ENOATTR) {
/* source object no longer has this attribute,
* maybe deleted out from under us, ignore but print warning */
MFU_LOG(MFU_LOG_WARN, "Attribute does not exist for name=%s on `%s' llistxattr() (errno=%d %s)",
name, src_path, errno, strerror(errno)
);
break;
}
else {
/* this is a real error */
MFU_LOG(MFU_LOG_ERR, "Failed to get value for name=%s on `%s' llistxattr() (errno=%d %s)",
name, src_path, errno, strerror(errno)
);
rc = -1;
break;
}
}
else {
if(val_size > 0 && val_bufsize == 0) {
/* called lgetxattr with size==0 and got back positive
* number indicating size of buffer we need to allocate */
val_bufsize = (size_t) val_size;
val = (void*) MFU_MALLOC(val_bufsize);
}
else {
/* got our value, it's size is in val_size, which may be 0 */
got_val = 1;
}
}
}
/* set attribute on destination object */
if(got_val) {
errno = 0;
int setrc = lsetxattr(dest_path, name, val, (size_t) val_size, 0);
if(setrc != 0) {
/* failed to set attribute */
MFU_LOG(MFU_LOG_ERR, "Failed to set value for name=%s on `%s' llistxattr() (errno=%d %s)",
name, dest_path, errno, strerror(errno)
);
rc = -1;
}
}
/* free value string */
mfu_free(&val);
val_bufsize = 0;
/* jump to next name */
size_t namelen = strlen(name) + 1;
name += namelen;
}
}
/* free space allocated for list */
mfu_free(&list);
list_bufsize = 0;
#endif /* DCOPY_USE_XATTR */
return rc;
}
static int mfu_copy_ownership(
mfu_flist flist,
uint64_t idx,
const char* dest_path)
{
/* assume we'll succeed */
int rc = 0;
/* get user id and group id of file */
uid_t uid = (uid_t) mfu_flist_file_get_uid(flist, idx);
gid_t gid = (gid_t) mfu_flist_file_get_gid(flist, idx);
/* note that we use lchown to change ownership of link itself, it path happens to be a link */
if(mfu_lchown(dest_path, uid, gid) != 0) {
/* TODO: are there other EPERM conditions we do want to report? */
/* since the user running dcp may not be the owner of the
* file, we could hit an EPERM error here, and the file
* will be left with the effective uid and gid of the dcp
* process, don't bother reporting an error for that case */
if (errno != EPERM) {
MFU_LOG(MFU_LOG_ERR, "Failed to change ownership on `%s' lchown() (errno=%d %s)",
dest_path, errno, strerror(errno)
);
}
rc = -1;
}
return rc;
}
/* TODO: condionally set setuid and setgid bits? */
static int mfu_copy_permissions(
mfu_flist flist,
uint64_t idx,
const char* dest_path)
{
/* assume we'll succeed */
int rc = 0;
/* get mode and type */
mfu_filetype type = mfu_flist_file_get_type(flist, idx);
mode_t mode = (mode_t) mfu_flist_file_get_mode(flist, idx);
/* change mode */
if(type != MFU_TYPE_LINK) {
if(mfu_chmod(dest_path, mode) != 0) {
MFU_LOG(MFU_LOG_ERR, "Failed to change permissions on `%s' chmod() (errno=%d %s)",
dest_path, errno, strerror(errno)
);
rc = -1;
}
}
return rc;
}
/* copy GPFS ACLs from source to destination */
static int mfu_copy_acls(
mfu_flist flist,
uint64_t idx,
const char* dest_path)
{
/* assume we'll succeed */
int rc = 0;
#ifdef GPFS_SUPPORT
/* get type */
mfu_filetype type = mfu_flist_file_get_type(flist, idx);
/* copy ACLs unless item is a link */
if(type != MFU_TYPE_LINK) {
/* if we have GPFS support enabled, then we'll use the GPFS API to read
* the ACL from the src_path and write to the dest_path.
* We use the opaque method as we are not trying to alter the ACL contents.
* Note that if the source is not a GPFS file-system, then the call will
* fail with EINVAL and so we never try to apply this to the dest_path */
/* need the file path to read the existing ACL */
const char* src_path = mfu_flist_file_get_name(flist, idx);
/* acl param mapped with gpfs_opaque_acl_t structure */
int aclflags = 0;
unsigned char acltype = GPFS_ACL_TYPE_ACCESS;
/* initial size of the struct for gpfs_opaque_acl, 512 bytes should
* be large enough for a fairly large ACL anyway */
size_t bufsize = 512;
/* gpfs_getacl needs a *void for where it will place the data, so we need
* to allocate some memory and then place a gpfs_opaque_acl into the
* memory as aclflags is set to 0 to indicate gpfs_opaque_acl_t */
void* aclbufmem = MFU_MALLOC(bufsize);
memset(aclbufmem, 0, bufsize);
/* set fields in structure to define acl query */
struct gpfs_opaque_acl* aclbuffer = (struct gpfs_opaque_acl*) aclbufmem;
aclbuffer->acl_buffer_len = (int) (bufsize - sizeof(struct gpfs_opaque_acl));
aclbuffer->acl_type = acltype;
/* try and get the ACL */
errno = 0;
int r = gpfs_getacl(src_path, aclflags, aclbufmem);
/* the buffer may not be big enough, if not, we'll get EONSPC and
* the first 4 bytes (acl_buffer_len) will tell us how much space we need */
if ((r != 0) && (errno == ENOSPC)) {
/* make a buffer which is exactly the right size */
unsigned int len = *(unsigned int*) &(aclbuffer->acl_buffer_len);
bufsize = len + sizeof(struct gpfs_opaque_acl);
MFU_LOG(MFU_LOG_DBG, "GPFS ACL buffer too small, needs to be %d",
(int) bufsize);
/* free the old buffer, then malloc the new size */
mfu_free(&aclbufmem);
aclbufmem = MFU_MALLOC(bufsize);
memset(aclbufmem, 0, bufsize);
/* set fields in structure to define acl query */
aclbuffer = (struct gpfs_opaque_acl*) aclbufmem;
aclbuffer->acl_buffer_len = (int) (bufsize - sizeof(struct gpfs_opaque_acl));
aclbuffer->acl_type = acltype;
/* once again try and get the ACL */
r = gpfs_getacl(src_path, aclflags, aclbufmem);
}
/* check whether we read the ACL successfully */
if (r == 0) {
/* Assuming we now have a valid call to an ACL,
* try to place it on dest_path */
errno = 0;
r = gpfs_putacl(dest_path, aclflags, aclbufmem);
if (r != 0 && errno != EINVAL) {
/* failed to put GPFS ACL, print message unless target is not a GPFS file system */
MFU_LOG(MFU_LOG_ERR, "Failed to copy GPFS ACL from %s to %s errno=%d (%s)",
src_path, dest_path, errno, strerror(errno));
rc = -1;
}
} else {
/* failed to get GPFS ACL, print message unless source is not a GPFS file system */
if (errno != EINVAL) {
MFU_LOG(MFU_LOG_ERR, "Failed to get GPFS ACL on %s errno=%d (%s)",
src_path, errno, strerror(errno));
rc = -1;
}
}
/* free the memory from the buffer */
mfu_free(&aclbufmem);
}
#endif /* GPFS_SUPPORT */
return rc;
}
static int mfu_copy_timestamps(
mfu_flist flist,
uint64_t idx,
const char* dest_path)
{
/* assume we'll succeed */
int rc = 0;
/* get atime seconds and nsecs */
uint64_t atime = mfu_flist_file_get_atime(flist, idx);
uint64_t atime_nsec = mfu_flist_file_get_atime_nsec(flist, idx);
/* get mtime seconds and nsecs */
uint64_t mtime = mfu_flist_file_get_mtime(flist, idx);
uint64_t mtime_nsec = mfu_flist_file_get_mtime_nsec(flist, idx);
/* fill in time structures */
struct timespec times[2];
times[0].tv_sec = (time_t) atime;
times[0].tv_nsec = (long) atime_nsec;
times[1].tv_sec = (time_t) mtime;
times[1].tv_nsec = (long) mtime_nsec;
/* set times with nanosecond precision using utimensat,
* assume path is relative to current working directory,
* if it's not absolute, and set times on link (not target file)
* if dest_path refers to a link */
if(mfu_utimensat(AT_FDCWD, dest_path, times, AT_SYMLINK_NOFOLLOW) != 0) {
MFU_LOG(MFU_LOG_ERR, "Failed to change timestamps on `%s' utime() (errno=%d %s)",
dest_path, errno, strerror(errno)
);
rc = -1;
}
return rc;
}
static int mfu_copy_close_file(mfu_copy_file_cache_t* cache)
{
int rc = 0;
/* close file if we have one */
char* name = cache->name;
if (name != NULL) {
int fd = cache->fd;
/* if open for write, fsync */
int read_flag = cache->read;
if (! read_flag) {
rc = mfu_fsync(name, fd);
}
/* close the file and delete the name string */
rc = mfu_close(name, fd);
mfu_free(&cache->name);
}
return rc;
}
/* progress message to print while setting file metadata */
static void meta_progress_fn(const uint64_t* vals, int count, int complete, int ranks, double secs)
{
#if 0
/* compute percentage of items removed */
double percent = 0.0;
if (remove_count_total > 0) {
percent = 100.0 * (double)vals[0] / (double)remove_count_total;
}
#endif
/* compute average delete rate */
double rate = 0.0;
if (secs > 0) {
rate = (double)vals[0] / secs;
}
#if 0
/* compute estimated time remaining */
double secs_remaining = -1.0;
if (rate > 0.0) {
secs_remaining = (double)(remove_count_total - vals[0]) / rate;
}
#endif
if (complete < ranks) {
MFU_LOG(MFU_LOG_INFO, "Updated %llu items in %f secs (%f items/sec) ...",
vals[0], secs, rate);
} else {
MFU_LOG(MFU_LOG_INFO, "Updated %llu items in %f secs (%f items/sec) done",
vals[0], secs, rate);
}
}
/* iterate through list of files and set ownership, timestamps,
* and permissions starting from deepest level and working upwards,
* we go in this direction in case updating a file updates its
* parent directory */
static int mfu_copy_set_metadata(int levels, int minlevel, mfu_flist* lists,
int numpaths, const mfu_param_path* paths,
const mfu_param_path* destpath, mfu_copy_opts_t* mfu_copy_opts)
{
/* assume we'll succeed */
int rc = 0;
/* determine whether we should print status messages */
int verbose = (mfu_debug_level >= MFU_LOG_VERBOSE);
/* get current rank */
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
if(mfu_copy_opts->preserve) {
MFU_LOG(MFU_LOG_INFO, "Setting ownership, permissions, and timestamps.");
}
else {
MFU_LOG(MFU_LOG_INFO, "Fixing permissions.");
}
}
/* start timer for entie operation */
MPI_Barrier(MPI_COMM_WORLD);
double total_start = MPI_Wtime();
uint64_t total_count = 0;
/* start progress messages while setting metadata */
mfu_progress* meta_prog = mfu_progress_start(mfu_progress_timeout, 1, MPI_COMM_WORLD, meta_progress_fn);
/* now set timestamps on files starting from deepest level */
int tmp_rc;
int level;
for (level = levels-1; level >= 0; level--) {
/* get list at this level */
mfu_flist list = lists[level];
/* cycle through our list of items and set timestamps
* for each one at this level */
uint64_t idx;
uint64_t size = mfu_flist_size(list);
for (idx = 0; idx < size; idx++) {
/* TODO: skip file if it's not readable */
/* get source name of item */
const char* name = mfu_flist_file_get_name(list, idx);
/* get destination name of item */
char* dest = mfu_param_path_copy_dest(name, numpaths,
paths, destpath, mfu_copy_opts);
/* No need to copy it */
if (dest == NULL) {
continue;
}
/* update our running total */
total_count++;
if(mfu_copy_opts->preserve) {
tmp_rc = mfu_copy_ownership(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
tmp_rc = mfu_copy_permissions(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
tmp_rc = mfu_copy_acls(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
tmp_rc = mfu_copy_timestamps(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
}
else {
/* TODO: set permissions based on source permissons
* masked by umask */
tmp_rc = mfu_copy_permissions(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
}
/* free destination item */
mfu_free(&dest);
/* update number of items we have completed for progress messages */
mfu_progress_update(&total_count, meta_prog);
}
/* wait for all procs to finish before we start
* with files at next level */
MPI_Barrier(MPI_COMM_WORLD);
}
/* finalize progress messages */
mfu_progress_complete(&total_count, &meta_prog);
/* stop timer and report total count */
MPI_Barrier(MPI_COMM_WORLD);
double total_end = MPI_Wtime();
/* print timing statistics */
if (verbose) {
uint64_t sum;
MPI_Allreduce(&total_count, &sum, 1, MPI_UINT64_T, MPI_SUM, MPI_COMM_WORLD);
double rate = 0.0;
double secs = total_end - total_start;
if (secs > 0.0) {
rate = (double)sum / secs;
}
if (rank == 0) {
MFU_LOG(MFU_LOG_INFO, "Updated %lu items in %f seconds (%f items/sec)",
(unsigned long)sum, secs, rate
);
}
}
return rc;
}
/* iterate through list of files and set ownership, timestamps,
* and permissions starting from deepest level and working upwards,
* we go in this direction in case updating a file updates its
* parent directory */
static int mfu_copy_set_metadata_dirs(int levels, int minlevel, mfu_flist* lists,
int numpaths, const mfu_param_path* paths,
const mfu_param_path* destpath, mfu_copy_opts_t* mfu_copy_opts)
{
/* assume we'll succeed */
int rc = 0;
/* determine whether we should print status messages */
int verbose = (mfu_debug_level >= MFU_LOG_VERBOSE);
/* get current rank */
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
if(mfu_copy_opts->preserve) {
MFU_LOG(MFU_LOG_INFO, "Setting ownership, permissions, and timestamps on directories.");
}
else {
MFU_LOG(MFU_LOG_INFO, "Fixing permissions on directories.");
}
}
/* start timer for entie operation */
MPI_Barrier(MPI_COMM_WORLD);
double total_start = MPI_Wtime();
uint64_t total_count = 0;
/* now set timestamps on files starting from deepest level */
int tmp_rc;
int level;
for (level = levels-1; level >= 0; level--) {
/* get list at this level */
mfu_flist list = lists[level];
/* cycle through our list of items and set timestamps
* for each one at this level */
uint64_t idx;
uint64_t size = mfu_flist_size(list);
for (idx = 0; idx < size; idx++) {
/* TODO: skip file if it's not readable */
/* get source name of item */
const char* name = mfu_flist_file_get_name(list, idx);
/* get destination name of item */
char* dest = mfu_param_path_copy_dest(name, numpaths,
paths, destpath, mfu_copy_opts);
/* No need to copy it */
if (dest == NULL) {
continue;
}
/* only need to set metadata on directories */
mfu_filetype type = mfu_flist_file_get_type(list, idx);
if (type != MFU_TYPE_DIR) {
continue;
}
/* update our running total */
total_count++;
if(mfu_copy_opts->preserve) {
tmp_rc = mfu_copy_ownership(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
tmp_rc = mfu_copy_permissions(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
tmp_rc = mfu_copy_acls(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
tmp_rc = mfu_copy_timestamps(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
}
else {
/* TODO: set permissions based on source permissons
* masked by umask */
tmp_rc = mfu_copy_permissions(list, idx, dest);
if (tmp_rc < 0) {
rc = -1;
}
}
/* free destination item */
mfu_free(&dest);
}
/* wait for all procs to finish before we start
* with files at next level */
MPI_Barrier(MPI_COMM_WORLD);
}
/* stop timer and report total count */
MPI_Barrier(MPI_COMM_WORLD);
double total_end = MPI_Wtime();
/* print timing statistics */
if (verbose) {
uint64_t sum;
MPI_Allreduce(&total_count, &sum, 1, MPI_UINT64_T, MPI_SUM, MPI_COMM_WORLD);
double rate = 0.0;
double secs = total_end - total_start;
if (secs > 0.0) {
rate = (double)sum / secs;
}
if (rank == 0) {
MFU_LOG(MFU_LOG_INFO, "Updated %lu items in %f seconds (%f items/sec)",
(unsigned long)sum, secs, rate
);
}
}
return rc;
}
/* creates dir in destpath for specified item, identifies source path
* that contains source dir, computes relative path to dir under source path,
* and creates dir at same relative path under destpath, copies xattrs
* when preserving permissions, which contains file striping info on Lustre,
* returns 0 on success and -1 on error */
static int mfu_create_directory(mfu_flist list, uint64_t idx,
int numpaths, const mfu_param_path* paths,
const mfu_param_path* destpath, mfu_copy_opts_t* mfu_copy_opts)
{
/* assume we'll succeed */
int rc = 0;
/* get name of directory */
const char* name = mfu_flist_file_get_name(list, idx);
/* get destination name */
char* dest_path = mfu_param_path_copy_dest(name, numpaths, paths,
destpath, mfu_copy_opts);
/* No need to copy it */
if (dest_path == NULL) {
return 0;
}
/* Skipping the destination directory ONLY if it already exists.
* If we are doing a sync operation and if the dest dir does not
* exist, we need to create it. The reason that
* the dest_path and the destpath->path are compared is because
* if we are syncing two directories we want the tree to have the
* same number of levels. If dsync is on then only the contents of
* the top level source directory will be copied (if necessary) into
* the target directory. So, the top level src directory is removed
* from the destination path. This path slicing based on whether or
* not dsync is on happens prior to this in
* mfu_param_path_copy_dest. */
if (mfu_copy_opts->do_sync &&
(strncmp(dest_path, destpath->path, strlen(dest_path)) == 0) &&
destpath->target_stat_valid)
{
mfu_free(&dest_path);
return 0;
}
/* create the destination directory */
MFU_LOG(MFU_LOG_DBG, "Creating directory `%s'", dest_path);
int mkdir_rc = mfu_mkdir(dest_path, DCOPY_DEF_PERMS_DIR);
if(mkdir_rc < 0) {
if(errno == EEXIST) {
MFU_LOG(MFU_LOG_WARN,
"Original directory exists, skip the creation: `%s' (errno=%d %s)",
dest_path, errno, strerror(errno));
} else {
MFU_LOG(MFU_LOG_ERR, "Create `%s' mkdir() failed (errno=%d %s)",
dest_path, errno, strerror(errno)
);
mfu_free(&dest_path);
return -1;
}
}
/* we do this now in case there are Lustre attributes for
* creating / striping files in the directory */
/* copy extended attributes on directory */
if (mfu_copy_opts->preserve) {
int tmp_rc = mfu_copy_xattrs(list, idx, dest_path);
if (tmp_rc < 0) {
rc = -1;
}
}
/* increment our directory count by one */
mfu_copy_stats.total_dirs++;
/* free the directory name */
mfu_free(&dest_path);
return rc;
}
/* create directories, we work from shallowest level to the deepest
* with a barrier in between levels, so that we don't try to create
* a child directory until the parent exists,
* returns 0 on success and -1 on failure */
static int mfu_create_directories(int levels, int minlevel, mfu_flist* lists,
int numpaths, const mfu_param_path* paths,
const mfu_param_path* destpath, mfu_copy_opts_t* mfu_copy_opts)
{
/* assume we'll succeed */
int rc = 0;
/* determine whether we should print status messages */
int verbose = (mfu_debug_level >= MFU_LOG_VERBOSE);
/* get current rank */
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* indicate to user what phase we're in */
if (rank == 0) {
MFU_LOG(MFU_LOG_INFO, "Creating directories.");
}
/* start timer for entie operation */
MPI_Barrier(MPI_COMM_WORLD);
double total_start = MPI_Wtime();
uint64_t total_count = 0;
/* work from shallowest level to deepest level */
int level;
for (level = 0; level < levels; level++) {
/* time how long this takes */
double start = MPI_Wtime();
/* get list of items for this level */
mfu_flist list = lists[level];
/* create each directory we have at this level */
uint64_t idx;
uint64_t size = mfu_flist_size(list);
uint64_t count = 0;
for (idx = 0; idx < size; idx++) {
/* check whether we have a directory */
mfu_filetype type = mfu_flist_file_get_type(list, idx);
if (type == MFU_TYPE_DIR) {
/* create the directory */
int tmp_rc = mfu_create_directory(list, idx, numpaths,
paths, destpath, mfu_copy_opts);
if (tmp_rc < 0) {
rc = -1;
}
count++;
}
}
/* add items to our running total */
total_count += count;
/* wait for all procs to finish before we start
* creating directories at next level */
MPI_Barrier(MPI_COMM_WORLD);
/* stop our timer */
double end = MPI_Wtime();
/* print statistics */
if (verbose) {
uint64_t min, max, sum;
MPI_Allreduce(&count, &min, 1, MPI_UINT64_T, MPI_MIN, MPI_COMM_WORLD);
MPI_Allreduce(&count, &max, 1, MPI_UINT64_T, MPI_MAX, MPI_COMM_WORLD);
MPI_Allreduce(&count, &sum, 1, MPI_UINT64_T, MPI_SUM, MPI_COMM_WORLD);
double rate = 0.0;
double secs = end - start;
if (secs > 0.0) {
rate = (double)sum / secs;
}
if (rank == 0) {
MFU_LOG(MFU_LOG_INFO, " level=%d min=%lu max=%lu sum=%lu rate=%f/sec secs=%f",
(minlevel + level), (unsigned long)min, (unsigned long)max, (unsigned long)sum, rate, secs
);
}
}
}
/* stop timer and report total count */
MPI_Barrier(MPI_COMM_WORLD);
double total_end = MPI_Wtime();
/* print timing statistics */
if (verbose) {
uint64_t sum;
MPI_Allreduce(&total_count, &sum, 1, MPI_UINT64_T, MPI_SUM, MPI_COMM_WORLD);
double rate = 0.0;
double secs = total_end - total_start;
if (secs > 0.0) {
rate = (double)sum / secs;
}
if (rank == 0) {
MFU_LOG(MFU_LOG_INFO, "Created %lu directories in %f seconds (%f items/sec)",
(unsigned long)sum, secs, rate
);
}
}
return rc;
}