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.
40 #include <sys/xattr.h>
41 #include <lustre/lustre_idl.h>
42 #include <lustre/lustreapi.h>
44 /* Progress reporting period */
45 #define REPORT_INTERVAL_DEFAULT 30
46 /* HSM hash subdir permissions */
47 #define DIR_PERM S_IRWXU
48 /* HSM hash file permissions */
49 #define FILE_PERM (S_IRUSR | S_IWUSR)
51 #define ONE_MB 0x100000
53 /* copytool uses a 32b bitmask field to register with kuc
54 * archive num = 0 => all
55 * archive num from 1 to 32
57 #define MAX_ARCHIVE_CNT (sizeof(__u32) * 8)
74 int o_archive_id[MAX_ARCHIVE_CNT];
76 unsigned long long o_bandwidth;
78 enum ct_action o_action;
81 char *o_src; /* for import, or rebind */
82 char *o_dst; /* for import, or rebind */
85 /* everything else is zeroed */
86 struct options opt = {
89 .o_verbose = LLAPI_MSG_WARN,
91 .o_report_int = REPORT_INTERVAL_DEFAULT,
92 .o_chunk_size = ONE_MB,
95 /* hsm_copytool_private will hold an open FD on the lustre mount point
96 * for us. Additionally open one on the archive FS root to make sure
97 * it doesn't drop out from under us (and remind the admin to shutdown
98 * the copytool before unmounting). */
100 static int arc_fd = -1;
102 static int err_major;
103 static int err_minor;
105 static char cmd_name[PATH_MAX];
106 static char fs_name[MAX_OBD_NAME + 1];
108 static struct hsm_copytool_private *ctdata;
111 #define CT_ERROR(format, ...) \
112 llapi_printf(LLAPI_MSG_ERROR, "%s: "format, cmd_name, ## __VA_ARGS__)
113 #define CT_DEBUG(format, ...) \
114 llapi_printf(LLAPI_MSG_DEBUG, "%s: "format, cmd_name, ## __VA_ARGS__)
115 #define CT_WARN(format, ...) \
116 llapi_printf(LLAPI_MSG_WARN, "%s: "format, cmd_name, ## __VA_ARGS__)
117 #define CT_TRACE(format, ...) \
118 llapi_printf(LLAPI_MSG_INFO, "%s: "format, cmd_name, ## __VA_ARGS__)
120 static void usage(const char *name, int rc)
123 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
124 "The Lustre HSM Posix copy tool can be used as a daemon or "
125 "as a command line tool\n"
126 "The Lustre HSM daemon acts on action requests from Lustre\n"
127 "to copy files to and from an HSM archive system.\n"
128 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
129 "to an HSM mounted at a given hsm_root.\n"
130 " --daemon Daemon mode, run in background\n"
132 " --no-attr Don't copy file attributes\n"
133 " --no-shadow Don't create shadow namespace in archive\n"
134 " --no-xattr Don't copy file extended attributes\n"
135 "The Lustre HSM tool performs administrator-type actions\n"
136 "on a Lustre HSM archive.\n"
137 "This POSIX-flavored tool can link an existing HSM namespace\n"
138 "into a Lustre filesystem.\n"
140 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
141 " import an archived subtree at\n"
142 " <src> (relative to hsm_root) into the Lustre filesystem at\n"
143 " <dst> (absolute)\n"
144 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
145 " rebind an entry in the HSM to a new FID\n"
146 " <old_FID> old FID the HSM entry is bound to\n"
147 " <new_FID> new FID to bind the HSM entry to\n"
148 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
149 " perform the rebind operation for all FID in the list file\n"
150 " each line of <list_file> consists of <old_FID> <new_FID>\n"
151 " %s [options] --max-sequence <fsname>\n"
152 " return the max fid sequence of archived files\n"
153 " -A, --archive <#> Archive number (repeatable)\n"
154 " -p, --hsm-root <path> Target HSM mount point\n"
155 " -q, --quiet Produce less verbose output\n"
156 " -v, --verbose Produce more verbose output\n"
157 " -c, --chunk-size <sz> I/O size used during data copy\n"
158 " (unit can be used, default is MB)\n"
159 " --abort-on-error Abort operation on major error\n"
160 " --dry-run Don't run, just show what would be done\n"
161 " --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
163 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
168 static int ct_parseopts(int argc, char * const *argv)
170 struct option long_opts[] = {
171 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
172 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
173 {"archive", required_argument, NULL, 'A'},
174 {"bandwidth", required_argument, NULL, 'b'},
175 {"chunk-size", required_argument, NULL, 'c'},
176 {"chunk_size", required_argument, NULL, 'c'},
177 {"daemon", no_argument, &opt.o_daemonize, 1},
178 {"dry-run", no_argument, &opt.o_dry_run, 1},
179 {"help", no_argument, NULL, 'h'},
180 {"hsm-root", required_argument, NULL, 'p'},
181 {"hsm_root", required_argument, NULL, 'p'},
182 {"import", no_argument, NULL, 'i'},
183 {"max-sequence", no_argument, NULL, 'M'},
184 {"max_sequence", no_argument, NULL, 'M'},
185 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
186 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
187 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
188 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
189 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
190 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
191 {"quiet", no_argument, NULL, 'q'},
192 {"rebind", no_argument, NULL, 'r'},
193 {"report", required_argument, &opt.o_report_int, 0},
194 {"verbose", no_argument, NULL, 'v'},
198 unsigned long long value;
199 unsigned long long unit;
202 while ((c = getopt_long(argc, argv, "A:b:c:hiMp:qrv",
203 long_opts, NULL)) != -1) {
206 if ((opt.o_archive_cnt >= MAX_ARCHIVE_CNT) ||
207 (atoi(optarg) >= MAX_ARCHIVE_CNT)) {
208 CT_ERROR("archive number must be less"
209 "than %zu\n", MAX_ARCHIVE_CNT);
212 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
215 case 'b': /* -b and -c have both a number with unit as arg */
218 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
219 CT_ERROR("bad value for -%c '%s'\n", c, optarg);
223 opt.o_chunk_size = value;
225 opt.o_bandwidth = value;
230 opt.o_action = CA_IMPORT;
233 opt.o_action = CA_MAXSEQ;
236 opt.o_hsm_root = optarg;
242 opt.o_action = CA_REBIND;
250 CT_ERROR("unrecognized option '%s'\n",
256 switch (opt.o_action) {
258 /* src dst mount_point */
259 if (argc != optind + 3) {
260 CT_ERROR("--import requires 2 arguments\n");
263 opt.o_src = argv[optind++];
264 opt.o_dst = argv[optind++];
267 /* FID1 FID2 mount_point or FILE mount_point */
268 if (argc == optind + 2) {
269 opt.o_src = argv[optind++];
271 } else if (argc == optind + 3) {
272 opt.o_src = argv[optind++];
273 opt.o_dst = argv[optind++];
275 CT_ERROR("--rebind requires 1 or 2 arguments\n");
281 /* just mount point */
285 if (argc != optind + 1) {
286 CT_ERROR("no mount point specified\n");
290 opt.o_mnt = argv[optind];
292 CT_TRACE("action=%d src=%s dst=%s mount_point=%s\n",
293 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
295 if (!opt.o_dry_run && opt.o_hsm_root == NULL) {
296 CT_ERROR("must specify a HSM root\n");
300 if (opt.o_action == CA_IMPORT) {
301 if (opt.o_src && opt.o_src[0] == '/') {
302 CT_ERROR("source path must be relative to HSM root.\n");
306 if (opt.o_dst && opt.o_dst[0] != '/') {
307 CT_ERROR("destination path must be absolute.\n");
316 static int ct_mkdir_p(const char *path)
326 while ((ptr = strchr(ptr, '/')) != NULL) {
328 rc = mkdir(saved, DIR_PERM);
330 if (rc < 0 && errno != EEXIST) {
332 CT_ERROR("'%s' mkdir failed (%s)\n", path,
345 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
347 char lov_file[PATH_MAX];
348 char lov_buf[XATTR_SIZE_MAX];
349 struct lov_user_md *lum;
354 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
355 CT_TRACE("saving stripe info of '%s' in %s\n", src, lov_file);
357 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
359 if (xattr_size < 0) {
361 CT_ERROR("'%s' cannot get stripe info on (%s)\n", src,
366 lum = (struct lov_user_md *)lov_buf;
368 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
369 lum->lmm_magic == LOV_USER_MAGIC_V3) {
370 /* Set stripe_offset to -1 so that it is not interpreted as a
371 * hint on restore. */
372 lum->lmm_stripe_offset = -1;
375 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
378 CT_ERROR("'%s' cannot open (%s)\n", lov_file, strerror(-rc));
382 rc = write(fd, lum, xattr_size);
385 CT_ERROR("'%s' cannot write %zd bytes (%s)\n",
386 lov_file, xattr_size, strerror(-rc));
394 CT_ERROR("'%s' cannot close (%s)\n", lov_file, strerror(-rc));
406 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
408 char lov_file[PATH_MAX];
412 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
413 CT_TRACE("reading stripe rules from '%s' for '%s'\n", lov_file, src);
415 fd = open(lov_file, O_RDONLY);
417 CT_ERROR("'%s' cannot open (%s)\n", lov_file, strerror(errno));
421 rc = read(fd, lovea, *lovea_size);
423 CT_ERROR("'%s' cannot read %zu bytes (%s)\n", lov_file,
424 *lovea_size, strerror(errno));
435 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
436 const void *lovea, size_t lovea_size)
440 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
443 CT_ERROR("'%s' cannot set striping (%s)\n",
444 dst, strerror(errno));
451 /* non-blocking read or write */
452 static int nonblock_rw(bool wr, int fd, char *buf, int size)
457 rc = write(fd, buf, size);
459 rc = read(fd, buf, size);
461 if ((rc < 0) && (errno == -EAGAIN)) {
463 struct timeval timeout;
465 timeout.tv_sec = opt.o_report_int;
470 rc = select(FD_SETSIZE, NULL, &set, NULL, &timeout);
472 rc = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
476 /* Timed out, we read nothing */
479 /* Should be available now */
481 rc = write(fd, buf, size);
483 rc = read(fd, buf, size);
492 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
493 const char *dst, int src_fd, int dst_fd,
494 const struct hsm_action_item *hai, long hal_flags)
496 struct hsm_extent he;
503 time_t last_print_time = time(0);
509 CT_TRACE("going to copy data from '%s' to %s\n", src, dst);
511 buf = malloc(opt.o_chunk_size);
515 if (fstat(src_fd, &src_st) < 0) {
516 CT_ERROR("'%s' stat failed (%s)\n", src, strerror(errno));
520 if (!S_ISREG(src_st.st_mode)) {
521 CT_ERROR("'%s' not a regular file\n", src);
525 rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
527 CT_ERROR("'%s' seek to read to "LPU64" (len %jd)"
529 src, hai->hai_extent.offset, (intmax_t)src_st.st_size,
535 if (fstat(dst_fd, &dst_st) < 0) {
536 CT_ERROR("'%s' stat failed (%s)\n", dst, strerror(errno));
540 if (!S_ISREG(dst_st.st_mode)) {
541 CT_ERROR("'%s' not a regular file\n", dst);
545 rc = lseek(dst_fd, hai->hai_extent.offset, SEEK_SET);
547 CT_ERROR("'%s' seek to write to "LPU64" failed (%s)\n", src,
548 hai->hai_extent.offset, strerror(errno));
553 he.offset = hai->hai_extent.offset;
555 rc = llapi_hsm_action_progress(hcp, &he, 0);
557 /* Action has been canceled or something wrong
558 * is happening. Stop copying data. */
559 CT_ERROR("%s->'%s' progress returned err %d\n", src, dst, rc);
564 /* Don't read beyond a given extent */
565 rlen = (hai->hai_extent.length == -1LL) ?
566 src_st.st_size : hai->hai_extent.length;
568 while (wpos < rlen) {
569 int chunk = (rlen - wpos > opt.o_chunk_size) ?
570 opt.o_chunk_size : rlen - wpos;
572 /* Only read more if we wrote everything in the buffer */
574 rsize = nonblock_rw(0, src_fd, buf, chunk);
579 if (rsize == -EAGAIN) {
583 /* Haven't read anything yet, let's
584 * give it back to the coordinator
585 * for rescheduling */
592 CT_ERROR("'%s' read failed (%s)\n", src,
602 wsize = nonblock_rw(1, dst_fd, buf + bufoff, rpos - wpos);
603 if (wsize == -EAGAIN)
608 CT_ERROR("'%s' write failed (%s)\n", dst,
617 if (opt.o_bandwidth != 0) {
618 static unsigned long long tot_bytes;
619 static time_t start_time, last_time;
620 time_t now = time(0);
621 double tot_time, excess;
622 unsigned int sleep_time;
624 if (now > last_time + 5) {
626 start_time = last_time = now;
630 tot_time = now - start_time;
634 excess = tot_bytes - tot_time * opt.o_bandwidth;
635 sleep_time = excess * 1000000 / opt.o_bandwidth;
636 if ((now - start_time) % 10 == 1)
637 CT_TRACE("bandwith control: excess=%E"
647 if (time(0) >= last_print_time + opt.o_report_int) {
648 last_print_time = time(0);
649 CT_TRACE("%%"LPU64" ", 100 * wpos / rlen);
651 rc = llapi_hsm_action_progress(hcp, &he, 0);
653 /* Action has been canceled or something wrong
654 * is happening. Stop copying data. */
655 CT_ERROR("%s->'%s' progress returned err %d\n",
666 * truncate restored file
667 * size is taken from the archive this is done to support
668 * restore after a force release which leaves the file with the
669 * wrong size (can big bigger than the new size)
671 if ((hai->hai_action == HSMA_RESTORE) &&
672 (src_st.st_size < dst_st.st_size)) {
674 * make sure the file is on disk before reporting success.
676 rc = ftruncate(dst_fd, src_st.st_size);
679 CT_ERROR("'%s' final truncate to %jd failed (%s)\n",
680 dst, (intmax_t)src_st.st_size, strerror(-rc));
690 /* Copy file attributes from file src to file dest */
691 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
695 struct timeval times[2];
697 if (fstat(src_fd, &st) < 0) {
698 CT_ERROR("'%s' stat failed (%s)\n",
699 src, strerror(errno));
703 times[0].tv_sec = st.st_atime;
704 times[0].tv_usec = 0;
705 times[1].tv_sec = st.st_mtime;
706 times[1].tv_usec = 0;
707 if (fchmod(dst_fd, st.st_mode) < 0 ||
708 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
709 futimes(dst_fd, times) < 0) {
710 CT_ERROR("'%s' fchmod fchown or futimes failed (%s)\n", src,
718 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
719 int dst_fd, bool is_restore)
721 char list[XATTR_LIST_MAX];
722 char value[XATTR_SIZE_MAX];
727 list_len = flistxattr(src_fd, list, sizeof(list));
732 while (name < list + list_len) {
733 rc = fgetxattr(src_fd, name, value, sizeof(value));
737 /* when we restore, we do not restore lustre xattr */
739 (strncmp(XATTR_TRUSTED_PREFIX, name,
740 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
741 rc = fsetxattr(dst_fd, name, value, rc, 0);
742 CT_TRACE("'%s' fsetxattr of '%s' rc=%d (%s)\n",
743 dst, name, rc, strerror(errno));
744 /* lustre.* attrs aren't supported on other FS's */
745 if (rc < 0 && errno != EOPNOTSUPP) {
746 CT_ERROR("'%s' fsetxattr of '%s' failed (%s)\n",
747 dst, name, strerror(errno));
751 name += strlen(name) + 1;
757 static int ct_path_lustre(char *buf, int sz, const char *mnt,
758 const lustre_fid *fid)
760 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
761 dot_lustre_name, PFID(fid));
764 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
765 const lustre_fid *fid)
767 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
768 DFID_NOBRACE, archive_dir,
769 (fid)->f_oid & 0xFFFF,
770 (fid)->f_oid >> 16 & 0xFFFF,
771 (unsigned int)((fid)->f_seq & 0xFFFF),
772 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
773 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
774 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
778 static bool ct_is_retryable(int err)
780 return err == -ETIMEDOUT;
783 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
784 const struct hsm_action_item *hai,
785 int mdt_index, int open_flags)
790 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
793 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
794 CT_ERROR("'%s' copy start failed (%s)\n", src, strerror(-rc));
800 static int ct_begin(struct hsm_copyaction_private **phcp,
801 const struct hsm_action_item *hai)
803 /* Restore takes specific parameters. Call the same function w/ default
804 * values for all other operations. */
805 return ct_begin_restore(phcp, hai, -1, 0);
808 static int ct_fini(struct hsm_copyaction_private **phcp,
809 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
814 CT_TRACE("Action completed, notifying coordinator "
815 "cookie="LPX64", FID="DFID", hp_flags=%d err=%d\n",
816 hai->hai_cookie, PFID(&hai->hai_fid),
819 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
820 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
821 if (rc == -ECANCELED)
822 CT_ERROR("'%s' completed action has been canceled: "
823 "cookie="LPX64", FID="DFID"\n", lstr, hai->hai_cookie,
824 PFID(&hai->hai_fid));
826 CT_ERROR("'%s' copy end failed (%s)\n", lstr, strerror(-rc));
828 CT_TRACE("'%s' copy end ok (rc=%d)\n", lstr, rc);
833 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
835 struct hsm_copyaction_private *hcp = NULL;
840 bool rename_needed = false;
846 rc = ct_begin(&hcp, hai);
850 /* we fill archive so:
852 * destination = lustre FID
854 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
855 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
856 if (hai->hai_extent.length == -1) {
857 /* whole file, write it to tmp location and atomically
858 * replace old archived file */
859 strncat(dst, "_tmp", sizeof(dst) - strlen(dst) - 1);
860 /* we cannot rely on the same test because ct_copy_data()
861 * updates hai_extent.length */
862 rename_needed = true;
865 CT_TRACE("'%s' archiving to %s\n", src, dst);
872 rc = ct_mkdir_p(dst);
874 CT_ERROR("'%s' mkdir_p failed (%s)\n", dst, strerror(-rc));
878 src_fd = llapi_hsm_action_get_fd(hcp);
880 CT_ERROR("'%s' open read failed (%s)\n", src, strerror(errno));
885 open_flags = O_WRONLY | O_NOFOLLOW | O_NONBLOCK;
886 /* If extent is specified, don't truncate an old archived copy */
887 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
889 dst_fd = open(dst, open_flags, FILE_PERM);
891 CT_ERROR("'%s' open write failed (%s)\n", dst, strerror(errno));
896 /* saving stripe is not critical */
897 rc = ct_save_stripe(src_fd, src, dst);
899 CT_ERROR("'%s' cannot save file striping info in '%s' (%s)\n",
900 src, dst, strerror(-rc));
902 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
904 CT_ERROR("'%s' data copy failed to '%s' (%s)\n",
905 src, dst, strerror(-rc));
911 CT_ERROR("'%s' cannot synchronize archive file '%s' (%s)\n",
912 src, dst, strerror(errno));
917 CT_TRACE("'%s' data archived to '%s' done\n", src, dst);
919 /* attrs will remain on the MDS; no need to copy them, except possibly
920 for disaster recovery */
921 if (opt.o_copy_attrs) {
922 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
924 CT_ERROR("'%s' attr copy failed to '%s' (%s)\n",
925 src, dst, strerror(-rc));
928 CT_TRACE("'%s' attr file copied to archive '%s'\n",
932 /* xattrs will remain on the MDS; no need to copy them, except possibly
933 for disaster recovery */
934 if (opt.o_copy_xattrs) {
935 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
937 CT_ERROR("'%s' xattr copy failed to '%s' (%s)\n",
938 src, dst, strerror(-rc));
939 rcf = rcf ? rcf : rc;
941 CT_ERROR("'%s' xattr file copied to archive '%s'\n",
945 if (rename_needed == true) {
946 char tmp_src[PATH_MAX];
947 char tmp_dst[PATH_MAX];
949 /* atomically replace old archived file */
950 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
952 rc = rename(dst, src);
954 CT_ERROR("'%s' renamed to '%s' failed (%s)\n", dst, src,
959 /* rename lov file */
960 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
961 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
962 rc = rename(tmp_dst, tmp_src);
964 CT_ERROR("'%s' renamed to '%s' failed (%s)\n",
965 tmp_dst, tmp_src, strerror(errno));
968 if (opt.o_shadow_tree) {
969 /* Create a namespace of softlinks that shadows the original
970 * Lustre namespace. This will only be current at
971 * time-of-archive (won't follow renames).
972 * WARNING: release won't kill these links; a manual
973 * cleanup of dead links would be required.
976 long long recno = -1;
982 sprintf(buf, DFID, PFID(&hai->hai_fid));
983 sprintf(src, "%s/shadow/", opt.o_hsm_root);
985 ptr = opt.o_hsm_root;
987 (*ptr++ == '/') ? depth-- : 0;
989 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
990 sizeof(src) - strlen(src), &recno, &linkno);
992 CT_ERROR("'%s' fid2path failed (%s)\n", buf,
994 rcf = rcf ? rcf : rc;
998 /* Figure out how many parent dirs to symlink back */
1001 (*ptr++ == '/') ? depth++ : 0;
1006 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1008 if (ct_mkdir_p(src)) {
1009 CT_ERROR("'%s' mkdir_p failed (%s)\n", src,
1011 rcf = rcf ? rcf : -errno;
1014 /* symlink already exists ? */
1015 sz = readlink(src, buf, sizeof(buf));
1018 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1019 if (unlink(src) && errno != ENOENT) {
1020 CT_ERROR("'%s' unlink symlink failed "
1023 rcf = rcf ? rcf : -errno;
1025 /* unlink old symlink done */
1026 CT_TRACE("'%s' remove old symlink pointing"
1027 " to '%s'\n", src, buf);
1030 /* symlink already ok */
1031 CT_TRACE("'%s' symlink already pointing"
1032 " to '%s'\n", src, dst);
1037 if (symlink(dst, src)) {
1038 CT_ERROR("'%s' symlink to '%s' failed (%s)\n", src, dst,
1040 rcf = rcf ? rcf : -errno;
1043 CT_TRACE("'%s' symlink to '%s' done\n", src, dst);
1055 if (ct_is_retryable(rc))
1056 hp_flags |= HP_FLAG_RETRY;
1068 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1073 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1075 struct hsm_copyaction_private *hcp = NULL;
1078 char lov_buf[XATTR_SIZE_MAX];
1079 size_t lov_size = sizeof(lov_buf);
1084 int mdt_index = -1; /* Not implemented */
1088 /* we fill lustre so:
1089 * source = lustre FID in the backend
1090 * destination = data FID = volatile file
1093 /* build backend file name from released file FID */
1094 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1096 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1097 * dependency on the structure format. */
1098 rc = ct_load_stripe(src, lov_buf, &lov_size);
1100 CT_WARN("'%s' cannot get stripe rules (%s), use default\n",
1101 src, strerror(-rc));
1104 open_flags |= O_LOV_DELAY_CREATE;
1108 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1112 /* get the FID of the volatile file */
1113 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1115 CT_ERROR("restoring "DFID", cannot get FID of created volatile"
1116 " file (%s)\n", PFID(&hai->hai_fid), strerror(-rc));
1120 /* build volatile "file name", for messages */
1121 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1123 CT_TRACE("'%s' restore data to '%s'\n", src, dst);
1125 if (opt.o_dry_run) {
1130 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
1132 CT_ERROR("'%s' open for read failed (%s)\n", src,
1138 dst_fd = llapi_hsm_action_get_fd(hcp);
1141 /* the layout cannot be allocated through .fid so we have to
1142 * restore a layout */
1143 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1145 CT_ERROR("'%s' cannot restore file striping info from "
1146 "'%s' (%s)\n", dst, src, strerror(-rc));
1152 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1154 CT_ERROR("'%s' data copy to '%s' failed (%s)\n", src, dst,
1157 if (ct_is_retryable(rc))
1158 hp_flags |= HP_FLAG_RETRY;
1162 CT_TRACE("'%s' data restore done to %s\n", src, dst);
1166 rc = ct_fini(&hcp, hai, hp_flags, rc);
1168 /* object swaping is done by cdt at copy end, so close of volatile file
1169 * cannot be done before */
1179 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1181 struct hsm_copyaction_private *hcp = NULL;
1185 rc = ct_begin(&hcp, hai);
1189 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1191 CT_TRACE("'%s' removed file\n", dst);
1193 if (opt.o_dry_run) {
1201 CT_ERROR("'%s' unlink failed (%s)\n", dst, strerror(-rc));
1206 strncat(dst, ".lov", sizeof(dst) - strlen(dst) - 1);
1210 CT_ERROR("'%s' unlink failed (%s)\n", dst, strerror(-rc));
1217 rc = ct_fini(&hcp, hai, 0, rc);
1222 static int ct_report_error(const struct hsm_action_item *hai, int flags,
1225 struct hsm_copyaction_private *hcp;
1228 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
1232 rc = llapi_hsm_action_end(&hcp, &hai->hai_extent, flags, abs(errval));
1237 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1241 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1242 /* Print the original path */
1244 char path[PATH_MAX];
1245 long long recno = -1;
1248 sprintf(fid, DFID, PFID(&hai->hai_fid));
1249 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64"\n",
1250 fid, hsm_copytool_action2name(hai->hai_action),
1251 hai->hai_len, hai->hai_cookie);
1252 rc = llapi_fid2path(opt.o_mnt, fid, path,
1253 sizeof(path), &recno, &linkno);
1255 CT_ERROR("'%s' fid2path failed (%s)\n", fid,
1258 CT_TRACE("'%s' processing file\n", path);
1261 switch (hai->hai_action) {
1262 /* set err_major, minor inside these functions */
1264 rc = ct_archive(hai, hal_flags);
1267 rc = ct_restore(hai, hal_flags);
1270 rc = ct_remove(hai, hal_flags);
1273 CT_TRACE("'%s' cancel not implemented\n", opt.o_mnt);
1274 /* Don't report progress to coordinator for this cookie:
1275 * the copy function will get ECANCELED when reporting
1281 CT_ERROR("'%s' unknown action %d\n", opt.o_mnt,
1284 ct_report_error(hai, 0, -EINVAL);
1292 struct hsm_action_item *hai;
1295 static void *ct_thread(void *data)
1297 struct ct_th_data *cttd = data;
1300 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1304 pthread_exit((void *)(intptr_t)rc);
1307 static int ct_process_item_async(const struct hsm_action_item *hai,
1310 pthread_attr_t attr;
1312 struct ct_th_data *data;
1315 data = malloc(sizeof(*data));
1319 data->hai = malloc(hai->hai_len);
1320 if (data->hai == NULL) {
1325 memcpy(data->hai, hai, hai->hai_len);
1326 data->hal_flags = hal_flags;
1328 rc = pthread_attr_init(&attr);
1330 CT_ERROR("'%s' pthread_attr_init: %s\n", opt.o_mnt,
1337 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1339 rc = pthread_create(&thread, &attr, ct_thread, data);
1341 CT_ERROR("'%s' thread create: (%s)\n", opt.o_mnt, strerror(rc));
1343 pthread_attr_destroy(&attr);
1347 static int ct_import_one(const char *src, const char *dst)
1349 char newarc[PATH_MAX];
1354 CT_TRACE("'%s' importing from %s\n", dst, src);
1356 if (stat(src, &st) < 0) {
1357 CT_ERROR("'%s' stat failed (%s)\n", src, strerror(errno));
1364 rc = llapi_hsm_import(dst,
1365 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1366 &st, 0, 0, 0, 0, NULL, &fid);
1368 CT_ERROR("'%s' import from '%s' failed (%s)\n", dst, src,
1373 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1375 rc = ct_mkdir_p(newarc);
1377 CT_ERROR("'%s' mkdir_p failed (%s)\n", newarc, strerror(-rc));
1383 /* Lots of choices now: mv, ln, ln -s ? */
1384 rc = link(src, newarc); /* hardlink */
1386 CT_ERROR("'%s' link to '%s' failed (%s)\n", newarc, src,
1391 CT_TRACE("'%s' imported from '%s'=='%s'\n", dst, newarc, src);
1396 static char *path_concat(const char *dirname, const char *basename)
1399 int dirlen = strlen(dirname);
1401 result = malloc(dirlen + strlen(basename) + 2);
1405 memcpy(result, dirname, dirlen);
1406 result[dirlen] = '/';
1407 strcpy(result + dirlen + 1, basename);
1412 static int ct_import_recurse(const char *relpath)
1415 struct dirent ent, *cookie = NULL;
1416 char *srcpath, *newpath;
1419 if (relpath == NULL)
1422 srcpath = path_concat(opt.o_hsm_root, relpath);
1423 if (srcpath == NULL) {
1428 dir = opendir(srcpath);
1430 /* Not a dir, or error */
1431 if (errno == ENOTDIR) {
1432 /* Single regular file case, treat o_dst as absolute
1434 rc = ct_import_one(srcpath, opt.o_dst);
1436 CT_ERROR("'%s' opendir failed (%s)\n", srcpath,
1447 rc = readdir_r(dir, &ent, &cookie);
1449 CT_ERROR("'%s' readdir_r failed (%s)\n", relpath,
1454 } else if ((rc == 0) && (cookie == NULL)) {
1455 /* end of directory */
1459 if (!strcmp(ent.d_name, ".") ||
1460 !strcmp(ent.d_name, ".."))
1463 /* New relative path */
1464 newpath = path_concat(relpath, ent.d_name);
1465 if (newpath == NULL) {
1471 if (ent.d_type == DT_DIR) {
1472 rc = ct_import_recurse(newpath);
1477 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1478 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1479 /* Make the target dir in the Lustre fs */
1480 rc = ct_mkdir_p(dst);
1482 /* Import the file */
1483 rc = ct_import_one(src, dst);
1485 CT_ERROR("'%s' ct_mkdir_p failed (%s)\n", dst,
1492 CT_ERROR("'%s' importing failed\n", newpath);
1493 if (err_major && opt.o_abort_on_error) {
1507 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1512 CT_TRACE("rebind "DFID" to "DFID"\n", PFID(old_fid), PFID(new_fid));
1514 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1515 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1517 if (!opt.o_dry_run) {
1519 if (rename(src, dst)) {
1520 CT_ERROR("'%s' rename to '%s' failed (%s)\n", src, dst,
1524 /* rename lov file */
1525 strncat(src, ".lov", sizeof(src) - strlen(src) - 1);
1526 strncat(dst, ".lov", sizeof(dst) - strlen(dst) - 1);
1527 if (rename(src, dst))
1528 CT_ERROR("'%s' rename to '%s' failed (%s)\n", src, dst,
1535 static bool fid_is_file(lustre_fid *fid)
1537 return fid_is_norm(fid) || fid_is_igif(fid);
1540 static bool should_ignore_line(const char *line)
1544 for (i = 0; line[i] != '\0'; i++) {
1545 if (isspace(line[i]))
1547 else if (line[i] == '#')
1556 static int ct_rebind_list(const char *list)
1562 size_t line_size = 0;
1563 unsigned int nl = 0;
1564 unsigned int ok = 0;
1566 filp = fopen(list, "r");
1568 CT_ERROR("'%s' open failed (%s)\n", list, strerror(errno));
1572 /* each line consists of 2 FID */
1573 while ((r = getline(&line, &line_size, filp)) != -1) {
1577 /* Ignore empty and commented out ('#...') lines. */
1578 if (should_ignore_line(line))
1583 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1584 if (rc != 6 || !fid_is_file(&old_fid) ||
1585 !fid_is_file(&new_fid)) {
1586 CT_ERROR("'%s' FID expected near '%s', line %u\n",
1592 if (ct_rebind_one(&old_fid, &new_fid))
1603 /* return 0 if all rebinds were sucessful */
1604 CT_TRACE("'%s' %u lines read, %u rebind successful\n", list, nl, ok);
1606 return ok == nl ? 0 : -1;
1609 static int ct_rebind(void)
1617 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1618 !fid_is_file(&old_fid)) {
1619 CT_ERROR("'%s' invalid FID format\n", opt.o_src);
1623 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1624 !fid_is_file(&new_fid)) {
1625 CT_ERROR("'%s' invalid FID format\n", opt.o_dst);
1629 rc = ct_rebind_one(&old_fid, &new_fid);
1634 /* o_src is a list file */
1635 rc = ct_rebind_list(opt.o_src);
1640 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1645 struct dirent ent, *cookie = NULL;
1649 dir = opendir(dirpath);
1652 CT_ERROR("'%s' failed to open directory (%s)\n", opt.o_hsm_root,
1657 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1659 /* end of directory.
1660 * rc is 0 and seqmax contains the max value. */
1663 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1666 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1667 CT_TRACE("'%s' unexpected dirname format, "
1668 "skip entry.\n", ent.d_name);
1671 if (sub_seq > *sub_seqmax)
1672 *sub_seqmax = sub_seq;
1675 CT_ERROR("'%s' readdir_r failed (%s)\n", dirpath, strerror(-rc));
1682 static int ct_max_sequence(void)
1685 char path[PATH_MAX];
1689 strncpy(path, opt.o_hsm_root, sizeof(path));
1690 /* FID sequence is stored in top-level directory names:
1691 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1693 for (i = 0; i < 4; i++) {
1694 rc = ct_dir_level_max(path, &subseq);
1697 seq |= ((__u64)subseq << ((3 - i) * 16));
1698 sprintf(path + strlen(path), "/%04x", subseq);
1701 printf("max_sequence: %016Lx\n", seq);
1706 static void handler(int signal)
1708 psignal(signal, "exiting");
1709 /* If we don't clean up upon interrupt, umount thinks there's a ref
1710 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1711 * does successfully unmount and the mount is actually gone, but the
1712 * mtab entry remains. So this just makes mtab happier. */
1713 llapi_hsm_copytool_unregister(&ctdata);
1717 /* Daemon waits for messages from the kernel; run it in the background. */
1718 static int ct_run(void)
1722 if (opt.o_daemonize) {
1725 CT_ERROR("%d: cannot start as daemon (%s)", getpid(),
1731 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt, 0,
1732 opt.o_archive_cnt, opt.o_archive_id);
1734 CT_ERROR("%d: cannot start copytool interface: %s\n", getpid(),
1739 signal(SIGINT, handler);
1740 signal(SIGTERM, handler);
1743 struct hsm_action_list *hal;
1744 struct hsm_action_item *hai;
1748 CT_TRACE("%d: waiting for message from kernel\n", getpid());
1750 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1751 if (rc == -ESHUTDOWN) {
1752 CT_TRACE("%d: shutting down", getpid());
1754 } else if (rc == -EAGAIN) {
1755 continue; /* msg not for us */
1756 } else if (rc < 0) {
1757 CT_WARN("%d: message receive: (%s)\n", getpid(),
1760 if (opt.o_abort_on_error)
1766 CT_TRACE("%d: copytool fs=%s archive#=%d item_count=%d\n",
1767 getpid(), hal->hal_fsname, hal->hal_archive_id,
1770 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1771 CT_ERROR("'%s' invalid fs name, expecting: %s\n",
1772 hal->hal_fsname, fs_name);
1774 if (opt.o_abort_on_error)
1780 hai = hai_first(hal);
1781 while (++i <= hal->hal_count) {
1782 if ((char *)hai - (char *)hal > msgsize) {
1783 CT_ERROR("'%s' item %d past end of message!\n",
1789 rc = ct_process_item_async(hai, hal->hal_flags);
1791 CT_ERROR("'%s' item %d process err: %s\n",
1792 opt.o_mnt, i, strerror(-rc));
1793 if (opt.o_abort_on_error && err_major)
1795 hai = hai_next(hai);
1798 llapi_hsm_action_list_free(&hal);
1800 if (opt.o_abort_on_error && err_major)
1804 llapi_hsm_copytool_unregister(&ctdata);
1809 static int ct_setup(void)
1813 /* set llapi message level */
1814 llapi_msg_set_level(opt.o_verbose);
1816 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1818 CT_ERROR("cannot open archive at '%s': %s\n", opt.o_hsm_root,
1823 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1825 CT_ERROR("cannot find a Lustre filesystem mounted at: %s\n",
1833 static int ct_cleanup(void)
1838 if (close(arc_fd) < 0) {
1839 CT_ERROR("cannot close archive: %s.\n", strerror(errno));
1846 int main(int argc, char **argv)
1850 strncpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1851 rc = ct_parseopts(argc, argv);
1853 CT_ERROR("try '%s --help' for more information.\n", cmd_name);
1859 switch (opt.o_action) {
1861 rc = ct_import_recurse(opt.o_src);
1867 rc = ct_max_sequence();
1874 if (opt.o_action != CA_MAXSEQ)
1875 CT_TRACE("%s(%d) finished, errs: %d major, %d minor, "
1876 "rc=%d (%s)\n", argv[0], getpid(), err_major,
1877 err_minor, rc, strerror(-rc));