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, 2015, 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 <libcfs/util/string.h>
55 #include <lustre/lustre_idl.h>
56 #include <lustre/lustreapi.h>
58 /* Progress reporting period */
59 #define REPORT_INTERVAL_DEFAULT 30
60 /* HSM hash subdir permissions */
61 #define DIR_PERM S_IRWXU
62 /* HSM hash file permissions */
63 #define FILE_PERM (S_IRUSR | S_IWUSR)
65 #define ONE_MB 0x100000
68 # define NSEC_PER_SEC 1000000000UL
86 int o_archive_id[LL_HSM_MAX_ARCHIVE];
88 unsigned long long o_bandwidth;
90 enum ct_action o_action;
95 char *o_src; /* for import, or rebind */
96 char *o_dst; /* for import, or rebind */
99 /* everything else is zeroed */
100 struct options opt = {
103 .o_verbose = LLAPI_MSG_INFO,
105 .o_report_int = REPORT_INTERVAL_DEFAULT,
106 .o_chunk_size = ONE_MB,
109 /* hsm_copytool_private will hold an open FD on the lustre mount point
110 * for us. Additionally open one on the archive FS root to make sure
111 * it doesn't drop out from under us (and remind the admin to shutdown
112 * the copytool before unmounting). */
114 static int arc_fd = -1;
116 static int err_major;
117 static int err_minor;
119 static char cmd_name[PATH_MAX];
120 static char fs_name[MAX_OBD_NAME + 1];
122 static struct hsm_copytool_private *ctdata;
124 static inline double ct_now(void)
128 gettimeofday(&tv, NULL);
130 return tv.tv_sec + 0.000001 * tv.tv_usec;
133 #define CT_ERROR(_rc, _format, ...) \
134 llapi_error(LLAPI_MSG_ERROR, _rc, \
135 "%f %s[%ld]: "_format, \
136 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
138 #define CT_DEBUG(_format, ...) \
139 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
140 "%f %s[%ld]: "_format, \
141 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
143 #define CT_WARN(_format, ...) \
144 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
145 "%f %s[%ld]: "_format, \
146 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
148 #define CT_TRACE(_format, ...) \
149 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
150 "%f %s[%ld]: "_format, \
151 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
153 static void usage(const char *name, int rc)
156 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
157 "The Lustre HSM Posix copy tool can be used as a daemon or "
158 "as a command line tool\n"
159 "The Lustre HSM daemon acts on action requests from Lustre\n"
160 "to copy files to and from an HSM archive system.\n"
161 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
162 "to an HSM mounted at a given hsm_root.\n"
163 " --daemon Daemon mode, run in background\n"
165 " --no-attr Don't copy file attributes\n"
166 " --no-shadow Don't create shadow namespace in archive\n"
167 " --no-xattr Don't copy file extended attributes\n"
168 "The Lustre HSM tool performs administrator-type actions\n"
169 "on a Lustre HSM archive.\n"
170 "This POSIX-flavored tool can link an existing HSM namespace\n"
171 "into a Lustre filesystem.\n"
173 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
174 " import an archived subtree from\n"
175 " <src> (FID or relative path to hsm_root) into the Lustre\n"
177 " <dst> (absolute path)\n"
178 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
179 " rebind an entry in the HSM to a new FID\n"
180 " <old_FID> old FID the HSM entry is bound to\n"
181 " <new_FID> new FID to bind the HSM entry to\n"
182 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
183 " perform the rebind operation for all FID in the list file\n"
184 " each line of <list_file> consists of <old_FID> <new_FID>\n"
185 " %s [options] --max-sequence <fsname>\n"
186 " return the max fid sequence of archived files\n"
187 " --abort-on-error Abort operation on major error\n"
188 " -A, --archive <#> Archive number (repeatable)\n"
189 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
191 " --dry-run Don't run, just show what would be done\n"
192 " -c, --chunk-size <sz> I/O size used during data copy\n"
193 " (unit can be used, default is MB)\n"
194 " -f, --event-fifo <path> Write events stream to fifo\n"
195 " -p, --hsm-root <path> Target HSM mount point\n"
196 " -q, --quiet Produce less verbose output\n"
197 " -u, --update-interval <s> Interval between progress reports sent\n"
199 " -v, --verbose Produce more verbose output\n",
200 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
205 static int ct_parseopts(int argc, char * const *argv)
207 struct option long_opts[] = {
208 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
209 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
210 {"archive", required_argument, NULL, 'A'},
211 {"bandwidth", required_argument, NULL, 'b'},
212 {"chunk-size", required_argument, NULL, 'c'},
213 {"chunk_size", required_argument, NULL, 'c'},
214 {"daemon", no_argument, &opt.o_daemonize, 1},
215 {"event-fifo", required_argument, NULL, 'f'},
216 {"event_fifo", required_argument, NULL, 'f'},
217 {"dry-run", no_argument, &opt.o_dry_run, 1},
218 {"help", no_argument, NULL, 'h'},
219 {"hsm-root", required_argument, NULL, 'p'},
220 {"hsm_root", required_argument, NULL, 'p'},
221 {"import", no_argument, NULL, 'i'},
222 {"max-sequence", no_argument, NULL, 'M'},
223 {"max_sequence", no_argument, NULL, 'M'},
224 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
225 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
226 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
227 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
228 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
229 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
230 {"quiet", no_argument, NULL, 'q'},
231 {"rebind", no_argument, NULL, 'r'},
232 {"update-interval", required_argument, NULL, 'u'},
233 {"update_interval", required_argument, NULL, 'u'},
234 {"verbose", no_argument, NULL, 'v'},
238 unsigned long long value;
239 unsigned long long unit;
242 while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
243 long_opts, NULL)) != -1) {
246 if ((opt.o_archive_cnt >= LL_HSM_MAX_ARCHIVE) ||
247 (atoi(optarg) >= LL_HSM_MAX_ARCHIVE)) {
249 CT_ERROR(rc, "archive number must be less"
250 "than %zu", LL_HSM_MAX_ARCHIVE);
253 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
256 case 'b': /* -b and -c have both a number with unit as arg */
259 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
261 CT_ERROR(rc, "bad value for -%c '%s'", c,
266 opt.o_chunk_size = value;
268 opt.o_bandwidth = value;
271 opt.o_event_fifo = optarg;
276 opt.o_action = CA_IMPORT;
279 opt.o_action = CA_MAXSEQ;
282 opt.o_hsm_root = optarg;
288 opt.o_action = CA_REBIND;
291 opt.o_report_int = atoi(optarg);
292 if (opt.o_report_int < 0) {
294 CT_ERROR(rc, "bad value for -%c '%s'", c,
309 switch (opt.o_action) {
311 /* src dst mount_point */
312 if (argc != optind + 3) {
314 CT_ERROR(rc, "--import requires 2 arguments");
317 opt.o_src = argv[optind++];
318 opt.o_dst = argv[optind++];
321 /* FID1 FID2 mount_point or FILE mount_point */
322 if (argc == optind + 2) {
323 opt.o_src = argv[optind++];
325 } else if (argc == optind + 3) {
326 opt.o_src = argv[optind++];
327 opt.o_dst = argv[optind++];
330 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
336 /* just mount point */
340 if (argc != optind + 1) {
342 CT_ERROR(rc, "no mount point specified");
346 opt.o_mnt = argv[optind];
349 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
350 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
352 if (opt.o_hsm_root == NULL) {
354 CT_ERROR(rc, "must specify a root directory for the backend");
358 if (opt.o_action == CA_IMPORT) {
359 if (opt.o_src && opt.o_src[0] == '/') {
362 "source path must be relative to HSM root");
366 if (opt.o_dst && opt.o_dst[0] != '/') {
368 CT_ERROR(rc, "destination path must be absolute");
377 static int ct_mkdir_p(const char *path)
390 while ((ptr = strchr(ptr, '/')) != NULL) {
392 rc = mkdir(saved, DIR_PERM);
394 if (rc < 0 && errno != EEXIST) {
396 CT_ERROR(rc, "cannot mkdir '%s'", path);
408 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
410 char lov_file[PATH_MAX];
411 char lov_buf[XATTR_SIZE_MAX];
412 struct lov_user_md *lum;
417 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
418 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
420 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
422 if (xattr_size < 0) {
424 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
428 lum = (struct lov_user_md *)lov_buf;
430 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
431 lum->lmm_magic == LOV_USER_MAGIC_V3) {
432 /* Set stripe_offset to -1 so that it is not interpreted as a
433 * hint on restore. */
434 lum->lmm_stripe_offset = -1;
437 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
440 CT_ERROR(rc, "cannot open '%s'", lov_file);
444 rc = write(fd, lum, xattr_size);
447 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
448 xattr_size, lov_file);
456 CT_ERROR(rc, "cannot close '%s'", lov_file);
468 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
470 char lov_file[PATH_MAX];
474 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
475 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
477 fd = open(lov_file, O_RDONLY);
479 CT_ERROR(errno, "cannot open '%s'", lov_file);
483 rc = read(fd, lovea, *lovea_size);
485 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
486 *lovea_size, lov_file);
497 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
498 const void *lovea, size_t lovea_size)
502 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
506 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
512 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
513 const char *dst, int src_fd, int dst_fd,
514 const struct hsm_action_item *hai, long hal_flags)
516 struct hsm_extent he;
517 __u64 offset = hai->hai_extent.offset;
521 __u64 write_total = 0;
522 __u64 length = hai->hai_extent.length;
523 time_t last_report_time;
525 double start_ct_now = ct_now();
526 /* Bandwidth Control */
529 time_t last_bw_print;
531 if (fstat(src_fd, &src_st) < 0) {
533 CT_ERROR(rc, "cannot stat '%s'", src);
537 if (!S_ISREG(src_st.st_mode)) {
539 CT_ERROR(rc, "'%s' is not a regular file", src);
543 if (hai->hai_extent.offset > (__u64)src_st.st_size) {
545 CT_ERROR(rc, "Trying to start reading past end ("LPU64" > "
546 "%jd) of '%s' source file", hai->hai_extent.offset,
547 (intmax_t)src_st.st_size, src);
551 if (fstat(dst_fd, &dst_st) < 0) {
553 CT_ERROR(rc, "cannot stat '%s'", dst);
557 if (!S_ISREG(dst_st.st_mode)) {
559 CT_ERROR(rc, "'%s' is not a regular file", dst);
563 /* Don't read beyond a given extent */
564 if (length > src_st.st_size - hai->hai_extent.offset)
565 length = src_st.st_size - hai->hai_extent.offset;
567 start_time = last_bw_print = last_report_time = time(NULL);
571 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
573 /* Action has been canceled or something wrong
574 * is happening. Stop copying data. */
575 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
582 buf = malloc(opt.o_chunk_size);
588 CT_TRACE("start copy of "LPU64" bytes from '%s' to '%s'",
591 while (write_total < length) {
594 int chunk = (length - write_total > opt.o_chunk_size) ?
595 opt.o_chunk_size : length - write_total;
597 rsize = pread(src_fd, buf, chunk, offset);
604 CT_ERROR(rc, "cannot read from '%s'", src);
608 wsize = pwrite(dst_fd, buf, rsize, offset);
611 CT_ERROR(rc, "cannot write to '%s'", dst);
615 write_total += wsize;
619 /* sleep if needed, to honor bandwidth limits */
620 if (opt.o_bandwidth != 0) {
621 unsigned long long write_theory;
623 write_theory = (now - start_time) * opt.o_bandwidth;
625 if (write_theory < write_total) {
626 unsigned long long excess;
627 struct timespec delay;
629 excess = write_total - write_theory;
631 delay.tv_sec = excess / opt.o_bandwidth;
632 delay.tv_nsec = (excess % opt.o_bandwidth) *
633 NSEC_PER_SEC / opt.o_bandwidth;
635 if (now >= last_bw_print + opt.o_report_int) {
636 CT_TRACE("bandwith control: %lluB/s "
637 "excess=%llu sleep for "
639 opt.o_bandwidth, excess,
640 (long long)delay.tv_sec,
646 rc = nanosleep(&delay, &delay);
647 } while (rc < 0 && errno == EINTR);
649 CT_ERROR(errno, "delay for bandwidth "
650 "control failed to sleep: "
651 "residual=%lld.%09lds",
652 (long long)delay.tv_sec,
660 if (now >= last_report_time + opt.o_report_int) {
661 last_report_time = now;
662 CT_TRACE("%%"LPU64" ", 100 * write_total / length);
663 he.length = write_total;
664 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
666 /* Action has been canceled or something wrong
667 * is happening. Stop copying data. */
668 CT_ERROR(rc, "progress ioctl for copy"
669 " '%s'->'%s' failed", src, dst);
678 * truncate restored file
679 * size is taken from the archive this is done to support
680 * restore after a force release which leaves the file with the
681 * wrong size (can big bigger than the new size)
683 if ((hai->hai_action == HSMA_RESTORE) &&
684 (src_st.st_size < dst_st.st_size)) {
686 * make sure the file is on disk before reporting success.
688 rc = ftruncate(dst_fd, src_st.st_size);
691 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
692 dst, (intmax_t)src_st.st_size);
700 CT_TRACE("copied "LPU64" bytes in %f seconds",
701 length, ct_now() - start_ct_now);
706 /* Copy file attributes from file src to file dest */
707 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
711 struct timeval times[2];
714 if (fstat(src_fd, &st) < 0) {
716 CT_ERROR(rc, "cannot stat '%s'", src);
720 times[0].tv_sec = st.st_atime;
721 times[0].tv_usec = 0;
722 times[1].tv_sec = st.st_mtime;
723 times[1].tv_usec = 0;
724 if (fchmod(dst_fd, st.st_mode) < 0 ||
725 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
726 futimes(dst_fd, times) < 0) {
728 CT_ERROR(rc, "cannot set attributes of '%s'", src);
735 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
736 int dst_fd, bool is_restore)
738 char list[XATTR_LIST_MAX];
739 char value[XATTR_SIZE_MAX];
744 list_len = flistxattr(src_fd, list, sizeof(list));
749 while (name < list + list_len) {
750 rc = fgetxattr(src_fd, name, value, sizeof(value));
754 /* when we restore, we do not restore lustre xattr */
756 (strncmp(XATTR_TRUSTED_PREFIX, name,
757 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
758 rc = fsetxattr(dst_fd, name, value, rc, 0);
759 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
760 name, dst, rc, strerror(errno));
761 /* lustre.* attrs aren't supported on other FS's */
762 if (rc < 0 && errno != EOPNOTSUPP) {
764 CT_ERROR(rc, "cannot set extended attribute"
770 name += strlen(name) + 1;
776 static int ct_path_lustre(char *buf, int sz, const char *mnt,
777 const lustre_fid *fid)
779 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
780 dot_lustre_name, PFID(fid));
783 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
784 const lustre_fid *fid)
786 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
787 DFID_NOBRACE, archive_dir,
788 (fid)->f_oid & 0xFFFF,
789 (fid)->f_oid >> 16 & 0xFFFF,
790 (unsigned int)((fid)->f_seq & 0xFFFF),
791 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
792 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
793 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
797 static bool ct_is_retryable(int err)
799 return err == -ETIMEDOUT;
802 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
803 const struct hsm_action_item *hai,
804 int mdt_index, int open_flags)
809 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
812 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
813 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
819 static int ct_begin(struct hsm_copyaction_private **phcp,
820 const struct hsm_action_item *hai)
822 /* Restore takes specific parameters. Call the same function w/ default
823 * values for all other operations. */
824 return ct_begin_restore(phcp, hai, -1, 0);
827 static int ct_fini(struct hsm_copyaction_private **phcp,
828 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
830 struct hsm_copyaction_private *hcp;
834 CT_TRACE("Action completed, notifying coordinator "
835 "cookie="LPX64", FID="DFID", hp_flags=%d err=%d",
836 hai->hai_cookie, PFID(&hai->hai_fid),
839 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
841 if (phcp == NULL || *phcp == NULL) {
842 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
844 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
851 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
852 if (rc == -ECANCELED)
853 CT_ERROR(rc, "completed action on '%s' has been canceled: "
854 "cookie="LPX64", FID="DFID, lstr, hai->hai_cookie,
855 PFID(&hai->hai_fid));
857 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
859 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
865 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
867 struct hsm_copyaction_private *hcp = NULL;
869 char dst[PATH_MAX] = "";
872 bool rename_needed = false;
878 rc = ct_begin(&hcp, hai);
882 /* we fill archive so:
884 * destination = lustre FID
886 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
887 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
888 if (hai->hai_extent.length == -1) {
889 /* whole file, write it to tmp location and atomically
890 * replace old archived file */
891 strlcat(dst, "_tmp", sizeof(dst));
892 /* we cannot rely on the same test because ct_copy_data()
893 * updates hai_extent.length */
894 rename_needed = true;
897 CT_TRACE("archiving '%s' to '%s'", src, dst);
904 rc = ct_mkdir_p(dst);
906 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
910 src_fd = llapi_hsm_action_get_fd(hcp);
913 CT_ERROR(rc, "cannot open '%s' for read", src);
917 open_flags = O_WRONLY | O_NOFOLLOW;
918 /* If extent is specified, don't truncate an old archived copy */
919 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
921 dst_fd = open(dst, open_flags, FILE_PERM);
924 CT_ERROR(rc, "cannot open '%s' for write", dst);
928 /* saving stripe is not critical */
929 rc = ct_save_stripe(src_fd, src, dst);
931 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
934 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
936 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
943 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
947 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
949 /* attrs will remain on the MDS; no need to copy them, except possibly
950 for disaster recovery */
951 if (opt.o_copy_attrs) {
952 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
954 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
958 CT_TRACE("attr file for '%s' saved to archive '%s'",
962 /* xattrs will remain on the MDS; no need to copy them, except possibly
963 for disaster recovery */
964 if (opt.o_copy_xattrs) {
965 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
967 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
969 rcf = rcf ? rcf : rc;
971 CT_TRACE("xattr file for '%s' saved to archive '%s'",
975 if (rename_needed == true) {
976 char tmp_src[PATH_MAX];
977 char tmp_dst[PATH_MAX];
979 /* atomically replace old archived file */
980 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
982 rc = rename(dst, src);
985 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
988 /* rename lov file */
989 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
990 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
991 rc = rename(tmp_dst, tmp_src);
993 CT_ERROR(errno, "cannot rename '%s' to '%s'",
997 if (opt.o_shadow_tree) {
998 /* Create a namespace of softlinks that shadows the original
999 * Lustre namespace. This will only be current at
1000 * time-of-archive (won't follow renames).
1001 * WARNING: release won't kill these links; a manual
1002 * cleanup of dead links would be required.
1005 long long recno = -1;
1011 sprintf(buf, DFID, PFID(&hai->hai_fid));
1012 sprintf(src, "%s/shadow/", opt.o_hsm_root);
1014 ptr = opt.o_hsm_root;
1016 (*ptr++ == '/') ? depth-- : 0;
1018 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
1019 sizeof(src) - strlen(src), &recno, &linkno);
1021 CT_ERROR(rc, "cannot get FID of '%s'", buf);
1022 rcf = rcf ? rcf : rc;
1026 /* Figure out how many parent dirs to symlink back */
1029 (*ptr++ == '/') ? depth++ : 0;
1034 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1036 if (ct_mkdir_p(src)) {
1037 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1038 rcf = rcf ? rcf : -errno;
1041 /* symlink already exists ? */
1042 sz = readlink(src, buf, sizeof(buf));
1043 /* detect truncation */
1044 if (sz == sizeof(buf)) {
1045 rcf = rcf ? rcf : -E2BIG;
1046 CT_ERROR(rcf, "readlink '%s' truncated", src);
1051 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1052 if (unlink(src) && errno != ENOENT) {
1054 "cannot unlink symlink '%s'",
1056 rcf = rcf ? rcf : -errno;
1058 /* unlink old symlink done */
1059 CT_TRACE("remove old symlink '%s' pointing"
1060 " to '%s'", src, buf);
1063 /* symlink already ok */
1064 CT_TRACE("symlink '%s' already pointing"
1065 " to '%s'", src, dst);
1070 if (symlink(dst, src)) {
1071 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1073 rcf = rcf ? rcf : -errno;
1076 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1088 if (ct_is_retryable(rc))
1089 hp_flags |= HP_FLAG_RETRY;
1100 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1105 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1107 struct hsm_copyaction_private *hcp = NULL;
1110 char lov_buf[XATTR_SIZE_MAX];
1111 size_t lov_size = sizeof(lov_buf);
1120 /* we fill lustre so:
1121 * source = lustre FID in the backend
1122 * destination = data FID = volatile file
1125 /* build backend file name from released file FID */
1126 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1128 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1131 CT_ERROR(rc, "cannot get mdt index "DFID"",
1132 PFID(&hai->hai_fid));
1135 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1136 * dependency on the structure format. */
1137 rc = ct_load_stripe(src, lov_buf, &lov_size);
1139 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1140 src, strerror(-rc));
1143 open_flags |= O_LOV_DELAY_CREATE;
1147 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1151 /* get the FID of the volatile file */
1152 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1154 CT_ERROR(rc, "restoring "DFID
1155 ", cannot get FID of created volatile file",
1156 PFID(&hai->hai_fid));
1160 /* build volatile "file name", for messages */
1161 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1163 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1165 if (opt.o_dry_run) {
1170 src_fd = open(src, O_RDONLY | O_NOATIME | O_NOFOLLOW);
1173 CT_ERROR(rc, "cannot open '%s' for read", src);
1177 dst_fd = llapi_hsm_action_get_fd(hcp);
1180 CT_ERROR(rc, "cannot open '%s' for write", dst);
1185 /* the layout cannot be allocated through .fid so we have to
1186 * restore a layout */
1187 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1189 CT_ERROR(rc, "cannot restore file striping info"
1190 " for '%s' from '%s'", dst, src);
1196 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1198 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1201 if (ct_is_retryable(rc))
1202 hp_flags |= HP_FLAG_RETRY;
1206 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1209 rc = ct_fini(&hcp, hai, hp_flags, rc);
1211 /* object swaping is done by cdt at copy end, so close of volatile file
1212 * cannot be done before */
1222 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1224 struct hsm_copyaction_private *hcp = NULL;
1228 rc = ct_begin(&hcp, hai);
1232 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1234 CT_TRACE("removing file '%s'", dst);
1236 if (opt.o_dry_run) {
1244 CT_ERROR(rc, "cannot unlink '%s'", dst);
1249 strlcat(dst, ".lov", sizeof(dst));
1253 CT_ERROR(rc, "cannot unlink '%s'", dst);
1259 rc = ct_fini(&hcp, hai, 0, rc);
1264 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1268 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1269 /* Print the original path */
1271 char path[PATH_MAX];
1272 long long recno = -1;
1275 sprintf(fid, DFID, PFID(&hai->hai_fid));
1276 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64,
1277 fid, hsm_copytool_action2name(hai->hai_action),
1278 hai->hai_len, hai->hai_cookie);
1279 rc = llapi_fid2path(opt.o_mnt, fid, path,
1280 sizeof(path), &recno, &linkno);
1282 CT_ERROR(rc, "cannot get path of FID %s", fid);
1284 CT_TRACE("processing file '%s'", path);
1287 switch (hai->hai_action) {
1288 /* set err_major, minor inside these functions */
1290 rc = ct_archive(hai, hal_flags);
1293 rc = ct_restore(hai, hal_flags);
1296 rc = ct_remove(hai, hal_flags);
1299 CT_TRACE("cancel not implemented for file system '%s'",
1301 /* Don't report progress to coordinator for this cookie:
1302 * the copy function will get ECANCELED when reporting
1309 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1312 ct_fini(NULL, hai, 0, rc);
1320 struct hsm_action_item *hai;
1323 static void *ct_thread(void *data)
1325 struct ct_th_data *cttd = data;
1328 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1332 pthread_exit((void *)(intptr_t)rc);
1335 static int ct_process_item_async(const struct hsm_action_item *hai,
1338 pthread_attr_t attr;
1340 struct ct_th_data *data;
1343 data = malloc(sizeof(*data));
1347 data->hai = malloc(hai->hai_len);
1348 if (data->hai == NULL) {
1353 memcpy(data->hai, hai, hai->hai_len);
1354 data->hal_flags = hal_flags;
1356 rc = pthread_attr_init(&attr);
1358 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1365 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1367 rc = pthread_create(&thread, &attr, ct_thread, data);
1369 CT_ERROR(rc, "cannot create thread for '%s' service",
1372 pthread_attr_destroy(&attr);
1376 static int ct_import_one(const char *src, const char *dst)
1378 char newarc[PATH_MAX];
1383 CT_TRACE("importing '%s' from '%s'", dst, src);
1385 if (stat(src, &st) < 0) {
1387 CT_ERROR(rc, "cannot stat '%s'", src);
1394 rc = llapi_hsm_import(dst,
1395 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1396 &st, 0, 0, 0, 0, NULL, &fid);
1398 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1402 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1404 rc = ct_mkdir_p(newarc);
1406 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1412 /* Lots of choices now: mv, ln, ln -s ? */
1413 rc = link(src, newarc); /* hardlink */
1416 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1420 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1425 static char *path_concat(const char *dirname, const char *basename)
1430 rc = asprintf(&result, "%s/%s", dirname, basename);
1437 static int ct_import_fid(const lustre_fid *import_fid)
1439 char fid_path[PATH_MAX];
1442 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1443 rc = access(fid_path, F_OK);
1444 if (rc == 0 || errno != ENOENT) {
1445 rc = (errno == 0) ? -EEXIST : -errno;
1446 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1450 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1453 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1455 return ct_import_one(fid_path, opt.o_dst);
1458 static int ct_import_recurse(const char *relpath)
1461 struct dirent ent, *cookie = NULL;
1462 char *srcpath, *newpath;
1463 lustre_fid import_fid;
1466 if (relpath == NULL)
1469 /* Is relpath a FID? In which case SFID should expand to three
1471 rc = sscanf(relpath, SFID, RFID(&import_fid));
1473 return ct_import_fid(&import_fid);
1475 srcpath = path_concat(opt.o_hsm_root, relpath);
1476 if (srcpath == NULL) {
1481 dir = opendir(srcpath);
1483 /* Not a dir, or error */
1484 if (errno == ENOTDIR) {
1485 /* Single regular file case, treat o_dst as absolute
1487 rc = ct_import_one(srcpath, opt.o_dst);
1490 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1499 rc = readdir_r(dir, &ent, &cookie);
1502 CT_ERROR(rc, "cannot readdir_r '%s'", relpath);
1505 } else if ((rc == 0) && (cookie == NULL)) {
1506 /* end of directory */
1510 if (!strcmp(ent.d_name, ".") ||
1511 !strcmp(ent.d_name, ".."))
1514 /* New relative path */
1515 newpath = path_concat(relpath, ent.d_name);
1516 if (newpath == NULL) {
1522 if (ent.d_type == DT_DIR) {
1523 rc = ct_import_recurse(newpath);
1528 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1529 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1530 /* Make the target dir in the Lustre fs */
1531 rc = ct_mkdir_p(dst);
1533 /* Import the file */
1534 rc = ct_import_one(src, dst);
1536 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1542 CT_ERROR(rc, "cannot import '%s'", newpath);
1543 if (err_major && opt.o_abort_on_error) {
1557 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1563 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1565 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1566 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1568 if (!opt.o_dry_run) {
1570 if (rename(src, dst)) {
1572 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1575 /* rename lov file */
1576 strlcat(src, ".lov", sizeof(src));
1577 strlcat(dst, ".lov", sizeof(dst));
1578 if (rename(src, dst))
1579 CT_ERROR(errno, "cannot rename '%s' to '%s'", src, dst);
1585 static bool fid_is_file(lustre_fid *fid)
1587 return fid_is_norm(fid) || fid_is_igif(fid);
1590 static bool should_ignore_line(const char *line)
1594 for (i = 0; line[i] != '\0'; i++) {
1595 if (isspace(line[i]))
1597 else if (line[i] == '#')
1606 static int ct_rebind_list(const char *list)
1612 size_t line_size = 0;
1613 unsigned int nl = 0;
1614 unsigned int ok = 0;
1616 filp = fopen(list, "r");
1619 CT_ERROR(rc, "cannot open '%s'", list);
1623 /* each line consists of 2 FID */
1624 while ((r = getline(&line, &line_size, filp)) != -1) {
1628 /* Ignore empty and commented out ('#...') lines. */
1629 if (should_ignore_line(line))
1634 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1635 if (rc != 6 || !fid_is_file(&old_fid) ||
1636 !fid_is_file(&new_fid)) {
1638 "'%s' FID expected near '%s', line %u",
1644 if (ct_rebind_one(&old_fid, &new_fid))
1655 /* return 0 if all rebinds were successful */
1656 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1658 return ok == nl ? 0 : -1;
1661 static int ct_rebind(void)
1669 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1670 !fid_is_file(&old_fid)) {
1672 CT_ERROR(rc, "'%s' invalid FID format", opt.o_src);
1676 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1677 !fid_is_file(&new_fid)) {
1679 CT_ERROR(rc, "'%s' invalid FID format", opt.o_dst);
1683 rc = ct_rebind_one(&old_fid, &new_fid);
1688 /* o_src is a list file */
1689 rc = ct_rebind_list(opt.o_src);
1694 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1699 struct dirent ent, *cookie = NULL;
1703 dir = opendir(dirpath);
1706 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1710 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1712 /* end of directory.
1713 * rc is 0 and seqmax contains the max value. */
1716 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1719 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1720 CT_TRACE("'%s' has an unexpected dirname format, "
1721 "skip entry", ent.d_name);
1724 if (sub_seq > *sub_seqmax)
1725 *sub_seqmax = sub_seq;
1728 CT_ERROR(rc, "cannot readdir_r '%s'", dirpath);
1735 static int ct_max_sequence(void)
1738 char path[PATH_MAX];
1742 strlcpy(path, opt.o_hsm_root, sizeof(path));
1743 /* FID sequence is stored in top-level directory names:
1744 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1746 for (i = 0; i < 4; i++) {
1749 rc = ct_dir_level_max(path, &subseq);
1752 seq |= ((__u64)subseq << ((3 - i) * 16));
1753 path_len = strlen(path);
1754 rc = snprintf(path + path_len, sizeof(path) - path_len,
1756 if (rc >= (sizeof(path) - path_len))
1758 path[sizeof(path) - 1] = '\0';
1761 printf("max_sequence: "LPX64"\n", seq);
1766 static void handler(int signal)
1768 psignal(signal, "exiting");
1769 /* If we don't clean up upon interrupt, umount thinks there's a ref
1770 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1771 * does successfully unmount and the mount is actually gone, but the
1772 * mtab entry remains. So this just makes mtab happier. */
1773 llapi_hsm_copytool_unregister(&ctdata);
1775 /* Also remove fifo upon signal as during normal/error exit */
1776 if (opt.o_event_fifo != NULL)
1777 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1781 /* Daemon waits for messages from the kernel; run it in the background. */
1782 static int ct_run(void)
1786 if (opt.o_daemonize) {
1790 CT_ERROR(rc, "cannot daemonize");
1795 setbuf(stdout, NULL);
1797 if (opt.o_event_fifo != NULL) {
1798 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
1800 CT_ERROR(rc, "failed to register event fifo");
1803 llapi_error_callback_set(llapi_hsm_log_error);
1806 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
1808 opt.o_archive_id, 0);
1810 CT_ERROR(rc, "cannot start copytool interface");
1814 signal(SIGINT, handler);
1815 signal(SIGTERM, handler);
1818 struct hsm_action_list *hal;
1819 struct hsm_action_item *hai;
1823 CT_TRACE("waiting for message from kernel");
1825 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1826 if (rc == -ESHUTDOWN) {
1827 CT_TRACE("shutting down");
1829 } else if (rc < 0) {
1830 CT_WARN("cannot receive action list: %s",
1833 if (opt.o_abort_on_error)
1839 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1840 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1842 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1844 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
1845 hal->hal_fsname, fs_name);
1847 if (opt.o_abort_on_error)
1853 hai = hai_first(hal);
1854 while (++i <= hal->hal_count) {
1855 if ((char *)hai - (char *)hal > msgsize) {
1858 "'%s' item %d past end of message!",
1863 rc = ct_process_item_async(hai, hal->hal_flags);
1865 CT_ERROR(rc, "'%s' item %d process",
1867 if (opt.o_abort_on_error && err_major)
1869 hai = hai_next(hai);
1872 if (opt.o_abort_on_error && err_major)
1876 llapi_hsm_copytool_unregister(&ctdata);
1877 if (opt.o_event_fifo != NULL)
1878 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1883 static int ct_setup(void)
1887 /* set llapi message level */
1888 llapi_msg_set_level(opt.o_verbose);
1890 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1893 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1897 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1899 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
1904 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
1905 if (opt.o_mnt_fd < 0) {
1907 CT_ERROR(rc, "cannot open mount point at '%s'",
1915 static int ct_cleanup(void)
1919 if (opt.o_mnt_fd >= 0) {
1920 rc = close(opt.o_mnt_fd);
1923 CT_ERROR(rc, "cannot close mount point");
1932 CT_ERROR(rc, "cannot close archive root directory");
1940 int main(int argc, char **argv)
1944 strlcpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1945 rc = ct_parseopts(argc, argv);
1947 CT_WARN("try '%s --help' for more information", cmd_name);
1955 switch (opt.o_action) {
1957 rc = ct_import_recurse(opt.o_src);
1963 rc = ct_max_sequence();
1970 if (opt.o_action != CA_MAXSEQ)
1971 CT_TRACE("process finished, errs: %d major, %d minor,"
1972 " rc=%d (%s)", err_major, err_minor, rc,