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, 2016, 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.
55 #include <sys/xattr.h>
56 #include <sys/syscall.h>
57 #include <sys/types.h>
59 #include <libcfs/util/string.h>
60 #include <lustre/lustre_fid.h>
61 #include <lustre/lustreapi.h>
63 /* Progress reporting period */
64 #define REPORT_INTERVAL_DEFAULT 30
65 /* HSM hash subdir permissions */
66 #define DIR_PERM S_IRWXU
67 /* HSM hash file permissions */
68 #define FILE_PERM (S_IRUSR | S_IWUSR)
70 #define ONE_MB 0x100000
73 # define NSEC_PER_SEC 1000000000UL
91 int o_archive_id[LL_HSM_MAX_ARCHIVE];
93 unsigned long long o_bandwidth;
95 enum ct_action o_action;
100 char *o_src; /* for import, or rebind */
101 char *o_dst; /* for import, or rebind */
104 /* everything else is zeroed */
105 struct options opt = {
108 .o_verbose = LLAPI_MSG_INFO,
110 .o_report_int = REPORT_INTERVAL_DEFAULT,
111 .o_chunk_size = ONE_MB,
114 /* hsm_copytool_private will hold an open FD on the lustre mount point
115 * for us. Additionally open one on the archive FS root to make sure
116 * it doesn't drop out from under us (and remind the admin to shutdown
117 * the copytool before unmounting). */
119 static int arc_fd = -1;
121 static int err_major;
122 static int err_minor;
124 static char cmd_name[PATH_MAX];
125 static char fs_name[MAX_OBD_NAME + 1];
127 static struct hsm_copytool_private *ctdata;
129 static inline double ct_now(void)
133 gettimeofday(&tv, NULL);
135 return tv.tv_sec + 0.000001 * tv.tv_usec;
138 #define CT_ERROR(_rc, _format, ...) \
139 llapi_error(LLAPI_MSG_ERROR, _rc, \
140 "%f %s[%ld]: "_format, \
141 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
143 #define CT_DEBUG(_format, ...) \
144 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
145 "%f %s[%ld]: "_format, \
146 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
148 #define CT_WARN(_format, ...) \
149 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
150 "%f %s[%ld]: "_format, \
151 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
153 #define CT_TRACE(_format, ...) \
154 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
155 "%f %s[%ld]: "_format, \
156 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
158 static void usage(const char *name, int rc)
161 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
162 "The Lustre HSM Posix copy tool can be used as a daemon or "
163 "as a command line tool\n"
164 "The Lustre HSM daemon acts on action requests from Lustre\n"
165 "to copy files to and from an HSM archive system.\n"
166 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
167 "to an HSM mounted at a given hsm_root.\n"
168 " --daemon Daemon mode, run in background\n"
170 " --no-attr Don't copy file attributes\n"
171 " --no-shadow Don't create shadow namespace in archive\n"
172 " --no-xattr Don't copy file extended attributes\n"
173 "The Lustre HSM tool performs administrator-type actions\n"
174 "on a Lustre HSM archive.\n"
175 "This POSIX-flavored tool can link an existing HSM namespace\n"
176 "into a Lustre filesystem.\n"
178 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
179 " import an archived subtree from\n"
180 " <src> (FID or relative path to hsm_root) into the Lustre\n"
182 " <dst> (absolute path)\n"
183 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
184 " rebind an entry in the HSM to a new FID\n"
185 " <old_FID> old FID the HSM entry is bound to\n"
186 " <new_FID> new FID to bind the HSM entry to\n"
187 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
188 " perform the rebind operation for all FID in the list file\n"
189 " each line of <list_file> consists of <old_FID> <new_FID>\n"
190 " %s [options] --max-sequence <fsname>\n"
191 " return the max fid sequence of archived files\n"
192 " --abort-on-error Abort operation on major error\n"
193 " -A, --archive <#> Archive number (repeatable)\n"
194 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
196 " --dry-run Don't run, just show what would be done\n"
197 " -c, --chunk-size <sz> I/O size used during data copy\n"
198 " (unit can be used, default is MB)\n"
199 " -f, --event-fifo <path> Write events stream to fifo\n"
200 " -p, --hsm-root <path> Target HSM mount point\n"
201 " -q, --quiet Produce less verbose output\n"
202 " -u, --update-interval <s> Interval between progress reports sent\n"
204 " -v, --verbose Produce more verbose output\n",
205 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
210 static int ct_parseopts(int argc, char * const *argv)
212 struct option long_opts[] = {
213 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
214 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
215 {"archive", required_argument, NULL, 'A'},
216 {"bandwidth", required_argument, NULL, 'b'},
217 {"chunk-size", required_argument, NULL, 'c'},
218 {"chunk_size", required_argument, NULL, 'c'},
219 {"daemon", no_argument, &opt.o_daemonize, 1},
220 {"event-fifo", required_argument, NULL, 'f'},
221 {"event_fifo", required_argument, NULL, 'f'},
222 {"dry-run", no_argument, &opt.o_dry_run, 1},
223 {"help", no_argument, NULL, 'h'},
224 {"hsm-root", required_argument, NULL, 'p'},
225 {"hsm_root", required_argument, NULL, 'p'},
226 {"import", no_argument, NULL, 'i'},
227 {"max-sequence", no_argument, NULL, 'M'},
228 {"max_sequence", no_argument, NULL, 'M'},
229 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
230 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
231 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
232 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
233 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
234 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
235 {"quiet", no_argument, NULL, 'q'},
236 {"rebind", no_argument, NULL, 'r'},
237 {"update-interval", required_argument, NULL, 'u'},
238 {"update_interval", required_argument, NULL, 'u'},
239 {"verbose", no_argument, NULL, 'v'},
243 unsigned long long value;
244 unsigned long long unit;
247 while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
248 long_opts, NULL)) != -1) {
251 if ((opt.o_archive_cnt >= LL_HSM_MAX_ARCHIVE) ||
252 (atoi(optarg) >= LL_HSM_MAX_ARCHIVE)) {
254 CT_ERROR(rc, "archive number must be less"
255 "than %zu", LL_HSM_MAX_ARCHIVE);
258 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
261 case 'b': /* -b and -c have both a number with unit as arg */
264 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
266 CT_ERROR(rc, "bad value for -%c '%s'", c,
271 opt.o_chunk_size = value;
273 opt.o_bandwidth = value;
276 opt.o_event_fifo = optarg;
281 opt.o_action = CA_IMPORT;
284 opt.o_action = CA_MAXSEQ;
287 opt.o_hsm_root = optarg;
293 opt.o_action = CA_REBIND;
296 opt.o_report_int = atoi(optarg);
297 if (opt.o_report_int < 0) {
299 CT_ERROR(rc, "bad value for -%c '%s'", c,
314 switch (opt.o_action) {
316 /* src dst mount_point */
317 if (argc != optind + 3) {
319 CT_ERROR(rc, "--import requires 2 arguments");
322 opt.o_src = argv[optind++];
323 opt.o_dst = argv[optind++];
326 /* FID1 FID2 mount_point or FILE mount_point */
327 if (argc == optind + 2) {
328 opt.o_src = argv[optind++];
330 } else if (argc == optind + 3) {
331 opt.o_src = argv[optind++];
332 opt.o_dst = argv[optind++];
335 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
341 /* just mount point */
345 if (argc != optind + 1) {
347 CT_ERROR(rc, "no mount point specified");
351 opt.o_mnt = argv[optind];
354 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
355 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
357 if (opt.o_hsm_root == NULL) {
359 CT_ERROR(rc, "must specify a root directory for the backend");
363 if (opt.o_action == CA_IMPORT) {
364 if (opt.o_src && opt.o_src[0] == '/') {
367 "source path must be relative to HSM root");
371 if (opt.o_dst && opt.o_dst[0] != '/') {
373 CT_ERROR(rc, "destination path must be absolute");
382 static int ct_mkdir_p(const char *path)
395 while ((ptr = strchr(ptr, '/')) != NULL) {
397 rc = mkdir(saved, DIR_PERM);
399 if (rc < 0 && errno != EEXIST) {
401 CT_ERROR(rc, "cannot mkdir '%s'", path);
413 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
415 char lov_file[PATH_MAX];
416 char lov_buf[XATTR_SIZE_MAX];
417 struct lov_user_md *lum;
422 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
423 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
425 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
427 if (xattr_size < 0) {
429 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
433 lum = (struct lov_user_md *)lov_buf;
435 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
436 lum->lmm_magic == LOV_USER_MAGIC_V3) {
437 /* Set stripe_offset to -1 so that it is not interpreted as a
438 * hint on restore. */
439 lum->lmm_stripe_offset = -1;
442 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
445 CT_ERROR(rc, "cannot open '%s'", lov_file);
449 rc = write(fd, lum, xattr_size);
452 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
453 xattr_size, lov_file);
461 CT_ERROR(rc, "cannot close '%s'", lov_file);
473 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
475 char lov_file[PATH_MAX];
479 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
480 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
482 fd = open(lov_file, O_RDONLY);
484 CT_ERROR(errno, "cannot open '%s'", lov_file);
488 rc = read(fd, lovea, *lovea_size);
490 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
491 *lovea_size, lov_file);
502 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
503 const void *lovea, size_t lovea_size)
507 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
511 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
517 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
518 const char *dst, int src_fd, int dst_fd,
519 const struct hsm_action_item *hai, long hal_flags)
521 struct hsm_extent he;
522 __u64 offset = hai->hai_extent.offset;
526 __u64 write_total = 0;
527 __u64 length = hai->hai_extent.length;
528 time_t last_report_time;
530 double start_ct_now = ct_now();
531 /* Bandwidth Control */
534 time_t last_bw_print;
536 if (fstat(src_fd, &src_st) < 0) {
538 CT_ERROR(rc, "cannot stat '%s'", src);
542 if (!S_ISREG(src_st.st_mode)) {
544 CT_ERROR(rc, "'%s' is not a regular file", src);
548 if (hai->hai_extent.offset > (__u64)src_st.st_size) {
550 CT_ERROR(rc, "Trying to start reading past end (%ju > "
551 "%jd) of '%s' source file",
552 (uintmax_t)hai->hai_extent.offset,
553 (intmax_t)src_st.st_size, src);
557 if (fstat(dst_fd, &dst_st) < 0) {
559 CT_ERROR(rc, "cannot stat '%s'", dst);
563 if (!S_ISREG(dst_st.st_mode)) {
565 CT_ERROR(rc, "'%s' is not a regular file", dst);
569 /* Don't read beyond a given extent */
570 if (length > src_st.st_size - hai->hai_extent.offset)
571 length = src_st.st_size - hai->hai_extent.offset;
573 start_time = last_bw_print = last_report_time = time(NULL);
577 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
579 /* Action has been canceled or something wrong
580 * is happening. Stop copying data. */
581 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
588 buf = malloc(opt.o_chunk_size);
594 CT_TRACE("start copy of %ju bytes from '%s' to '%s'",
595 (uintmax_t)length, src, dst);
597 while (write_total < length) {
600 int chunk = (length - write_total > opt.o_chunk_size) ?
601 opt.o_chunk_size : length - write_total;
603 rsize = pread(src_fd, buf, chunk, offset);
610 CT_ERROR(rc, "cannot read from '%s'", src);
614 wsize = pwrite(dst_fd, buf, rsize, offset);
617 CT_ERROR(rc, "cannot write to '%s'", dst);
621 write_total += wsize;
625 /* sleep if needed, to honor bandwidth limits */
626 if (opt.o_bandwidth != 0) {
627 unsigned long long write_theory;
629 write_theory = (now - start_time) * opt.o_bandwidth;
631 if (write_theory < write_total) {
632 unsigned long long excess;
633 struct timespec delay;
635 excess = write_total - write_theory;
637 delay.tv_sec = excess / opt.o_bandwidth;
638 delay.tv_nsec = (excess % opt.o_bandwidth) *
639 NSEC_PER_SEC / opt.o_bandwidth;
641 if (now >= last_bw_print + opt.o_report_int) {
642 CT_TRACE("bandwith control: %lluB/s "
643 "excess=%llu sleep for "
645 opt.o_bandwidth, excess,
646 (long long)delay.tv_sec,
652 rc = nanosleep(&delay, &delay);
653 } while (rc < 0 && errno == EINTR);
655 CT_ERROR(errno, "delay for bandwidth "
656 "control failed to sleep: "
657 "residual=%lld.%09lds",
658 (long long)delay.tv_sec,
666 if (now >= last_report_time + opt.o_report_int) {
667 last_report_time = now;
668 CT_TRACE("%%%ju ", (uintmax_t)(100 * write_total / length));
669 /* only give the length of the write since the last
671 he.length = offset - he.offset;
672 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
674 /* Action has been canceled or something wrong
675 * is happening. Stop copying data. */
676 CT_ERROR(rc, "progress ioctl for copy"
677 " '%s'->'%s' failed", src, dst);
687 * truncate restored file
688 * size is taken from the archive this is done to support
689 * restore after a force release which leaves the file with the
690 * wrong size (can big bigger than the new size)
692 if ((hai->hai_action == HSMA_RESTORE) &&
693 (src_st.st_size < dst_st.st_size)) {
695 * make sure the file is on disk before reporting success.
697 rc = ftruncate(dst_fd, src_st.st_size);
700 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
701 dst, (intmax_t)src_st.st_size);
709 CT_TRACE("copied %ju bytes in %f seconds",
710 (uintmax_t)length, ct_now() - start_ct_now);
715 /* Copy file attributes from file src to file dest */
716 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
720 struct timeval times[2];
723 if (fstat(src_fd, &st) < 0) {
725 CT_ERROR(rc, "cannot stat '%s'", src);
729 times[0].tv_sec = st.st_atime;
730 times[0].tv_usec = 0;
731 times[1].tv_sec = st.st_mtime;
732 times[1].tv_usec = 0;
733 if (fchmod(dst_fd, st.st_mode) < 0 ||
734 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
735 futimes(dst_fd, times) < 0) {
737 CT_ERROR(rc, "cannot set attributes of '%s'", src);
744 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
745 int dst_fd, bool is_restore)
747 char list[XATTR_LIST_MAX];
748 char value[XATTR_SIZE_MAX];
753 list_len = flistxattr(src_fd, list, sizeof(list));
758 while (name < list + list_len) {
759 rc = fgetxattr(src_fd, name, value, sizeof(value));
763 /* when we restore, we do not restore lustre xattr */
765 (strncmp(XATTR_TRUSTED_PREFIX, name,
766 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
767 rc = fsetxattr(dst_fd, name, value, rc, 0);
768 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
769 name, dst, rc, strerror(errno));
770 /* lustre.* attrs aren't supported on other FS's */
771 if (rc < 0 && errno != EOPNOTSUPP) {
773 CT_ERROR(rc, "cannot set extended attribute"
779 name += strlen(name) + 1;
785 static int ct_path_lustre(char *buf, int sz, const char *mnt,
786 const lustre_fid *fid)
788 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
789 dot_lustre_name, PFID(fid));
792 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
793 const lustre_fid *fid)
795 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
796 DFID_NOBRACE, archive_dir,
797 (fid)->f_oid & 0xFFFF,
798 (fid)->f_oid >> 16 & 0xFFFF,
799 (unsigned int)((fid)->f_seq & 0xFFFF),
800 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
801 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
802 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
806 static bool ct_is_retryable(int err)
808 return err == -ETIMEDOUT;
811 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
812 const struct hsm_action_item *hai,
813 int mdt_index, int open_flags)
818 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
821 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
822 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
828 static int ct_begin(struct hsm_copyaction_private **phcp,
829 const struct hsm_action_item *hai)
831 /* Restore takes specific parameters. Call the same function w/ default
832 * values for all other operations. */
833 return ct_begin_restore(phcp, hai, -1, 0);
836 static int ct_fini(struct hsm_copyaction_private **phcp,
837 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
839 struct hsm_copyaction_private *hcp;
843 CT_TRACE("Action completed, notifying coordinator "
844 "cookie=%#jx, FID="DFID", hp_flags=%d err=%d",
845 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid),
848 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
850 if (phcp == NULL || *phcp == NULL) {
851 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
853 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
860 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
861 if (rc == -ECANCELED)
862 CT_ERROR(rc, "completed action on '%s' has been canceled: "
863 "cookie=%#jx, FID="DFID, lstr,
864 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid));
866 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
868 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
874 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
876 struct hsm_copyaction_private *hcp = NULL;
878 char dst[PATH_MAX] = "";
881 bool rename_needed = false;
887 rc = ct_begin(&hcp, hai);
891 /* we fill archive so:
893 * destination = lustre FID
895 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
896 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
897 if (hai->hai_extent.length == -1) {
898 /* whole file, write it to tmp location and atomically
899 * replace old archived file */
900 strlcat(dst, "_tmp", sizeof(dst));
901 /* we cannot rely on the same test because ct_copy_data()
902 * updates hai_extent.length */
903 rename_needed = true;
906 CT_TRACE("archiving '%s' to '%s'", src, dst);
913 rc = ct_mkdir_p(dst);
915 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
919 src_fd = llapi_hsm_action_get_fd(hcp);
922 CT_ERROR(rc, "cannot open '%s' for read", src);
926 open_flags = O_WRONLY | O_NOFOLLOW;
927 /* If extent is specified, don't truncate an old archived copy */
928 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
930 dst_fd = open(dst, open_flags, FILE_PERM);
933 CT_ERROR(rc, "cannot open '%s' for write", dst);
937 /* saving stripe is not critical */
938 rc = ct_save_stripe(src_fd, src, dst);
940 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
943 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
945 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
952 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
956 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
958 /* attrs will remain on the MDS; no need to copy them, except possibly
959 for disaster recovery */
960 if (opt.o_copy_attrs) {
961 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
963 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
967 CT_TRACE("attr file for '%s' saved to archive '%s'",
971 /* xattrs will remain on the MDS; no need to copy them, except possibly
972 for disaster recovery */
973 if (opt.o_copy_xattrs) {
974 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
976 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
978 rcf = rcf ? rcf : rc;
980 CT_TRACE("xattr file for '%s' saved to archive '%s'",
984 if (rename_needed == true) {
985 char tmp_src[PATH_MAX];
986 char tmp_dst[PATH_MAX];
988 /* atomically replace old archived file */
989 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
991 rc = rename(dst, src);
994 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
997 /* rename lov file */
998 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
999 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
1000 rc = rename(tmp_dst, tmp_src);
1002 CT_ERROR(errno, "cannot rename '%s' to '%s'",
1006 if (opt.o_shadow_tree) {
1007 /* Create a namespace of softlinks that shadows the original
1008 * Lustre namespace. This will only be current at
1009 * time-of-archive (won't follow renames).
1010 * WARNING: release won't kill these links; a manual
1011 * cleanup of dead links would be required.
1014 long long recno = -1;
1020 sprintf(buf, DFID, PFID(&hai->hai_fid));
1021 sprintf(src, "%s/shadow/", opt.o_hsm_root);
1023 ptr = opt.o_hsm_root;
1025 (*ptr++ == '/') ? depth-- : 0;
1027 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
1028 sizeof(src) - strlen(src), &recno, &linkno);
1030 CT_ERROR(rc, "cannot get FID of '%s'", buf);
1031 rcf = rcf ? rcf : rc;
1035 /* Figure out how many parent dirs to symlink back */
1038 (*ptr++ == '/') ? depth++ : 0;
1043 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1045 if (ct_mkdir_p(src)) {
1046 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1047 rcf = rcf ? rcf : -errno;
1050 /* symlink already exists ? */
1051 sz = readlink(src, buf, sizeof(buf));
1052 /* detect truncation */
1053 if (sz == sizeof(buf)) {
1054 rcf = rcf ? rcf : -E2BIG;
1055 CT_ERROR(rcf, "readlink '%s' truncated", src);
1060 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1061 if (unlink(src) && errno != ENOENT) {
1063 "cannot unlink symlink '%s'",
1065 rcf = rcf ? rcf : -errno;
1067 /* unlink old symlink done */
1068 CT_TRACE("remove old symlink '%s' pointing"
1069 " to '%s'", src, buf);
1072 /* symlink already ok */
1073 CT_TRACE("symlink '%s' already pointing"
1074 " to '%s'", src, dst);
1079 if (symlink(dst, src)) {
1080 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1082 rcf = rcf ? rcf : -errno;
1085 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1097 if (ct_is_retryable(rc))
1098 hp_flags |= HP_FLAG_RETRY;
1109 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1114 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1116 struct hsm_copyaction_private *hcp = NULL;
1119 char lov_buf[XATTR_SIZE_MAX];
1120 size_t lov_size = sizeof(lov_buf);
1129 /* we fill lustre so:
1130 * source = lustre FID in the backend
1131 * destination = data FID = volatile file
1134 /* build backend file name from released file FID */
1135 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1137 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1140 CT_ERROR(rc, "cannot get mdt index "DFID"",
1141 PFID(&hai->hai_fid));
1144 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1145 * dependency on the structure format. */
1146 rc = ct_load_stripe(src, lov_buf, &lov_size);
1148 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1149 src, strerror(-rc));
1152 open_flags |= O_LOV_DELAY_CREATE;
1156 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1160 /* get the FID of the volatile file */
1161 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1163 CT_ERROR(rc, "restoring "DFID
1164 ", cannot get FID of created volatile file",
1165 PFID(&hai->hai_fid));
1169 /* build volatile "file name", for messages */
1170 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1172 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1174 if (opt.o_dry_run) {
1179 src_fd = open(src, O_RDONLY | O_NOATIME | O_NOFOLLOW);
1182 CT_ERROR(rc, "cannot open '%s' for read", src);
1186 dst_fd = llapi_hsm_action_get_fd(hcp);
1189 CT_ERROR(rc, "cannot open '%s' for write", dst);
1194 /* the layout cannot be allocated through .fid so we have to
1195 * restore a layout */
1196 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1198 CT_ERROR(rc, "cannot restore file striping info"
1199 " for '%s' from '%s'", dst, src);
1205 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1207 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1210 if (ct_is_retryable(rc))
1211 hp_flags |= HP_FLAG_RETRY;
1215 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1218 rc = ct_fini(&hcp, hai, hp_flags, rc);
1220 /* object swaping is done by cdt at copy end, so close of volatile file
1221 * cannot be done before */
1231 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1233 struct hsm_copyaction_private *hcp = NULL;
1237 rc = ct_begin(&hcp, hai);
1241 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1243 CT_TRACE("removing file '%s'", dst);
1245 if (opt.o_dry_run) {
1253 CT_ERROR(rc, "cannot unlink '%s'", dst);
1258 strlcat(dst, ".lov", sizeof(dst));
1262 CT_ERROR(rc, "cannot unlink '%s'", dst);
1268 rc = ct_fini(&hcp, hai, 0, rc);
1273 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1277 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1278 /* Print the original path */
1280 char path[PATH_MAX];
1281 long long recno = -1;
1284 sprintf(fid, DFID, PFID(&hai->hai_fid));
1285 CT_TRACE("'%s' action %s reclen %d, cookie=%#jx",
1286 fid, hsm_copytool_action2name(hai->hai_action),
1287 hai->hai_len, (uintmax_t)hai->hai_cookie);
1288 rc = llapi_fid2path(opt.o_mnt, fid, path,
1289 sizeof(path), &recno, &linkno);
1291 CT_ERROR(rc, "cannot get path of FID %s", fid);
1293 CT_TRACE("processing file '%s'", path);
1296 switch (hai->hai_action) {
1297 /* set err_major, minor inside these functions */
1299 rc = ct_archive(hai, hal_flags);
1302 rc = ct_restore(hai, hal_flags);
1305 rc = ct_remove(hai, hal_flags);
1308 CT_TRACE("cancel not implemented for file system '%s'",
1310 /* Don't report progress to coordinator for this cookie:
1311 * the copy function will get ECANCELED when reporting
1318 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1321 ct_fini(NULL, hai, 0, rc);
1329 struct hsm_action_item *hai;
1332 static void *ct_thread(void *data)
1334 struct ct_th_data *cttd = data;
1337 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1341 pthread_exit((void *)(intptr_t)rc);
1344 static int ct_process_item_async(const struct hsm_action_item *hai,
1347 pthread_attr_t attr;
1349 struct ct_th_data *data;
1352 data = malloc(sizeof(*data));
1356 data->hai = malloc(hai->hai_len);
1357 if (data->hai == NULL) {
1362 memcpy(data->hai, hai, hai->hai_len);
1363 data->hal_flags = hal_flags;
1365 rc = pthread_attr_init(&attr);
1367 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1374 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1376 rc = pthread_create(&thread, &attr, ct_thread, data);
1378 CT_ERROR(rc, "cannot create thread for '%s' service",
1381 pthread_attr_destroy(&attr);
1385 static int ct_import_one(const char *src, const char *dst)
1387 char newarc[PATH_MAX];
1392 CT_TRACE("importing '%s' from '%s'", dst, src);
1394 if (stat(src, &st) < 0) {
1396 CT_ERROR(rc, "cannot stat '%s'", src);
1403 rc = llapi_hsm_import(dst,
1404 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1405 &st, 0, 0, 0, 0, NULL, &fid);
1407 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1411 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1413 rc = ct_mkdir_p(newarc);
1415 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1421 /* Lots of choices now: mv, ln, ln -s ? */
1422 rc = link(src, newarc); /* hardlink */
1425 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1429 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1434 static char *path_concat(const char *dirname, const char *basename)
1439 rc = asprintf(&result, "%s/%s", dirname, basename);
1446 static int ct_import_fid(const lustre_fid *import_fid)
1448 char fid_path[PATH_MAX];
1451 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1452 rc = access(fid_path, F_OK);
1453 if (rc == 0 || errno != ENOENT) {
1454 rc = (errno == 0) ? -EEXIST : -errno;
1455 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1459 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1462 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1464 return ct_import_one(fid_path, opt.o_dst);
1467 static int ct_import_recurse(const char *relpath)
1470 struct dirent ent, *cookie = NULL;
1471 char *srcpath, *newpath;
1472 lustre_fid import_fid;
1475 if (relpath == NULL)
1478 /* Is relpath a FID? In which case SFID should expand to three
1480 rc = sscanf(relpath, SFID, RFID(&import_fid));
1482 return ct_import_fid(&import_fid);
1484 srcpath = path_concat(opt.o_hsm_root, relpath);
1485 if (srcpath == NULL) {
1490 dir = opendir(srcpath);
1492 /* Not a dir, or error */
1493 if (errno == ENOTDIR) {
1494 /* Single regular file case, treat o_dst as absolute
1496 rc = ct_import_one(srcpath, opt.o_dst);
1499 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1508 rc = readdir_r(dir, &ent, &cookie);
1511 CT_ERROR(rc, "cannot readdir_r '%s'", relpath);
1514 } else if ((rc == 0) && (cookie == NULL)) {
1515 /* end of directory */
1519 if (!strcmp(ent.d_name, ".") ||
1520 !strcmp(ent.d_name, ".."))
1523 /* New relative path */
1524 newpath = path_concat(relpath, ent.d_name);
1525 if (newpath == NULL) {
1531 if (ent.d_type == DT_DIR) {
1532 rc = ct_import_recurse(newpath);
1537 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1538 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1539 /* Make the target dir in the Lustre fs */
1540 rc = ct_mkdir_p(dst);
1542 /* Import the file */
1543 rc = ct_import_one(src, dst);
1545 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1551 CT_ERROR(rc, "cannot import '%s'", newpath);
1552 if (err_major && opt.o_abort_on_error) {
1566 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1572 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1574 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1575 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1577 if (!opt.o_dry_run) {
1579 if (rename(src, dst)) {
1581 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1584 /* rename lov file */
1585 strlcat(src, ".lov", sizeof(src));
1586 strlcat(dst, ".lov", sizeof(dst));
1587 if (rename(src, dst))
1588 CT_ERROR(errno, "cannot rename '%s' to '%s'", src, dst);
1594 static bool fid_is_file(lustre_fid *fid)
1596 return fid_is_norm(fid) || fid_is_igif(fid);
1599 static bool should_ignore_line(const char *line)
1603 for (i = 0; line[i] != '\0'; i++) {
1604 if (isspace(line[i]))
1606 else if (line[i] == '#')
1615 static int ct_rebind_list(const char *list)
1621 size_t line_size = 0;
1622 unsigned int nl = 0;
1623 unsigned int ok = 0;
1625 filp = fopen(list, "r");
1628 CT_ERROR(rc, "cannot open '%s'", list);
1632 /* each line consists of 2 FID */
1633 while ((r = getline(&line, &line_size, filp)) != -1) {
1637 /* Ignore empty and commented out ('#...') lines. */
1638 if (should_ignore_line(line))
1643 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1644 if (rc != 6 || !fid_is_file(&old_fid) ||
1645 !fid_is_file(&new_fid)) {
1647 "'%s' FID expected near '%s', line %u",
1653 if (ct_rebind_one(&old_fid, &new_fid))
1664 /* return 0 if all rebinds were successful */
1665 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1667 return ok == nl ? 0 : -1;
1670 static int ct_rebind(void)
1678 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1679 !fid_is_file(&old_fid)) {
1681 CT_ERROR(rc, "'%s' invalid FID format", opt.o_src);
1685 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1686 !fid_is_file(&new_fid)) {
1688 CT_ERROR(rc, "'%s' invalid FID format", opt.o_dst);
1692 rc = ct_rebind_one(&old_fid, &new_fid);
1697 /* o_src is a list file */
1698 rc = ct_rebind_list(opt.o_src);
1703 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1708 struct dirent ent, *cookie = NULL;
1712 dir = opendir(dirpath);
1715 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1719 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1721 /* end of directory.
1722 * rc is 0 and seqmax contains the max value. */
1725 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1728 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1729 CT_TRACE("'%s' has an unexpected dirname format, "
1730 "skip entry", ent.d_name);
1733 if (sub_seq > *sub_seqmax)
1734 *sub_seqmax = sub_seq;
1737 CT_ERROR(rc, "cannot readdir_r '%s'", dirpath);
1744 static int ct_max_sequence(void)
1747 char path[PATH_MAX];
1751 strlcpy(path, opt.o_hsm_root, sizeof(path));
1752 /* FID sequence is stored in top-level directory names:
1753 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1755 for (i = 0; i < 4; i++) {
1758 rc = ct_dir_level_max(path, &subseq);
1761 seq |= ((__u64)subseq << ((3 - i) * 16));
1762 path_len = strlen(path);
1763 rc = snprintf(path + path_len, sizeof(path) - path_len,
1765 if (rc >= (sizeof(path) - path_len))
1767 path[sizeof(path) - 1] = '\0';
1770 printf("max_sequence: %#jx\n", (uintmax_t)seq);
1775 static void handler(int signal)
1777 psignal(signal, "exiting");
1778 /* If we don't clean up upon interrupt, umount thinks there's a ref
1779 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1780 * does successfully unmount and the mount is actually gone, but the
1781 * mtab entry remains. So this just makes mtab happier. */
1782 llapi_hsm_copytool_unregister(&ctdata);
1784 /* Also remove fifo upon signal as during normal/error exit */
1785 if (opt.o_event_fifo != NULL)
1786 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1790 /* Daemon waits for messages from the kernel; run it in the background. */
1791 static int ct_run(void)
1793 struct sigaction cleanup_sigaction;
1796 if (opt.o_daemonize) {
1800 CT_ERROR(rc, "cannot daemonize");
1805 setbuf(stdout, NULL);
1807 if (opt.o_event_fifo != NULL) {
1808 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
1810 CT_ERROR(rc, "failed to register event fifo");
1813 llapi_error_callback_set(llapi_hsm_log_error);
1816 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
1818 opt.o_archive_id, 0);
1820 CT_ERROR(rc, "cannot start copytool interface");
1824 memset(&cleanup_sigaction, 0, sizeof(cleanup_sigaction));
1825 cleanup_sigaction.sa_handler = handler;
1826 sigemptyset(&cleanup_sigaction.sa_mask);
1827 sigaction(SIGINT, &cleanup_sigaction, NULL);
1828 sigaction(SIGTERM, &cleanup_sigaction, NULL);
1831 struct hsm_action_list *hal;
1832 struct hsm_action_item *hai;
1836 CT_TRACE("waiting for message from kernel");
1838 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1839 if (rc == -ESHUTDOWN) {
1840 CT_TRACE("shutting down");
1842 } else if (rc < 0) {
1843 CT_WARN("cannot receive action list: %s",
1846 if (opt.o_abort_on_error)
1852 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1853 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1855 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1857 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
1858 hal->hal_fsname, fs_name);
1860 if (opt.o_abort_on_error)
1866 hai = hai_first(hal);
1867 while (++i <= hal->hal_count) {
1868 if ((char *)hai - (char *)hal > msgsize) {
1871 "'%s' item %d past end of message!",
1876 rc = ct_process_item_async(hai, hal->hal_flags);
1878 CT_ERROR(rc, "'%s' item %d process",
1880 if (opt.o_abort_on_error && err_major)
1882 hai = hai_next(hai);
1885 if (opt.o_abort_on_error && err_major)
1889 llapi_hsm_copytool_unregister(&ctdata);
1890 if (opt.o_event_fifo != NULL)
1891 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1896 static int ct_setup(void)
1900 /* set llapi message level */
1901 llapi_msg_set_level(opt.o_verbose);
1903 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1906 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1910 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1912 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
1917 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
1918 if (opt.o_mnt_fd < 0) {
1920 CT_ERROR(rc, "cannot open mount point at '%s'",
1928 static int ct_cleanup(void)
1932 if (opt.o_mnt_fd >= 0) {
1933 rc = close(opt.o_mnt_fd);
1936 CT_ERROR(rc, "cannot close mount point");
1945 CT_ERROR(rc, "cannot close archive root directory");
1953 int main(int argc, char **argv)
1957 strlcpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1958 rc = ct_parseopts(argc, argv);
1960 CT_WARN("try '%s --help' for more information", cmd_name);
1968 switch (opt.o_action) {
1970 rc = ct_import_recurse(opt.o_src);
1976 rc = ct_max_sequence();
1983 if (opt.o_action != CA_MAXSEQ)
1984 CT_TRACE("process finished, errs: %d major, %d minor,"
1985 " rc=%d (%s)", err_major, err_minor, rc,