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
27 /* HSM copytool program for POSIX filesystem-based HSM's.
29 * An HSM copytool daemon acts on action requests from Lustre to copy files
30 * to and from an HSM archive system. This one in particular makes regular
31 * POSIX filesystem calls to a given path, where an HSM is presumably mounted.
33 * This particular tool can also import an existing HSM archive.
48 #include <sys/xattr.h>
49 #include <sys/syscall.h>
50 #include <sys/types.h>
51 #include <lustre/lustre_idl.h>
52 #include <lustre/lustreapi.h>
54 /* Progress reporting period */
55 #define REPORT_INTERVAL_DEFAULT 30
56 /* HSM hash subdir permissions */
57 #define DIR_PERM S_IRWXU
58 /* HSM hash file permissions */
59 #define FILE_PERM (S_IRUSR | S_IWUSR)
61 #define ONE_MB 0x100000
64 # define NSEC_PER_SEC 1000000000UL
67 /* copytool uses a 32b bitmask field to register with kuc
68 * archive num = 0 => all
69 * archive num from 1 to 32
71 #define MAX_ARCHIVE_CNT (sizeof(__u32) * 8)
88 int o_archive_id[MAX_ARCHIVE_CNT];
90 unsigned long long o_bandwidth;
92 enum ct_action o_action;
97 char *o_src; /* for import, or rebind */
98 char *o_dst; /* for import, or rebind */
101 /* everything else is zeroed */
102 struct options opt = {
105 .o_verbose = LLAPI_MSG_INFO,
107 .o_report_int = REPORT_INTERVAL_DEFAULT,
108 .o_chunk_size = ONE_MB,
111 /* hsm_copytool_private will hold an open FD on the lustre mount point
112 * for us. Additionally open one on the archive FS root to make sure
113 * it doesn't drop out from under us (and remind the admin to shutdown
114 * the copytool before unmounting). */
116 static int arc_fd = -1;
118 static int err_major;
119 static int err_minor;
121 static char cmd_name[PATH_MAX];
122 static char fs_name[MAX_OBD_NAME + 1];
124 static struct hsm_copytool_private *ctdata;
126 static inline double ct_now(void)
130 gettimeofday(&tv, NULL);
132 return tv.tv_sec + 0.000001 * tv.tv_usec;
135 #define CT_ERROR(_rc, _format, ...) \
136 llapi_error(LLAPI_MSG_ERROR, _rc, \
137 "%f %s[%ld]: "_format, \
138 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
140 #define CT_DEBUG(_format, ...) \
141 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
142 "%f %s[%ld]: "_format, \
143 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
145 #define CT_WARN(_format, ...) \
146 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
147 "%f %s[%ld]: "_format, \
148 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
150 #define CT_TRACE(_format, ...) \
151 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
152 "%f %s[%ld]: "_format, \
153 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
155 static void usage(const char *name, int rc)
158 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
159 "The Lustre HSM Posix copy tool can be used as a daemon or "
160 "as a command line tool\n"
161 "The Lustre HSM daemon acts on action requests from Lustre\n"
162 "to copy files to and from an HSM archive system.\n"
163 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
164 "to an HSM mounted at a given hsm_root.\n"
165 " --daemon Daemon mode, run in background\n"
167 " --no-attr Don't copy file attributes\n"
168 " --no-shadow Don't create shadow namespace in archive\n"
169 " --no-xattr Don't copy file extended attributes\n"
170 "The Lustre HSM tool performs administrator-type actions\n"
171 "on a Lustre HSM archive.\n"
172 "This POSIX-flavored tool can link an existing HSM namespace\n"
173 "into a Lustre filesystem.\n"
175 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
176 " import an archived subtree from\n"
177 " <src> (FID or relative path to hsm_root) into the Lustre\n"
179 " <dst> (absolute path)\n"
180 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
181 " rebind an entry in the HSM to a new FID\n"
182 " <old_FID> old FID the HSM entry is bound to\n"
183 " <new_FID> new FID to bind the HSM entry to\n"
184 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
185 " perform the rebind operation for all FID in the list file\n"
186 " each line of <list_file> consists of <old_FID> <new_FID>\n"
187 " %s [options] --max-sequence <fsname>\n"
188 " return the max fid sequence of archived files\n"
189 " --abort-on-error Abort operation on major error\n"
190 " -A, --archive <#> Archive number (repeatable)\n"
191 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
193 " --dry-run Don't run, just show what would be done\n"
194 " -c, --chunk-size <sz> I/O size used during data copy\n"
195 " (unit can be used, default is MB)\n"
196 " -f, --event-fifo <path> Write events stream to fifo\n"
197 " -p, --hsm-root <path> Target HSM mount point\n"
198 " -q, --quiet Produce less verbose output\n"
199 " -u, --update-interval <s> Interval between progress reports sent\n"
201 " -v, --verbose Produce more verbose output\n",
202 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
207 static int ct_parseopts(int argc, char * const *argv)
209 struct option long_opts[] = {
210 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
211 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
212 {"archive", required_argument, NULL, 'A'},
213 {"bandwidth", required_argument, NULL, 'b'},
214 {"chunk-size", required_argument, NULL, 'c'},
215 {"chunk_size", required_argument, NULL, 'c'},
216 {"daemon", no_argument, &opt.o_daemonize, 1},
217 {"event-fifo", required_argument, NULL, 'f'},
218 {"event_fifo", required_argument, NULL, 'f'},
219 {"dry-run", no_argument, &opt.o_dry_run, 1},
220 {"help", no_argument, NULL, 'h'},
221 {"hsm-root", required_argument, NULL, 'p'},
222 {"hsm_root", required_argument, NULL, 'p'},
223 {"import", no_argument, NULL, 'i'},
224 {"max-sequence", no_argument, NULL, 'M'},
225 {"max_sequence", no_argument, NULL, 'M'},
226 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
227 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
228 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
229 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
230 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
231 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
232 {"quiet", no_argument, NULL, 'q'},
233 {"rebind", no_argument, NULL, 'r'},
234 {"update-interval", required_argument, NULL, 'u'},
235 {"update_interval", required_argument, NULL, 'u'},
236 {"verbose", no_argument, NULL, 'v'},
240 unsigned long long value;
241 unsigned long long unit;
244 while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
245 long_opts, NULL)) != -1) {
248 if ((opt.o_archive_cnt >= MAX_ARCHIVE_CNT) ||
249 (atoi(optarg) >= MAX_ARCHIVE_CNT)) {
251 CT_ERROR(rc, "archive number must be less"
252 "than %zu", MAX_ARCHIVE_CNT);
255 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
258 case 'b': /* -b and -c have both a number with unit as arg */
261 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
263 CT_ERROR(rc, "bad value for -%c '%s'", c,
268 opt.o_chunk_size = value;
270 opt.o_bandwidth = value;
273 opt.o_event_fifo = optarg;
278 opt.o_action = CA_IMPORT;
281 opt.o_action = CA_MAXSEQ;
284 opt.o_hsm_root = optarg;
290 opt.o_action = CA_REBIND;
293 opt.o_report_int = atoi(optarg);
294 if (opt.o_report_int < 0) {
296 CT_ERROR(rc, "bad value for -%c '%s'", c,
311 switch (opt.o_action) {
313 /* src dst mount_point */
314 if (argc != optind + 3) {
316 CT_ERROR(rc, "--import requires 2 arguments");
319 opt.o_src = argv[optind++];
320 opt.o_dst = argv[optind++];
323 /* FID1 FID2 mount_point or FILE mount_point */
324 if (argc == optind + 2) {
325 opt.o_src = argv[optind++];
327 } else if (argc == optind + 3) {
328 opt.o_src = argv[optind++];
329 opt.o_dst = argv[optind++];
332 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
338 /* just mount point */
342 if (argc != optind + 1) {
344 CT_ERROR(rc, "no mount point specified");
348 opt.o_mnt = argv[optind];
351 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
352 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
354 if (!opt.o_dry_run && opt.o_hsm_root == NULL) {
356 CT_ERROR(rc, "must specify a root directory for the backend");
360 if (opt.o_action == CA_IMPORT) {
361 if (opt.o_src && opt.o_src[0] == '/') {
364 "source path must be relative to HSM root");
368 if (opt.o_dst && opt.o_dst[0] != '/') {
370 CT_ERROR(rc, "destination path must be absolute");
379 static int ct_mkdir_p(const char *path)
392 while ((ptr = strchr(ptr, '/')) != NULL) {
394 rc = mkdir(saved, DIR_PERM);
396 if (rc < 0 && errno != EEXIST) {
398 CT_ERROR(rc, "cannot mkdir '%s'", path);
410 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
412 char lov_file[PATH_MAX];
413 char lov_buf[XATTR_SIZE_MAX];
414 struct lov_user_md *lum;
419 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
420 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
422 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
424 if (xattr_size < 0) {
426 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
430 lum = (struct lov_user_md *)lov_buf;
432 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
433 lum->lmm_magic == LOV_USER_MAGIC_V3) {
434 /* Set stripe_offset to -1 so that it is not interpreted as a
435 * hint on restore. */
436 lum->lmm_stripe_offset = -1;
439 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
442 CT_ERROR(rc, "cannot open '%s'", lov_file);
446 rc = write(fd, lum, xattr_size);
449 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
450 xattr_size, lov_file);
458 CT_ERROR(rc, "cannot close '%s'", lov_file);
470 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
472 char lov_file[PATH_MAX];
476 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
477 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
479 fd = open(lov_file, O_RDONLY);
481 CT_ERROR(errno, "cannot open '%s'", lov_file);
485 rc = read(fd, lovea, *lovea_size);
487 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
488 *lovea_size, lov_file);
499 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
500 const void *lovea, size_t lovea_size)
504 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
508 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
514 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
515 const char *dst, int src_fd, int dst_fd,
516 const struct hsm_action_item *hai, long hal_flags)
518 struct hsm_extent he;
519 __u64 offset = hai->hai_extent.offset;
523 __u64 write_total = 0;
525 time_t last_report_time;
527 double start_ct_now = ct_now();
528 /* Bandwidth Control */
531 time_t last_bw_print;
533 if (fstat(src_fd, &src_st) < 0) {
535 CT_ERROR(rc, "cannot stat '%s'", src);
539 if (!S_ISREG(src_st.st_mode)) {
541 CT_ERROR(rc, "'%s' is not a regular file", src);
545 if (fstat(dst_fd, &dst_st) < 0) {
547 CT_ERROR(rc, "cannot stat '%s'", dst);
551 if (!S_ISREG(dst_st.st_mode)) {
553 CT_ERROR(rc, "'%s' is not a regular file", dst);
557 rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
561 "cannot seek for read to "LPU64" (len %jd) in '%s'",
562 hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
566 /* Don't read beyond a given extent */
567 length = min(hai->hai_extent.length, src_st.st_size);
569 start_time = last_bw_print = last_report_time = time(NULL);
573 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
575 /* Action has been canceled or something wrong
576 * is happening. Stop copying data. */
577 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
584 buf = malloc(opt.o_chunk_size);
590 CT_TRACE("start copy of "LPU64" bytes from '%s' to '%s'",
593 while (write_total < length) {
596 int chunk = (length - write_total > opt.o_chunk_size) ?
597 opt.o_chunk_size : length - write_total;
599 rsize = pread(src_fd, buf, chunk, offset);
606 CT_ERROR(rc, "cannot read from '%s'", src);
610 wsize = pwrite(dst_fd, buf, rsize, offset);
613 CT_ERROR(rc, "cannot write to '%s'", dst);
617 write_total += wsize;
621 /* sleep if needed, to honor bandwidth limits */
622 if (opt.o_bandwidth != 0) {
623 unsigned long long write_theory;
625 write_theory = (now - start_time) * opt.o_bandwidth;
627 if (write_theory < write_total) {
628 unsigned long long excess;
629 struct timespec delay;
631 excess = write_total - write_theory;
633 delay.tv_sec = excess / opt.o_bandwidth;
634 delay.tv_nsec = (excess % opt.o_bandwidth) *
635 NSEC_PER_SEC / opt.o_bandwidth;
637 if (now >= last_bw_print + opt.o_report_int) {
638 CT_TRACE("bandwith control: %lluB/s "
639 "excess=%llu sleep for "
641 opt.o_bandwidth, excess,
642 (long long)delay.tv_sec,
648 rc = nanosleep(&delay, &delay);
649 } while (rc < 0 && errno == EINTR);
651 CT_ERROR(errno, "delay for bandwidth "
652 "control failed to sleep: "
653 "residual=%lld.%09lds",
654 (long long)delay.tv_sec,
662 if (now >= last_report_time + opt.o_report_int) {
663 last_report_time = now;
664 CT_TRACE("%%"LPU64" ", 100 * write_total / length);
665 he.length = write_total;
666 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
668 /* Action has been canceled or something wrong
669 * is happening. Stop copying data. */
670 CT_ERROR(rc, "progress ioctl for copy"
671 " '%s'->'%s' failed", src, dst);
680 * truncate restored file
681 * size is taken from the archive this is done to support
682 * restore after a force release which leaves the file with the
683 * wrong size (can big bigger than the new size)
685 if ((hai->hai_action == HSMA_RESTORE) &&
686 (src_st.st_size < dst_st.st_size)) {
688 * make sure the file is on disk before reporting success.
690 rc = ftruncate(dst_fd, src_st.st_size);
693 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
694 dst, (intmax_t)src_st.st_size);
702 CT_TRACE("copied "LPU64" bytes in %f seconds",
703 length, ct_now() - start_ct_now);
708 /* Copy file attributes from file src to file dest */
709 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
713 struct timeval times[2];
716 if (fstat(src_fd, &st) < 0) {
718 CT_ERROR(rc, "cannot stat '%s'", src);
722 times[0].tv_sec = st.st_atime;
723 times[0].tv_usec = 0;
724 times[1].tv_sec = st.st_mtime;
725 times[1].tv_usec = 0;
726 if (fchmod(dst_fd, st.st_mode) < 0 ||
727 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
728 futimes(dst_fd, times) < 0) {
730 CT_ERROR(rc, "cannot set attributes of '%s'", src);
737 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
738 int dst_fd, bool is_restore)
740 char list[XATTR_LIST_MAX];
741 char value[XATTR_SIZE_MAX];
746 list_len = flistxattr(src_fd, list, sizeof(list));
751 while (name < list + list_len) {
752 rc = fgetxattr(src_fd, name, value, sizeof(value));
756 /* when we restore, we do not restore lustre xattr */
758 (strncmp(XATTR_TRUSTED_PREFIX, name,
759 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
760 rc = fsetxattr(dst_fd, name, value, rc, 0);
761 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
762 name, dst, rc, strerror(errno));
763 /* lustre.* attrs aren't supported on other FS's */
764 if (rc < 0 && errno != EOPNOTSUPP) {
766 CT_ERROR(rc, "cannot set extended attribute"
772 name += strlen(name) + 1;
778 static int ct_path_lustre(char *buf, int sz, const char *mnt,
779 const lustre_fid *fid)
781 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
782 dot_lustre_name, PFID(fid));
785 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
786 const lustre_fid *fid)
788 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
789 DFID_NOBRACE, archive_dir,
790 (fid)->f_oid & 0xFFFF,
791 (fid)->f_oid >> 16 & 0xFFFF,
792 (unsigned int)((fid)->f_seq & 0xFFFF),
793 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
794 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
795 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
799 static bool ct_is_retryable(int err)
801 return err == -ETIMEDOUT;
804 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
805 const struct hsm_action_item *hai,
806 int mdt_index, int open_flags)
811 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
814 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
815 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
821 static int ct_begin(struct hsm_copyaction_private **phcp,
822 const struct hsm_action_item *hai)
824 /* Restore takes specific parameters. Call the same function w/ default
825 * values for all other operations. */
826 return ct_begin_restore(phcp, hai, -1, 0);
829 static int ct_fini(struct hsm_copyaction_private **phcp,
830 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
832 struct hsm_copyaction_private *hcp;
836 CT_TRACE("Action completed, notifying coordinator "
837 "cookie="LPX64", FID="DFID", hp_flags=%d err=%d",
838 hai->hai_cookie, PFID(&hai->hai_fid),
841 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
843 if (phcp == NULL || *phcp == NULL) {
844 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
846 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
853 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
854 if (rc == -ECANCELED)
855 CT_ERROR(rc, "completed action on '%s' has been canceled: "
856 "cookie="LPX64", FID="DFID, lstr, hai->hai_cookie,
857 PFID(&hai->hai_fid));
859 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
861 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
867 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
869 struct hsm_copyaction_private *hcp = NULL;
871 char dst[PATH_MAX] = "";
874 bool rename_needed = false;
880 rc = ct_begin(&hcp, hai);
884 /* we fill archive so:
886 * destination = lustre FID
888 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
889 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
890 if (hai->hai_extent.length == -1) {
891 /* whole file, write it to tmp location and atomically
892 * replace old archived file */
893 strlcat(dst, "_tmp", sizeof(dst));
894 /* we cannot rely on the same test because ct_copy_data()
895 * updates hai_extent.length */
896 rename_needed = true;
899 CT_TRACE("archiving '%s' to '%s'", src, dst);
906 rc = ct_mkdir_p(dst);
908 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
912 src_fd = llapi_hsm_action_get_fd(hcp);
915 CT_ERROR(rc, "cannot open '%s' for read", src);
919 open_flags = O_WRONLY | O_NOFOLLOW | O_NONBLOCK;
920 /* If extent is specified, don't truncate an old archived copy */
921 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
923 dst_fd = open(dst, open_flags, FILE_PERM);
926 CT_ERROR(rc, "cannot open '%s' for write", dst);
930 /* saving stripe is not critical */
931 rc = ct_save_stripe(src_fd, src, dst);
933 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
936 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
938 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
945 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
949 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
951 /* attrs will remain on the MDS; no need to copy them, except possibly
952 for disaster recovery */
953 if (opt.o_copy_attrs) {
954 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
956 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
960 CT_TRACE("attr file for '%s' saved to archive '%s'",
964 /* xattrs will remain on the MDS; no need to copy them, except possibly
965 for disaster recovery */
966 if (opt.o_copy_xattrs) {
967 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
969 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
971 rcf = rcf ? rcf : rc;
973 CT_TRACE("xattr file for '%s' saved to archive '%s'",
977 if (rename_needed == true) {
978 char tmp_src[PATH_MAX];
979 char tmp_dst[PATH_MAX];
981 /* atomically replace old archived file */
982 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
984 rc = rename(dst, src);
987 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
990 /* rename lov file */
991 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
992 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
993 rc = rename(tmp_dst, tmp_src);
995 CT_ERROR(errno, "cannot rename '%s' to '%s'",
999 if (opt.o_shadow_tree) {
1000 /* Create a namespace of softlinks that shadows the original
1001 * Lustre namespace. This will only be current at
1002 * time-of-archive (won't follow renames).
1003 * WARNING: release won't kill these links; a manual
1004 * cleanup of dead links would be required.
1007 long long recno = -1;
1013 sprintf(buf, DFID, PFID(&hai->hai_fid));
1014 sprintf(src, "%s/shadow/", opt.o_hsm_root);
1016 ptr = opt.o_hsm_root;
1018 (*ptr++ == '/') ? depth-- : 0;
1020 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
1021 sizeof(src) - strlen(src), &recno, &linkno);
1023 CT_ERROR(rc, "cannot get FID of '%s'", buf);
1024 rcf = rcf ? rcf : rc;
1028 /* Figure out how many parent dirs to symlink back */
1031 (*ptr++ == '/') ? depth++ : 0;
1036 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1038 if (ct_mkdir_p(src)) {
1039 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1040 rcf = rcf ? rcf : -errno;
1043 /* symlink already exists ? */
1044 sz = readlink(src, buf, sizeof(buf));
1045 /* detect truncation */
1046 if (sz == sizeof(buf)) {
1047 rcf = rcf ? rcf : -E2BIG;
1048 CT_ERROR(rcf, "readlink '%s' truncated", src);
1053 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1054 if (unlink(src) && errno != ENOENT) {
1056 "cannot unlink symlink '%s'",
1058 rcf = rcf ? rcf : -errno;
1060 /* unlink old symlink done */
1061 CT_TRACE("remove old symlink '%s' pointing"
1062 " to '%s'", src, buf);
1065 /* symlink already ok */
1066 CT_TRACE("symlink '%s' already pointing"
1067 " to '%s'", src, dst);
1072 if (symlink(dst, src)) {
1073 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1075 rcf = rcf ? rcf : -errno;
1078 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1090 if (ct_is_retryable(rc))
1091 hp_flags |= HP_FLAG_RETRY;
1102 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1107 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1109 struct hsm_copyaction_private *hcp = NULL;
1112 char lov_buf[XATTR_SIZE_MAX];
1113 size_t lov_size = sizeof(lov_buf);
1122 /* we fill lustre so:
1123 * source = lustre FID in the backend
1124 * destination = data FID = volatile file
1127 /* build backend file name from released file FID */
1128 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1130 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1133 CT_ERROR(rc, "cannot get mdt index "DFID"",
1134 PFID(&hai->hai_fid));
1137 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1138 * dependency on the structure format. */
1139 rc = ct_load_stripe(src, lov_buf, &lov_size);
1141 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1142 src, strerror(-rc));
1145 open_flags |= O_LOV_DELAY_CREATE;
1149 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1153 /* get the FID of the volatile file */
1154 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1156 CT_ERROR(rc, "restoring "DFID
1157 ", cannot get FID of created volatile file",
1158 PFID(&hai->hai_fid));
1162 /* build volatile "file name", for messages */
1163 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1165 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1167 if (opt.o_dry_run) {
1172 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
1175 CT_ERROR(rc, "cannot open '%s' for read", src);
1179 dst_fd = llapi_hsm_action_get_fd(hcp);
1182 CT_ERROR(rc, "cannot open '%s' for write", dst);
1187 /* the layout cannot be allocated through .fid so we have to
1188 * restore a layout */
1189 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1191 CT_ERROR(rc, "cannot restore file striping info"
1192 " for '%s' from '%s'", dst, src);
1198 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1200 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1203 if (ct_is_retryable(rc))
1204 hp_flags |= HP_FLAG_RETRY;
1208 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1211 rc = ct_fini(&hcp, hai, hp_flags, rc);
1213 /* object swaping is done by cdt at copy end, so close of volatile file
1214 * cannot be done before */
1224 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1226 struct hsm_copyaction_private *hcp = NULL;
1230 rc = ct_begin(&hcp, hai);
1234 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1236 CT_TRACE("removing file '%s'", dst);
1238 if (opt.o_dry_run) {
1246 CT_ERROR(rc, "cannot unlink '%s'", dst);
1251 strlcat(dst, ".lov", sizeof(dst));
1255 CT_ERROR(rc, "cannot unlink '%s'", dst);
1261 rc = ct_fini(&hcp, hai, 0, rc);
1266 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1270 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1271 /* Print the original path */
1273 char path[PATH_MAX];
1274 long long recno = -1;
1277 sprintf(fid, DFID, PFID(&hai->hai_fid));
1278 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64,
1279 fid, hsm_copytool_action2name(hai->hai_action),
1280 hai->hai_len, hai->hai_cookie);
1281 rc = llapi_fid2path(opt.o_mnt, fid, path,
1282 sizeof(path), &recno, &linkno);
1284 CT_ERROR(rc, "cannot get path of FID %s", fid);
1286 CT_TRACE("processing file '%s'", path);
1289 switch (hai->hai_action) {
1290 /* set err_major, minor inside these functions */
1292 rc = ct_archive(hai, hal_flags);
1295 rc = ct_restore(hai, hal_flags);
1298 rc = ct_remove(hai, hal_flags);
1301 CT_TRACE("cancel not implemented for file system '%s'",
1303 /* Don't report progress to coordinator for this cookie:
1304 * the copy function will get ECANCELED when reporting
1311 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1314 ct_fini(NULL, hai, 0, rc);
1322 struct hsm_action_item *hai;
1325 static void *ct_thread(void *data)
1327 struct ct_th_data *cttd = data;
1330 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1334 pthread_exit((void *)(intptr_t)rc);
1337 static int ct_process_item_async(const struct hsm_action_item *hai,
1340 pthread_attr_t attr;
1342 struct ct_th_data *data;
1345 data = malloc(sizeof(*data));
1349 data->hai = malloc(hai->hai_len);
1350 if (data->hai == NULL) {
1355 memcpy(data->hai, hai, hai->hai_len);
1356 data->hal_flags = hal_flags;
1358 rc = pthread_attr_init(&attr);
1360 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1367 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1369 rc = pthread_create(&thread, &attr, ct_thread, data);
1371 CT_ERROR(rc, "cannot create thread for '%s' service",
1374 pthread_attr_destroy(&attr);
1378 static int ct_import_one(const char *src, const char *dst)
1380 char newarc[PATH_MAX];
1385 CT_TRACE("importing '%s' from '%s'", dst, src);
1387 if (stat(src, &st) < 0) {
1389 CT_ERROR(rc, "cannot stat '%s'", src);
1396 rc = llapi_hsm_import(dst,
1397 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1398 &st, 0, 0, 0, 0, NULL, &fid);
1400 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1404 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1406 rc = ct_mkdir_p(newarc);
1408 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1414 /* Lots of choices now: mv, ln, ln -s ? */
1415 rc = link(src, newarc); /* hardlink */
1418 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1422 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1427 static char *path_concat(const char *dirname, const char *basename)
1432 rc = asprintf(&result, "%s/%s", dirname, basename);
1439 static int ct_import_fid(const lustre_fid *import_fid)
1441 char fid_path[PATH_MAX];
1444 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1445 rc = access(fid_path, F_OK);
1446 if (rc == 0 || errno != ENOENT) {
1447 rc = (errno == 0) ? -EEXIST : -errno;
1448 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1452 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1455 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1457 return ct_import_one(fid_path, opt.o_dst);
1460 static int ct_import_recurse(const char *relpath)
1463 struct dirent ent, *cookie = NULL;
1464 char *srcpath, *newpath;
1465 lustre_fid import_fid;
1468 if (relpath == NULL)
1471 /* Is relpath a FID? In which case SFID should expand to three
1473 rc = sscanf(relpath, SFID, RFID(&import_fid));
1475 return ct_import_fid(&import_fid);
1477 srcpath = path_concat(opt.o_hsm_root, relpath);
1478 if (srcpath == NULL) {
1483 dir = opendir(srcpath);
1485 /* Not a dir, or error */
1486 if (errno == ENOTDIR) {
1487 /* Single regular file case, treat o_dst as absolute
1489 rc = ct_import_one(srcpath, opt.o_dst);
1492 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1501 rc = readdir_r(dir, &ent, &cookie);
1504 CT_ERROR(rc, "cannot readdir_r '%s'", relpath);
1507 } else if ((rc == 0) && (cookie == NULL)) {
1508 /* end of directory */
1512 if (!strcmp(ent.d_name, ".") ||
1513 !strcmp(ent.d_name, ".."))
1516 /* New relative path */
1517 newpath = path_concat(relpath, ent.d_name);
1518 if (newpath == NULL) {
1524 if (ent.d_type == DT_DIR) {
1525 rc = ct_import_recurse(newpath);
1530 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1531 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1532 /* Make the target dir in the Lustre fs */
1533 rc = ct_mkdir_p(dst);
1535 /* Import the file */
1536 rc = ct_import_one(src, dst);
1538 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1544 CT_ERROR(rc, "cannot import '%s'", newpath);
1545 if (err_major && opt.o_abort_on_error) {
1559 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1565 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1567 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1568 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1570 if (!opt.o_dry_run) {
1572 if (rename(src, dst)) {
1574 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1577 /* rename lov file */
1578 strlcat(src, ".lov", sizeof(src));
1579 strlcat(dst, ".lov", sizeof(dst));
1580 if (rename(src, dst))
1581 CT_ERROR(errno, "cannot rename '%s' to '%s'", src, dst);
1587 static bool fid_is_file(lustre_fid *fid)
1589 return fid_is_norm(fid) || fid_is_igif(fid);
1592 static bool should_ignore_line(const char *line)
1596 for (i = 0; line[i] != '\0'; i++) {
1597 if (isspace(line[i]))
1599 else if (line[i] == '#')
1608 static int ct_rebind_list(const char *list)
1614 size_t line_size = 0;
1615 unsigned int nl = 0;
1616 unsigned int ok = 0;
1618 filp = fopen(list, "r");
1621 CT_ERROR(rc, "cannot open '%s'", list);
1625 /* each line consists of 2 FID */
1626 while ((r = getline(&line, &line_size, filp)) != -1) {
1630 /* Ignore empty and commented out ('#...') lines. */
1631 if (should_ignore_line(line))
1636 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1637 if (rc != 6 || !fid_is_file(&old_fid) ||
1638 !fid_is_file(&new_fid)) {
1640 "'%s' FID expected near '%s', line %u",
1646 if (ct_rebind_one(&old_fid, &new_fid))
1657 /* return 0 if all rebinds were sucessful */
1658 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1660 return ok == nl ? 0 : -1;
1663 static int ct_rebind(void)
1671 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1672 !fid_is_file(&old_fid)) {
1674 CT_ERROR(rc, "'%s' invalid FID format", opt.o_src);
1678 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1679 !fid_is_file(&new_fid)) {
1681 CT_ERROR(rc, "'%s' invalid FID format", opt.o_dst);
1685 rc = ct_rebind_one(&old_fid, &new_fid);
1690 /* o_src is a list file */
1691 rc = ct_rebind_list(opt.o_src);
1696 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1701 struct dirent ent, *cookie = NULL;
1705 dir = opendir(dirpath);
1708 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1712 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1714 /* end of directory.
1715 * rc is 0 and seqmax contains the max value. */
1718 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1721 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1722 CT_TRACE("'%s' has an unexpected dirname format, "
1723 "skip entry", ent.d_name);
1726 if (sub_seq > *sub_seqmax)
1727 *sub_seqmax = sub_seq;
1730 CT_ERROR(rc, "cannot readdir_r '%s'", dirpath);
1737 static int ct_max_sequence(void)
1740 char path[PATH_MAX];
1744 strlcpy(path, opt.o_hsm_root, sizeof(path));
1745 /* FID sequence is stored in top-level directory names:
1746 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1748 for (i = 0; i < 4; i++) {
1751 rc = ct_dir_level_max(path, &subseq);
1754 seq |= ((__u64)subseq << ((3 - i) * 16));
1755 path_len = strlen(path);
1756 rc = snprintf(path + path_len, sizeof(path) - path_len,
1758 if (rc >= (sizeof(path) - path_len))
1760 path[sizeof(path) - 1] = '\0';
1763 printf("max_sequence: "LPX64"\n", seq);
1768 static void handler(int signal)
1770 psignal(signal, "exiting");
1771 /* If we don't clean up upon interrupt, umount thinks there's a ref
1772 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1773 * does successfully unmount and the mount is actually gone, but the
1774 * mtab entry remains. So this just makes mtab happier. */
1775 llapi_hsm_copytool_unregister(&ctdata);
1779 /* Daemon waits for messages from the kernel; run it in the background. */
1780 static int ct_run(void)
1784 if (opt.o_daemonize) {
1788 CT_ERROR(rc, "cannot daemonize");
1793 setbuf(stdout, NULL);
1795 if (opt.o_event_fifo != NULL) {
1796 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
1798 CT_ERROR(rc, "failed to register event fifo");
1801 llapi_error_callback_set(llapi_hsm_log_error);
1804 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
1806 opt.o_archive_id, 0);
1808 CT_ERROR(rc, "cannot start copytool interface");
1812 signal(SIGINT, handler);
1813 signal(SIGTERM, handler);
1816 struct hsm_action_list *hal;
1817 struct hsm_action_item *hai;
1821 CT_TRACE("waiting for message from kernel");
1823 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1824 if (rc == -ESHUTDOWN) {
1825 CT_TRACE("shutting down");
1827 } else if (rc < 0) {
1828 CT_WARN("cannot receive action list: %s",
1831 if (opt.o_abort_on_error)
1837 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1838 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1840 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1842 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
1843 hal->hal_fsname, fs_name);
1845 if (opt.o_abort_on_error)
1851 hai = hai_first(hal);
1852 while (++i <= hal->hal_count) {
1853 if ((char *)hai - (char *)hal > msgsize) {
1856 "'%s' item %d past end of message!",
1861 rc = ct_process_item_async(hai, hal->hal_flags);
1863 CT_ERROR(rc, "'%s' item %d process",
1865 if (opt.o_abort_on_error && err_major)
1867 hai = hai_next(hai);
1870 if (opt.o_abort_on_error && err_major)
1874 llapi_hsm_copytool_unregister(&ctdata);
1875 if (opt.o_event_fifo != NULL)
1876 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1881 static int ct_setup(void)
1885 /* set llapi message level */
1886 llapi_msg_set_level(opt.o_verbose);
1888 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1891 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1895 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1897 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
1902 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
1903 if (opt.o_mnt_fd < 0) {
1905 CT_ERROR(rc, "cannot open mount point at '%s'",
1913 static int ct_cleanup(void)
1917 if (opt.o_mnt_fd >= 0) {
1918 rc = close(opt.o_mnt_fd);
1921 CT_ERROR(rc, "cannot close mount point");
1930 CT_ERROR(rc, "cannot close archive root directory");
1938 int main(int argc, char **argv)
1942 strlcpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1943 rc = ct_parseopts(argc, argv);
1945 CT_WARN("try '%s --help' for more information", cmd_name);
1953 switch (opt.o_action) {
1955 rc = ct_import_recurse(opt.o_src);
1961 rc = ct_max_sequence();
1968 if (opt.o_action != CA_MAXSEQ)
1969 CT_TRACE("process finished, errs: %d major, %d minor,"
1970 " rc=%d (%s)", err_major, err_minor, rc,