4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.htm
23 * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
26 * Copyright (c) 2013, 2014, Intel Corporation.
28 /* HSM copytool program for POSIX filesystem-based HSM's.
30 * An HSM copytool daemon acts on action requests from Lustre to copy files
31 * to and from an HSM archive system. This one in particular makes regular
32 * POSIX filesystem calls to a given path, where an HSM is presumably mounted.
34 * This particular tool can also import an existing HSM archive.
51 #include <sys/xattr.h>
52 #include <sys/syscall.h>
53 #include <sys/types.h>
54 #include <lustre/lustre_idl.h>
55 #include <lustre/lustreapi.h>
57 /* Progress reporting period */
58 #define REPORT_INTERVAL_DEFAULT 30
59 /* HSM hash subdir permissions */
60 #define DIR_PERM S_IRWXU
61 /* HSM hash file permissions */
62 #define FILE_PERM (S_IRUSR | S_IWUSR)
64 #define ONE_MB 0x100000
67 # define NSEC_PER_SEC 1000000000UL
85 int o_archive_id[LL_HSM_MAX_ARCHIVE];
87 unsigned long long o_bandwidth;
89 enum ct_action o_action;
94 char *o_src; /* for import, or rebind */
95 char *o_dst; /* for import, or rebind */
98 /* everything else is zeroed */
99 struct options opt = {
102 .o_verbose = LLAPI_MSG_INFO,
104 .o_report_int = REPORT_INTERVAL_DEFAULT,
105 .o_chunk_size = ONE_MB,
108 /* hsm_copytool_private will hold an open FD on the lustre mount point
109 * for us. Additionally open one on the archive FS root to make sure
110 * it doesn't drop out from under us (and remind the admin to shutdown
111 * the copytool before unmounting). */
113 static int arc_fd = -1;
115 static int err_major;
116 static int err_minor;
118 static char cmd_name[PATH_MAX];
119 static char fs_name[MAX_OBD_NAME + 1];
121 static struct hsm_copytool_private *ctdata;
123 static inline double ct_now(void)
127 gettimeofday(&tv, NULL);
129 return tv.tv_sec + 0.000001 * tv.tv_usec;
132 #define CT_ERROR(_rc, _format, ...) \
133 llapi_error(LLAPI_MSG_ERROR, _rc, \
134 "%f %s[%ld]: "_format, \
135 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
137 #define CT_DEBUG(_format, ...) \
138 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
139 "%f %s[%ld]: "_format, \
140 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
142 #define CT_WARN(_format, ...) \
143 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
144 "%f %s[%ld]: "_format, \
145 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
147 #define CT_TRACE(_format, ...) \
148 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
149 "%f %s[%ld]: "_format, \
150 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
152 static void usage(const char *name, int rc)
155 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
156 "The Lustre HSM Posix copy tool can be used as a daemon or "
157 "as a command line tool\n"
158 "The Lustre HSM daemon acts on action requests from Lustre\n"
159 "to copy files to and from an HSM archive system.\n"
160 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
161 "to an HSM mounted at a given hsm_root.\n"
162 " --daemon Daemon mode, run in background\n"
164 " --no-attr Don't copy file attributes\n"
165 " --no-shadow Don't create shadow namespace in archive\n"
166 " --no-xattr Don't copy file extended attributes\n"
167 "The Lustre HSM tool performs administrator-type actions\n"
168 "on a Lustre HSM archive.\n"
169 "This POSIX-flavored tool can link an existing HSM namespace\n"
170 "into a Lustre filesystem.\n"
172 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
173 " import an archived subtree from\n"
174 " <src> (FID or relative path to hsm_root) into the Lustre\n"
176 " <dst> (absolute path)\n"
177 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
178 " rebind an entry in the HSM to a new FID\n"
179 " <old_FID> old FID the HSM entry is bound to\n"
180 " <new_FID> new FID to bind the HSM entry to\n"
181 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
182 " perform the rebind operation for all FID in the list file\n"
183 " each line of <list_file> consists of <old_FID> <new_FID>\n"
184 " %s [options] --max-sequence <fsname>\n"
185 " return the max fid sequence of archived files\n"
186 " --abort-on-error Abort operation on major error\n"
187 " -A, --archive <#> Archive number (repeatable)\n"
188 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
190 " --dry-run Don't run, just show what would be done\n"
191 " -c, --chunk-size <sz> I/O size used during data copy\n"
192 " (unit can be used, default is MB)\n"
193 " -f, --event-fifo <path> Write events stream to fifo\n"
194 " -p, --hsm-root <path> Target HSM mount point\n"
195 " -q, --quiet Produce less verbose output\n"
196 " -u, --update-interval <s> Interval between progress reports sent\n"
198 " -v, --verbose Produce more verbose output\n",
199 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
204 static int ct_parseopts(int argc, char * const *argv)
206 struct option long_opts[] = {
207 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
208 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
209 {"archive", required_argument, NULL, 'A'},
210 {"bandwidth", required_argument, NULL, 'b'},
211 {"chunk-size", required_argument, NULL, 'c'},
212 {"chunk_size", required_argument, NULL, 'c'},
213 {"daemon", no_argument, &opt.o_daemonize, 1},
214 {"event-fifo", required_argument, NULL, 'f'},
215 {"event_fifo", required_argument, NULL, 'f'},
216 {"dry-run", no_argument, &opt.o_dry_run, 1},
217 {"help", no_argument, NULL, 'h'},
218 {"hsm-root", required_argument, NULL, 'p'},
219 {"hsm_root", required_argument, NULL, 'p'},
220 {"import", no_argument, NULL, 'i'},
221 {"max-sequence", no_argument, NULL, 'M'},
222 {"max_sequence", no_argument, NULL, 'M'},
223 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
224 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
225 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
226 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
227 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
228 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
229 {"quiet", no_argument, NULL, 'q'},
230 {"rebind", no_argument, NULL, 'r'},
231 {"update-interval", required_argument, NULL, 'u'},
232 {"update_interval", required_argument, NULL, 'u'},
233 {"verbose", no_argument, NULL, 'v'},
237 unsigned long long value;
238 unsigned long long unit;
241 while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
242 long_opts, NULL)) != -1) {
245 if ((opt.o_archive_cnt >= LL_HSM_MAX_ARCHIVE) ||
246 (atoi(optarg) >= LL_HSM_MAX_ARCHIVE)) {
248 CT_ERROR(rc, "archive number must be less"
249 "than %zu", LL_HSM_MAX_ARCHIVE);
252 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
255 case 'b': /* -b and -c have both a number with unit as arg */
258 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
260 CT_ERROR(rc, "bad value for -%c '%s'", c,
265 opt.o_chunk_size = value;
267 opt.o_bandwidth = value;
270 opt.o_event_fifo = optarg;
275 opt.o_action = CA_IMPORT;
278 opt.o_action = CA_MAXSEQ;
281 opt.o_hsm_root = optarg;
287 opt.o_action = CA_REBIND;
290 opt.o_report_int = atoi(optarg);
291 if (opt.o_report_int < 0) {
293 CT_ERROR(rc, "bad value for -%c '%s'", c,
308 switch (opt.o_action) {
310 /* src dst mount_point */
311 if (argc != optind + 3) {
313 CT_ERROR(rc, "--import requires 2 arguments");
316 opt.o_src = argv[optind++];
317 opt.o_dst = argv[optind++];
320 /* FID1 FID2 mount_point or FILE mount_point */
321 if (argc == optind + 2) {
322 opt.o_src = argv[optind++];
324 } else if (argc == optind + 3) {
325 opt.o_src = argv[optind++];
326 opt.o_dst = argv[optind++];
329 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
335 /* just mount point */
339 if (argc != optind + 1) {
341 CT_ERROR(rc, "no mount point specified");
345 opt.o_mnt = argv[optind];
348 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
349 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
351 if (opt.o_hsm_root == NULL) {
353 CT_ERROR(rc, "must specify a root directory for the backend");
357 if (opt.o_action == CA_IMPORT) {
358 if (opt.o_src && opt.o_src[0] == '/') {
361 "source path must be relative to HSM root");
365 if (opt.o_dst && opt.o_dst[0] != '/') {
367 CT_ERROR(rc, "destination path must be absolute");
376 static int ct_mkdir_p(const char *path)
389 while ((ptr = strchr(ptr, '/')) != NULL) {
391 rc = mkdir(saved, DIR_PERM);
393 if (rc < 0 && errno != EEXIST) {
395 CT_ERROR(rc, "cannot mkdir '%s'", path);
407 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
409 char lov_file[PATH_MAX];
410 char lov_buf[XATTR_SIZE_MAX];
411 struct lov_user_md *lum;
416 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
417 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
419 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
421 if (xattr_size < 0) {
423 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
427 lum = (struct lov_user_md *)lov_buf;
429 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
430 lum->lmm_magic == LOV_USER_MAGIC_V3) {
431 /* Set stripe_offset to -1 so that it is not interpreted as a
432 * hint on restore. */
433 lum->lmm_stripe_offset = -1;
436 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
439 CT_ERROR(rc, "cannot open '%s'", lov_file);
443 rc = write(fd, lum, xattr_size);
446 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
447 xattr_size, lov_file);
455 CT_ERROR(rc, "cannot close '%s'", lov_file);
467 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
469 char lov_file[PATH_MAX];
473 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
474 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
476 fd = open(lov_file, O_RDONLY);
478 CT_ERROR(errno, "cannot open '%s'", lov_file);
482 rc = read(fd, lovea, *lovea_size);
484 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
485 *lovea_size, lov_file);
496 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
497 const void *lovea, size_t lovea_size)
501 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
505 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
511 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
512 const char *dst, int src_fd, int dst_fd,
513 const struct hsm_action_item *hai, long hal_flags)
515 struct hsm_extent he;
516 __u64 offset = hai->hai_extent.offset;
520 __u64 write_total = 0;
522 time_t last_report_time;
524 double start_ct_now = ct_now();
525 /* Bandwidth Control */
528 time_t last_bw_print;
530 if (fstat(src_fd, &src_st) < 0) {
532 CT_ERROR(rc, "cannot stat '%s'", src);
536 if (!S_ISREG(src_st.st_mode)) {
538 CT_ERROR(rc, "'%s' is not a regular file", src);
542 if (hai->hai_extent.offset > (__u64)src_st.st_size) {
544 CT_ERROR(rc, "Trying to start reading past end ("LPU64" > "
545 "%jd) of '%s' source file", hai->hai_extent.offset,
546 (intmax_t)src_st.st_size, src);
550 if (fstat(dst_fd, &dst_st) < 0) {
552 CT_ERROR(rc, "cannot stat '%s'", dst);
556 if (!S_ISREG(dst_st.st_mode)) {
558 CT_ERROR(rc, "'%s' is not a regular file", dst);
562 /* Don't read beyond a given extent */
563 length = min(hai->hai_extent.length,
564 src_st.st_size - hai->hai_extent.offset);
566 start_time = last_bw_print = last_report_time = time(NULL);
570 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
572 /* Action has been canceled or something wrong
573 * is happening. Stop copying data. */
574 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
581 buf = malloc(opt.o_chunk_size);
587 CT_TRACE("start copy of "LPU64" bytes from '%s' to '%s'",
590 while (write_total < length) {
593 int chunk = (length - write_total > opt.o_chunk_size) ?
594 opt.o_chunk_size : length - write_total;
596 rsize = pread(src_fd, buf, chunk, offset);
603 CT_ERROR(rc, "cannot read from '%s'", src);
607 wsize = pwrite(dst_fd, buf, rsize, offset);
610 CT_ERROR(rc, "cannot write to '%s'", dst);
614 write_total += wsize;
618 /* sleep if needed, to honor bandwidth limits */
619 if (opt.o_bandwidth != 0) {
620 unsigned long long write_theory;
622 write_theory = (now - start_time) * opt.o_bandwidth;
624 if (write_theory < write_total) {
625 unsigned long long excess;
626 struct timespec delay;
628 excess = write_total - write_theory;
630 delay.tv_sec = excess / opt.o_bandwidth;
631 delay.tv_nsec = (excess % opt.o_bandwidth) *
632 NSEC_PER_SEC / opt.o_bandwidth;
634 if (now >= last_bw_print + opt.o_report_int) {
635 CT_TRACE("bandwith control: %lluB/s "
636 "excess=%llu sleep for "
638 opt.o_bandwidth, excess,
639 (long long)delay.tv_sec,
645 rc = nanosleep(&delay, &delay);
646 } while (rc < 0 && errno == EINTR);
648 CT_ERROR(errno, "delay for bandwidth "
649 "control failed to sleep: "
650 "residual=%lld.%09lds",
651 (long long)delay.tv_sec,
659 if (now >= last_report_time + opt.o_report_int) {
660 last_report_time = now;
661 CT_TRACE("%%"LPU64" ", 100 * write_total / length);
662 he.length = write_total;
663 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
665 /* Action has been canceled or something wrong
666 * is happening. Stop copying data. */
667 CT_ERROR(rc, "progress ioctl for copy"
668 " '%s'->'%s' failed", src, dst);
677 * truncate restored file
678 * size is taken from the archive this is done to support
679 * restore after a force release which leaves the file with the
680 * wrong size (can big bigger than the new size)
682 if ((hai->hai_action == HSMA_RESTORE) &&
683 (src_st.st_size < dst_st.st_size)) {
685 * make sure the file is on disk before reporting success.
687 rc = ftruncate(dst_fd, src_st.st_size);
690 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
691 dst, (intmax_t)src_st.st_size);
699 CT_TRACE("copied "LPU64" bytes in %f seconds",
700 length, ct_now() - start_ct_now);
705 /* Copy file attributes from file src to file dest */
706 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
710 struct timeval times[2];
713 if (fstat(src_fd, &st) < 0) {
715 CT_ERROR(rc, "cannot stat '%s'", src);
719 times[0].tv_sec = st.st_atime;
720 times[0].tv_usec = 0;
721 times[1].tv_sec = st.st_mtime;
722 times[1].tv_usec = 0;
723 if (fchmod(dst_fd, st.st_mode) < 0 ||
724 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
725 futimes(dst_fd, times) < 0) {
727 CT_ERROR(rc, "cannot set attributes of '%s'", src);
734 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
735 int dst_fd, bool is_restore)
737 char list[XATTR_LIST_MAX];
738 char value[XATTR_SIZE_MAX];
743 list_len = flistxattr(src_fd, list, sizeof(list));
748 while (name < list + list_len) {
749 rc = fgetxattr(src_fd, name, value, sizeof(value));
753 /* when we restore, we do not restore lustre xattr */
755 (strncmp(XATTR_TRUSTED_PREFIX, name,
756 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
757 rc = fsetxattr(dst_fd, name, value, rc, 0);
758 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
759 name, dst, rc, strerror(errno));
760 /* lustre.* attrs aren't supported on other FS's */
761 if (rc < 0 && errno != EOPNOTSUPP) {
763 CT_ERROR(rc, "cannot set extended attribute"
769 name += strlen(name) + 1;
775 static int ct_path_lustre(char *buf, int sz, const char *mnt,
776 const lustre_fid *fid)
778 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
779 dot_lustre_name, PFID(fid));
782 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
783 const lustre_fid *fid)
785 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
786 DFID_NOBRACE, archive_dir,
787 (fid)->f_oid & 0xFFFF,
788 (fid)->f_oid >> 16 & 0xFFFF,
789 (unsigned int)((fid)->f_seq & 0xFFFF),
790 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
791 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
792 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
796 static bool ct_is_retryable(int err)
798 return err == -ETIMEDOUT;
801 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
802 const struct hsm_action_item *hai,
803 int mdt_index, int open_flags)
808 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
811 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
812 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
818 static int ct_begin(struct hsm_copyaction_private **phcp,
819 const struct hsm_action_item *hai)
821 /* Restore takes specific parameters. Call the same function w/ default
822 * values for all other operations. */
823 return ct_begin_restore(phcp, hai, -1, 0);
826 static int ct_fini(struct hsm_copyaction_private **phcp,
827 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
829 struct hsm_copyaction_private *hcp;
833 CT_TRACE("Action completed, notifying coordinator "
834 "cookie="LPX64", FID="DFID", hp_flags=%d err=%d",
835 hai->hai_cookie, PFID(&hai->hai_fid),
838 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
840 if (phcp == NULL || *phcp == NULL) {
841 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
843 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
850 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
851 if (rc == -ECANCELED)
852 CT_ERROR(rc, "completed action on '%s' has been canceled: "
853 "cookie="LPX64", FID="DFID, lstr, hai->hai_cookie,
854 PFID(&hai->hai_fid));
856 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
858 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
864 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
866 struct hsm_copyaction_private *hcp = NULL;
868 char dst[PATH_MAX] = "";
871 bool rename_needed = false;
877 rc = ct_begin(&hcp, hai);
881 /* we fill archive so:
883 * destination = lustre FID
885 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
886 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
887 if (hai->hai_extent.length == -1) {
888 /* whole file, write it to tmp location and atomically
889 * replace old archived file */
890 strlcat(dst, "_tmp", sizeof(dst));
891 /* we cannot rely on the same test because ct_copy_data()
892 * updates hai_extent.length */
893 rename_needed = true;
896 CT_TRACE("archiving '%s' to '%s'", src, dst);
903 rc = ct_mkdir_p(dst);
905 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
909 src_fd = llapi_hsm_action_get_fd(hcp);
912 CT_ERROR(rc, "cannot open '%s' for read", src);
916 open_flags = O_WRONLY | O_NOFOLLOW;
917 /* If extent is specified, don't truncate an old archived copy */
918 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
920 dst_fd = open(dst, open_flags, FILE_PERM);
923 CT_ERROR(rc, "cannot open '%s' for write", dst);
927 /* saving stripe is not critical */
928 rc = ct_save_stripe(src_fd, src, dst);
930 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
933 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
935 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
942 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
946 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
948 /* attrs will remain on the MDS; no need to copy them, except possibly
949 for disaster recovery */
950 if (opt.o_copy_attrs) {
951 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
953 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
957 CT_TRACE("attr file for '%s' saved to archive '%s'",
961 /* xattrs will remain on the MDS; no need to copy them, except possibly
962 for disaster recovery */
963 if (opt.o_copy_xattrs) {
964 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
966 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
968 rcf = rcf ? rcf : rc;
970 CT_TRACE("xattr file for '%s' saved to archive '%s'",
974 if (rename_needed == true) {
975 char tmp_src[PATH_MAX];
976 char tmp_dst[PATH_MAX];
978 /* atomically replace old archived file */
979 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
981 rc = rename(dst, src);
984 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
987 /* rename lov file */
988 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
989 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
990 rc = rename(tmp_dst, tmp_src);
992 CT_ERROR(errno, "cannot rename '%s' to '%s'",
996 if (opt.o_shadow_tree) {
997 /* Create a namespace of softlinks that shadows the original
998 * Lustre namespace. This will only be current at
999 * time-of-archive (won't follow renames).
1000 * WARNING: release won't kill these links; a manual
1001 * cleanup of dead links would be required.
1004 long long recno = -1;
1010 sprintf(buf, DFID, PFID(&hai->hai_fid));
1011 sprintf(src, "%s/shadow/", opt.o_hsm_root);
1013 ptr = opt.o_hsm_root;
1015 (*ptr++ == '/') ? depth-- : 0;
1017 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
1018 sizeof(src) - strlen(src), &recno, &linkno);
1020 CT_ERROR(rc, "cannot get FID of '%s'", buf);
1021 rcf = rcf ? rcf : rc;
1025 /* Figure out how many parent dirs to symlink back */
1028 (*ptr++ == '/') ? depth++ : 0;
1033 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1035 if (ct_mkdir_p(src)) {
1036 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1037 rcf = rcf ? rcf : -errno;
1040 /* symlink already exists ? */
1041 sz = readlink(src, buf, sizeof(buf));
1042 /* detect truncation */
1043 if (sz == sizeof(buf)) {
1044 rcf = rcf ? rcf : -E2BIG;
1045 CT_ERROR(rcf, "readlink '%s' truncated", src);
1050 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1051 if (unlink(src) && errno != ENOENT) {
1053 "cannot unlink symlink '%s'",
1055 rcf = rcf ? rcf : -errno;
1057 /* unlink old symlink done */
1058 CT_TRACE("remove old symlink '%s' pointing"
1059 " to '%s'", src, buf);
1062 /* symlink already ok */
1063 CT_TRACE("symlink '%s' already pointing"
1064 " to '%s'", src, dst);
1069 if (symlink(dst, src)) {
1070 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1072 rcf = rcf ? rcf : -errno;
1075 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1087 if (ct_is_retryable(rc))
1088 hp_flags |= HP_FLAG_RETRY;
1099 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1104 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1106 struct hsm_copyaction_private *hcp = NULL;
1109 char lov_buf[XATTR_SIZE_MAX];
1110 size_t lov_size = sizeof(lov_buf);
1119 /* we fill lustre so:
1120 * source = lustre FID in the backend
1121 * destination = data FID = volatile file
1124 /* build backend file name from released file FID */
1125 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1127 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1130 CT_ERROR(rc, "cannot get mdt index "DFID"",
1131 PFID(&hai->hai_fid));
1134 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1135 * dependency on the structure format. */
1136 rc = ct_load_stripe(src, lov_buf, &lov_size);
1138 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1139 src, strerror(-rc));
1142 open_flags |= O_LOV_DELAY_CREATE;
1146 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1150 /* get the FID of the volatile file */
1151 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1153 CT_ERROR(rc, "restoring "DFID
1154 ", cannot get FID of created volatile file",
1155 PFID(&hai->hai_fid));
1159 /* build volatile "file name", for messages */
1160 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1162 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1164 if (opt.o_dry_run) {
1169 src_fd = open(src, O_RDONLY | O_NOATIME | O_NOFOLLOW);
1172 CT_ERROR(rc, "cannot open '%s' for read", src);
1176 dst_fd = llapi_hsm_action_get_fd(hcp);
1179 CT_ERROR(rc, "cannot open '%s' for write", dst);
1184 /* the layout cannot be allocated through .fid so we have to
1185 * restore a layout */
1186 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1188 CT_ERROR(rc, "cannot restore file striping info"
1189 " for '%s' from '%s'", dst, src);
1195 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1197 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1200 if (ct_is_retryable(rc))
1201 hp_flags |= HP_FLAG_RETRY;
1205 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1208 rc = ct_fini(&hcp, hai, hp_flags, rc);
1210 /* object swaping is done by cdt at copy end, so close of volatile file
1211 * cannot be done before */
1221 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1223 struct hsm_copyaction_private *hcp = NULL;
1227 rc = ct_begin(&hcp, hai);
1231 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1233 CT_TRACE("removing file '%s'", dst);
1235 if (opt.o_dry_run) {
1243 CT_ERROR(rc, "cannot unlink '%s'", dst);
1248 strlcat(dst, ".lov", sizeof(dst));
1252 CT_ERROR(rc, "cannot unlink '%s'", dst);
1258 rc = ct_fini(&hcp, hai, 0, rc);
1263 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1267 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1268 /* Print the original path */
1270 char path[PATH_MAX];
1271 long long recno = -1;
1274 sprintf(fid, DFID, PFID(&hai->hai_fid));
1275 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64,
1276 fid, hsm_copytool_action2name(hai->hai_action),
1277 hai->hai_len, hai->hai_cookie);
1278 rc = llapi_fid2path(opt.o_mnt, fid, path,
1279 sizeof(path), &recno, &linkno);
1281 CT_ERROR(rc, "cannot get path of FID %s", fid);
1283 CT_TRACE("processing file '%s'", path);
1286 switch (hai->hai_action) {
1287 /* set err_major, minor inside these functions */
1289 rc = ct_archive(hai, hal_flags);
1292 rc = ct_restore(hai, hal_flags);
1295 rc = ct_remove(hai, hal_flags);
1298 CT_TRACE("cancel not implemented for file system '%s'",
1300 /* Don't report progress to coordinator for this cookie:
1301 * the copy function will get ECANCELED when reporting
1308 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1311 ct_fini(NULL, hai, 0, rc);
1319 struct hsm_action_item *hai;
1322 static void *ct_thread(void *data)
1324 struct ct_th_data *cttd = data;
1327 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1331 pthread_exit((void *)(intptr_t)rc);
1334 static int ct_process_item_async(const struct hsm_action_item *hai,
1337 pthread_attr_t attr;
1339 struct ct_th_data *data;
1342 data = malloc(sizeof(*data));
1346 data->hai = malloc(hai->hai_len);
1347 if (data->hai == NULL) {
1352 memcpy(data->hai, hai, hai->hai_len);
1353 data->hal_flags = hal_flags;
1355 rc = pthread_attr_init(&attr);
1357 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1364 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1366 rc = pthread_create(&thread, &attr, ct_thread, data);
1368 CT_ERROR(rc, "cannot create thread for '%s' service",
1371 pthread_attr_destroy(&attr);
1375 static int ct_import_one(const char *src, const char *dst)
1377 char newarc[PATH_MAX];
1382 CT_TRACE("importing '%s' from '%s'", dst, src);
1384 if (stat(src, &st) < 0) {
1386 CT_ERROR(rc, "cannot stat '%s'", src);
1393 rc = llapi_hsm_import(dst,
1394 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1395 &st, 0, 0, 0, 0, NULL, &fid);
1397 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1401 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1403 rc = ct_mkdir_p(newarc);
1405 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1411 /* Lots of choices now: mv, ln, ln -s ? */
1412 rc = link(src, newarc); /* hardlink */
1415 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1419 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1424 static char *path_concat(const char *dirname, const char *basename)
1429 rc = asprintf(&result, "%s/%s", dirname, basename);
1436 static int ct_import_fid(const lustre_fid *import_fid)
1438 char fid_path[PATH_MAX];
1441 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1442 rc = access(fid_path, F_OK);
1443 if (rc == 0 || errno != ENOENT) {
1444 rc = (errno == 0) ? -EEXIST : -errno;
1445 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1449 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1452 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1454 return ct_import_one(fid_path, opt.o_dst);
1457 static int ct_import_recurse(const char *relpath)
1460 struct dirent ent, *cookie = NULL;
1461 char *srcpath, *newpath;
1462 lustre_fid import_fid;
1465 if (relpath == NULL)
1468 /* Is relpath a FID? In which case SFID should expand to three
1470 rc = sscanf(relpath, SFID, RFID(&import_fid));
1472 return ct_import_fid(&import_fid);
1474 srcpath = path_concat(opt.o_hsm_root, relpath);
1475 if (srcpath == NULL) {
1480 dir = opendir(srcpath);
1482 /* Not a dir, or error */
1483 if (errno == ENOTDIR) {
1484 /* Single regular file case, treat o_dst as absolute
1486 rc = ct_import_one(srcpath, opt.o_dst);
1489 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1498 rc = readdir_r(dir, &ent, &cookie);
1501 CT_ERROR(rc, "cannot readdir_r '%s'", relpath);
1504 } else if ((rc == 0) && (cookie == NULL)) {
1505 /* end of directory */
1509 if (!strcmp(ent.d_name, ".") ||
1510 !strcmp(ent.d_name, ".."))
1513 /* New relative path */
1514 newpath = path_concat(relpath, ent.d_name);
1515 if (newpath == NULL) {
1521 if (ent.d_type == DT_DIR) {
1522 rc = ct_import_recurse(newpath);
1527 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1528 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1529 /* Make the target dir in the Lustre fs */
1530 rc = ct_mkdir_p(dst);
1532 /* Import the file */
1533 rc = ct_import_one(src, dst);
1535 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1541 CT_ERROR(rc, "cannot import '%s'", newpath);
1542 if (err_major && opt.o_abort_on_error) {
1556 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1562 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1564 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1565 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1567 if (!opt.o_dry_run) {
1569 if (rename(src, dst)) {
1571 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1574 /* rename lov file */
1575 strlcat(src, ".lov", sizeof(src));
1576 strlcat(dst, ".lov", sizeof(dst));
1577 if (rename(src, dst))
1578 CT_ERROR(errno, "cannot rename '%s' to '%s'", src, dst);
1584 static bool fid_is_file(lustre_fid *fid)
1586 return fid_is_norm(fid) || fid_is_igif(fid);
1589 static bool should_ignore_line(const char *line)
1593 for (i = 0; line[i] != '\0'; i++) {
1594 if (isspace(line[i]))
1596 else if (line[i] == '#')
1605 static int ct_rebind_list(const char *list)
1611 size_t line_size = 0;
1612 unsigned int nl = 0;
1613 unsigned int ok = 0;
1615 filp = fopen(list, "r");
1618 CT_ERROR(rc, "cannot open '%s'", list);
1622 /* each line consists of 2 FID */
1623 while ((r = getline(&line, &line_size, filp)) != -1) {
1627 /* Ignore empty and commented out ('#...') lines. */
1628 if (should_ignore_line(line))
1633 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1634 if (rc != 6 || !fid_is_file(&old_fid) ||
1635 !fid_is_file(&new_fid)) {
1637 "'%s' FID expected near '%s', line %u",
1643 if (ct_rebind_one(&old_fid, &new_fid))
1654 /* return 0 if all rebinds were successful */
1655 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1657 return ok == nl ? 0 : -1;
1660 static int ct_rebind(void)
1668 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1669 !fid_is_file(&old_fid)) {
1671 CT_ERROR(rc, "'%s' invalid FID format", opt.o_src);
1675 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1676 !fid_is_file(&new_fid)) {
1678 CT_ERROR(rc, "'%s' invalid FID format", opt.o_dst);
1682 rc = ct_rebind_one(&old_fid, &new_fid);
1687 /* o_src is a list file */
1688 rc = ct_rebind_list(opt.o_src);
1693 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1698 struct dirent ent, *cookie = NULL;
1702 dir = opendir(dirpath);
1705 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1709 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1711 /* end of directory.
1712 * rc is 0 and seqmax contains the max value. */
1715 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1718 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1719 CT_TRACE("'%s' has an unexpected dirname format, "
1720 "skip entry", ent.d_name);
1723 if (sub_seq > *sub_seqmax)
1724 *sub_seqmax = sub_seq;
1727 CT_ERROR(rc, "cannot readdir_r '%s'", dirpath);
1734 static int ct_max_sequence(void)
1737 char path[PATH_MAX];
1741 strlcpy(path, opt.o_hsm_root, sizeof(path));
1742 /* FID sequence is stored in top-level directory names:
1743 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1745 for (i = 0; i < 4; i++) {
1748 rc = ct_dir_level_max(path, &subseq);
1751 seq |= ((__u64)subseq << ((3 - i) * 16));
1752 path_len = strlen(path);
1753 rc = snprintf(path + path_len, sizeof(path) - path_len,
1755 if (rc >= (sizeof(path) - path_len))
1757 path[sizeof(path) - 1] = '\0';
1760 printf("max_sequence: "LPX64"\n", seq);
1765 static void handler(int signal)
1767 psignal(signal, "exiting");
1768 /* If we don't clean up upon interrupt, umount thinks there's a ref
1769 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1770 * does successfully unmount and the mount is actually gone, but the
1771 * mtab entry remains. So this just makes mtab happier. */
1772 llapi_hsm_copytool_unregister(&ctdata);
1774 /* Also remove fifo upon signal as during normal/error exit */
1775 if (opt.o_event_fifo != NULL)
1776 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1780 /* Daemon waits for messages from the kernel; run it in the background. */
1781 static int ct_run(void)
1785 if (opt.o_daemonize) {
1789 CT_ERROR(rc, "cannot daemonize");
1794 setbuf(stdout, NULL);
1796 if (opt.o_event_fifo != NULL) {
1797 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
1799 CT_ERROR(rc, "failed to register event fifo");
1802 llapi_error_callback_set(llapi_hsm_log_error);
1805 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
1807 opt.o_archive_id, 0);
1809 CT_ERROR(rc, "cannot start copytool interface");
1813 signal(SIGINT, handler);
1814 signal(SIGTERM, handler);
1817 struct hsm_action_list *hal;
1818 struct hsm_action_item *hai;
1822 CT_TRACE("waiting for message from kernel");
1824 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1825 if (rc == -ESHUTDOWN) {
1826 CT_TRACE("shutting down");
1828 } else if (rc < 0) {
1829 CT_WARN("cannot receive action list: %s",
1832 if (opt.o_abort_on_error)
1838 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1839 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1841 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1843 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
1844 hal->hal_fsname, fs_name);
1846 if (opt.o_abort_on_error)
1852 hai = hai_first(hal);
1853 while (++i <= hal->hal_count) {
1854 if ((char *)hai - (char *)hal > msgsize) {
1857 "'%s' item %d past end of message!",
1862 rc = ct_process_item_async(hai, hal->hal_flags);
1864 CT_ERROR(rc, "'%s' item %d process",
1866 if (opt.o_abort_on_error && err_major)
1868 hai = hai_next(hai);
1871 if (opt.o_abort_on_error && err_major)
1875 llapi_hsm_copytool_unregister(&ctdata);
1876 if (opt.o_event_fifo != NULL)
1877 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1882 static int ct_setup(void)
1886 /* set llapi message level */
1887 llapi_msg_set_level(opt.o_verbose);
1889 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1892 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1896 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1898 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
1903 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
1904 if (opt.o_mnt_fd < 0) {
1906 CT_ERROR(rc, "cannot open mount point at '%s'",
1914 static int ct_cleanup(void)
1918 if (opt.o_mnt_fd >= 0) {
1919 rc = close(opt.o_mnt_fd);
1922 CT_ERROR(rc, "cannot close mount point");
1931 CT_ERROR(rc, "cannot close archive root directory");
1939 int main(int argc, char **argv)
1943 strlcpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1944 rc = ct_parseopts(argc, argv);
1946 CT_WARN("try '%s --help' for more information", cmd_name);
1954 switch (opt.o_action) {
1956 rc = ct_import_recurse(opt.o_src);
1962 rc = ct_max_sequence();
1969 if (opt.o_action != CA_MAXSEQ)
1970 CT_TRACE("process finished, errs: %d major, %d minor,"
1971 " rc=%d (%s)", err_major, err_minor, rc,