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.
37 #include <sys/xattr.h>
40 #include <sys/errno.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 /* The LLAPI will hold an open FD on lustre for us. Additionally open one on
96 * the archive FS root to make sure it doesn't drop out from under us (and
97 * remind the admin to shutdown the copytool before unmounting). */
98 static int arc_fd = -1;
100 static int err_major;
101 static int err_minor;
103 static char cmd_name[PATH_MAX];
104 static char fs_name[MAX_OBD_NAME + 1];
106 static struct hsm_copytool_private *ctdata;
109 #define CT_ERROR(format, ...) \
110 llapi_printf(LLAPI_MSG_ERROR, "%s: "format, cmd_name, ## __VA_ARGS__)
111 #define CT_DEBUG(format, ...) \
112 llapi_printf(LLAPI_MSG_DEBUG, "%s: "format, cmd_name, ## __VA_ARGS__)
113 #define CT_WARN(format, ...) \
114 llapi_printf(LLAPI_MSG_WARN, "%s: "format, cmd_name, ## __VA_ARGS__)
115 #define CT_TRACE(format, ...) \
116 llapi_printf(LLAPI_MSG_INFO, "%s: "format, cmd_name, ## __VA_ARGS__)
118 static void usage(const char *name, int rc)
121 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
122 "The Lustre HSM Posix copy tool can be used as a daemon or "
123 "as a command line tool\n"
124 "The Lustre HSM daemon acts on action requests from Lustre\n"
125 "to copy files to and from an HSM archive system.\n"
126 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
127 "to an HSM mounted at a given hsm_root.\n"
128 " -d, --daemon Daemon mode, run in background\n"
130 " --no-attr Don't copy file attributes\n"
131 " --no-shadow Don't create shadow namespace in archive\n"
132 " --no-xattr Don't copy file extended attributes\n"
133 "The Lustre HSM tool performs administrator-type actions\n"
134 "on a Lustre HSM archive.\n"
135 "This POSIX-flavored tool can link an existing HSM namespace\n"
136 "into a Lustre filesystem.\n"
138 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
139 " import an archived subtree at\n"
140 " <src> (relative to hsm_root) into the Lustre filesystem at\n"
141 " <dst> (absolute)\n"
142 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
143 " rebind an entry in the HSM to a new FID\n"
144 " <old_FID> old FID the HSM entry is bound to\n"
145 " <new_FID> new FID to bind the HSM entry to\n"
146 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
147 " perform the rebind operation for all FID in the list file\n"
148 " each line of <list_file> consists of <old_FID> <new_FID>\n"
149 " %s [options] --max-sequence <fsname>\n"
150 " return the max fid sequence of archived files\n"
151 " -A, --archive <#> Archive number (repeatable)\n"
152 " -p, --hsm-root <path> Target HSM mount point\n"
153 " -q, --quiet Produce less verbose output\n"
154 " -v, --verbose Produce more verbose output\n"
155 " -c, --chunk-size <sz> I/O size used during data copy\n"
156 " (unit can be used, default is MB)\n"
157 " --abort-on-error Abort operation on major error\n"
158 " --dry-run Don't run, just show what would be done\n"
159 " --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
161 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
166 static int ct_parseopts(int argc, char * const *argv)
168 struct option long_opts[] = {
169 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
170 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
171 {"archive", required_argument, NULL, 'A'},
172 {"bandwidth", required_argument, NULL, 'b'},
173 {"chunk-size", required_argument, NULL, 'c'},
174 {"chunk_size", required_argument, NULL, 'c'},
175 {"daemon", no_argument, NULL, 'd'},
176 {"dry-run", no_argument, &opt.o_dry_run, 1},
177 {"help", no_argument, NULL, 'h'},
178 {"hsm-root", required_argument, NULL, 'p'},
179 {"hsm_root", required_argument, NULL, 'p'},
180 {"import", no_argument, NULL, 'i'},
181 {"max-sequence", no_argument, NULL, 'M'},
182 {"max_sequence", no_argument, NULL, 'M'},
183 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
184 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
185 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
186 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
187 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
188 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
189 {"quiet", no_argument, NULL, 'q'},
190 {"rebind", no_argument, NULL, 'r'},
191 {"report", required_argument, &opt.o_report_int, 0},
192 {"verbose", no_argument, NULL, 'v'},
196 unsigned long long value;
197 unsigned long long unit;
200 while ((c = getopt_long(argc, argv, "A:b:c:dhiMp:qruv",
201 long_opts, NULL)) != -1) {
204 if ((opt.o_archive_cnt >= MAX_ARCHIVE_CNT) ||
205 (atoi(optarg) >= MAX_ARCHIVE_CNT)) {
206 CT_ERROR("archive number must be less"
207 "than %lu\n", MAX_ARCHIVE_CNT);
210 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
213 case 'b': /* -b and -c have both a number with unit as arg */
216 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
217 CT_ERROR("bad value for -%c '%s'\n", c, optarg);
221 opt.o_chunk_size = value;
223 opt.o_bandwidth = value;
226 opt.o_action = CA_DAEMON;
231 opt.o_action = CA_IMPORT;
234 opt.o_action = CA_MAXSEQ;
237 opt.o_hsm_root = optarg;
243 opt.o_action = CA_REBIND;
251 CT_ERROR("unrecognized option '%s'\n",
257 switch (opt.o_action) {
259 /* src dst mount_point */
260 if (argc != optind + 3) {
261 CT_ERROR("--import requires 2 arguments\n");
264 opt.o_src = argv[optind++];
265 opt.o_dst = argv[optind++];
268 /* FID1 FID2 mount_point or FILE mount_point */
269 if (argc == optind + 2) {
270 opt.o_src = argv[optind++];
272 } else if (argc == optind + 3) {
273 opt.o_src = argv[optind++];
274 opt.o_dst = argv[optind++];
276 CT_ERROR("--rebind requires 1 or 2 arguments\n");
282 /* just mount point */
286 if (argc != optind + 1) {
287 CT_ERROR("no mount point specified\n");
291 opt.o_mnt = argv[optind];
293 CT_TRACE("action=%d src=%s dst=%s mount_point=%s\n",
294 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
296 if (!opt.o_dry_run && opt.o_hsm_root == NULL) {
297 CT_ERROR("must specify a HSM root\n");
301 if (opt.o_action == CA_IMPORT) {
302 if (opt.o_src && opt.o_src[0] == '/') {
303 CT_ERROR("source path must be relative to HSM root.\n");
307 if (opt.o_dst && opt.o_dst[0] != '/') {
308 CT_ERROR("destination path must be absolute.\n");
317 static int ct_mkdir_p(const char *path)
327 while ((ptr = strchr(ptr, '/')) != NULL) {
329 rc = mkdir(saved, DIR_PERM);
331 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) {
360 CT_ERROR("'%s' cannot get stripe info on (%s)\n", src,
365 lum = (struct lov_user_md *)lov_buf;
367 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
368 lum->lmm_magic == LOV_USER_MAGIC_V3) {
369 /* Set stripe_offset to -1 so that it is not interpreted as a
370 * hint on restore. */
371 lum->lmm_stripe_offset = -1;
374 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
376 CT_ERROR("'%s' cannot open (%s)\n", lov_file, strerror(errno));
380 rc = write(fd, lum, xattr_size);
382 CT_ERROR("'%s' cannot write %d bytes (%s)\n",
383 lov_file, xattr_size, strerror(errno));
390 CT_ERROR("'%s' cannot close (%s)\n", lov_file, strerror(errno));
397 static int ct_load_stripe(const char *src, struct lov_user_md_v3 *lum,
400 char lov_file[PATH_MAX];
404 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
405 CT_TRACE("reading stripe rules from '%s' for '%s'\n", lov_file, src);
407 fd = open(lov_file, O_RDONLY);
409 CT_ERROR("'%s' cannot open (%s)\n", lov_file, strerror(errno));
413 rc = read(fd, lum, *lum_size);
415 CT_ERROR("'%s' cannot read %lu bytes (%s)\n", lov_file,
416 lum_size, strerror(errno));
421 *lum_size = (size_t)rc;
427 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd)
430 char lov_buf[XATTR_SIZE_MAX];
431 size_t lum_size = sizeof(lov_buf);
433 rc = ct_load_stripe(src, (struct lov_user_md_v3 *)lov_buf, &lum_size);
435 CT_WARN("'%s' cannot get stripe rules (%s), use default\n",
440 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lov_buf, lum_size, XATTR_CREATE);
442 CT_ERROR("'%s' cannot set striping (%s)\n",
443 dst, strerror(errno));
450 /* non-blocking read or write */
451 static int nonblock_rw(bool wr, int fd, char *buf, int size)
456 rc = write(fd, buf, size);
458 rc = read(fd, buf, size);
460 if ((rc < 0) && (errno == -EAGAIN)) {
462 struct timeval timeout;
464 timeout.tv_sec = opt.o_report_int;
469 rc = select(FD_SETSIZE, NULL, &set, NULL, &timeout);
471 rc = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
475 /* Timed out, we read nothing */
478 /* Should be available now */
480 rc = write(fd, buf, size);
482 rc = read(fd, buf, size);
491 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
492 const char *dst, int src_fd, int dst_fd,
493 const struct hsm_action_item *hai, long hal_flags)
495 struct hsm_extent he;
502 time_t last_print_time = time(0);
508 CT_TRACE("going to copy data from '%s' to %s\n", src, dst);
510 buf = malloc(opt.o_chunk_size);
514 if (fstat(src_fd, &src_st) < 0) {
515 CT_ERROR("'%s' stat failed (%s)\n", src, strerror(errno));
519 if (!S_ISREG(src_st.st_mode)) {
520 CT_ERROR("'%s' not a regular file\n", src);
524 rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
526 CT_ERROR("'%s' seek to read to "LPU64" (len %zu)"
528 src, hai->hai_extent.offset, src_st.st_size,
534 if (fstat(dst_fd, &dst_st) < 0) {
535 CT_ERROR("'%s' stat failed (%s)\n", dst, strerror(errno));
539 if (!S_ISREG(dst_st.st_mode)) {
540 CT_ERROR("'%s' not a regular file\n", dst);
544 rc = lseek(dst_fd, hai->hai_extent.offset, SEEK_SET);
546 CT_ERROR("'%s' seek to write to "LPU64" failed (%s)\n", src,
547 hai->hai_extent.offset, strerror(errno));
552 he.offset = hai->hai_extent.offset;
554 rc = llapi_hsm_action_progress(hcp, &he, 0);
556 /* Action has been canceled or something wrong
557 * is happening. Stop copying data. */
558 CT_ERROR("%s->'%s' progress returned err %d\n", src, dst, rc);
563 /* Don't read beyond a given extent */
564 rlen = (hai->hai_extent.length == -1LL) ?
565 src_st.st_size : hai->hai_extent.length;
567 while (wpos < rlen) {
568 int chunk = (rlen - wpos > opt.o_chunk_size) ?
569 opt.o_chunk_size : rlen - wpos;
571 /* Only read more if we wrote everything in the buffer */
573 rsize = nonblock_rw(0, src_fd, buf, chunk);
578 if (rsize == -EAGAIN) {
582 /* Haven't read anything yet, let's
583 * give it back to the coordinator
584 * for rescheduling */
591 CT_ERROR("'%s' read failed (%s)\n", src,
601 wsize = nonblock_rw(1, dst_fd, buf + bufoff, rpos - wpos);
602 if (wsize == -EAGAIN)
607 CT_ERROR("'%s' write failed (%s)\n", dst,
616 if (opt.o_bandwidth != 0) {
617 static unsigned long long tot_bytes;
618 static time_t start_time, last_time;
619 time_t now = time(0);
620 double tot_time, excess;
621 unsigned int sleep_time;
623 if (now > last_time + 5) {
625 start_time = last_time = now;
629 tot_time = now - start_time;
633 excess = tot_bytes - tot_time * opt.o_bandwidth;
634 sleep_time = excess * 1000000 / opt.o_bandwidth;
635 if ((now - start_time) % 10 == 1)
636 CT_TRACE("bandwith control: excess=%E"
646 if (time(0) >= last_print_time + opt.o_report_int) {
647 last_print_time = time(0);
648 CT_TRACE("%%"LPU64" ", 100 * wpos / rlen);
650 rc = llapi_hsm_action_progress(hcp, &he, 0);
652 /* Action has been canceled or something wrong
653 * is happening. Stop copying data. */
654 CT_ERROR("%s->'%s' progress returned err %d\n",
665 * truncate restored file
666 * size is taken from the archive this is done to support
667 * restore after a force release which leaves the file with the
668 * wrong size (can big bigger than the new size)
670 if ((hai->hai_action == HSMA_RESTORE) &&
671 (src_st.st_size < dst_st.st_size)) {
673 * make sure the file is on disk before reporting success.
675 rc = ftruncate(dst_fd, src_st.st_size);
678 CT_ERROR("'%s' final truncate to %lu failed (%s)\n",
679 dst, src_st.st_size, strerror(-rc));
688 CT_ERROR("'%s' fsync failed (%s)\n", dst,
699 /* Copy file attributes from file src to file dest */
700 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
704 struct timeval times[2];
706 if (fstat(src_fd, &st) < 0) {
707 CT_ERROR("'%s' stat failed (%s)\n",
708 src, strerror(errno));
712 times[0].tv_sec = st.st_atime;
713 times[0].tv_usec = 0;
714 times[1].tv_sec = st.st_mtime;
715 times[1].tv_usec = 0;
716 if (fchmod(dst_fd, st.st_mode) < 0 ||
717 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
718 futimes(dst_fd, times) < 0)
719 CT_ERROR("'%s' fchmod fchown or futimes failed (%s)\n", src,
725 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
726 int dst_fd, bool is_restore)
728 char list[XATTR_LIST_MAX];
729 char value[XATTR_SIZE_MAX];
734 list_len = flistxattr(src_fd, list, sizeof(list));
739 while (name < list + list_len) {
740 rc = fgetxattr(src_fd, name, value, sizeof(value));
744 /* when we restore, we do not restore lustre xattr */
746 (strncmp(XATTR_TRUSTED_PREFIX, name,
747 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
748 rc = fsetxattr(dst_fd, name, value, rc, 0);
749 CT_TRACE("'%s' fsetxattr of '%s' rc=%d (%s)\n",
750 dst, name, rc, strerror(errno));
751 /* lustre.* attrs aren't supported on other FS's */
752 if (rc < 0 && errno != EOPNOTSUPP) {
753 CT_ERROR("'%s' fsetxattr of '%s' failed (%s)\n",
754 dst, name, strerror(errno));
758 name += strlen(name) + 1;
764 static int ct_path_lustre(char *buf, int sz, const char *mnt,
765 const lustre_fid *fid)
767 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
768 dot_lustre_name, PFID(fid));
771 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
772 const lustre_fid *fid)
774 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
775 DFID_NOBRACE, archive_dir,
776 (fid)->f_oid & 0xFFFF,
777 (fid)->f_oid >> 16 & 0xFFFF,
778 (unsigned int)((fid)->f_seq & 0xFFFF),
779 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
780 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
781 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
785 static bool ct_is_retryable(int err)
787 return err == -ETIMEDOUT;
790 static int ct_begin(struct hsm_copyaction_private **phcp,
791 const struct hsm_action_item *hai)
796 rc = llapi_hsm_action_begin(phcp, ctdata, hai, false);
798 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
799 CT_ERROR("'%s' copy start failed (%s)\n", src, strerror(-rc));
805 static int ct_fini(struct hsm_copyaction_private **phcp,
806 const struct hsm_action_item *hai, int flags, int ct_rc)
811 CT_TRACE("Action completed, notifying coordinator "
812 "cookie="LPX64", FID="DFID", flags=%d err=%d\n",
813 hai->hai_cookie, PFID(&hai->hai_fid),
816 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
817 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, flags, abs(ct_rc));
818 if (rc == -ECANCELED)
819 CT_ERROR("'%s' completed action has been canceled: "
820 "cookie="LPX64", FID="DFID"\n", lstr, hai->hai_cookie,
821 PFID(&hai->hai_fid));
823 CT_ERROR("'%s' copy end failed (%s)\n", lstr, strerror(-rc));
825 CT_TRACE("'%s' copy end ok (rc=%d)\n", lstr, rc);
830 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
832 struct hsm_copyaction_private *hcp = NULL;
837 bool rename_needed = false;
843 rc = ct_begin(&hcp, hai);
847 /* we fill archive so:
849 * destination = lustre FID
851 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
852 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
853 if (hai->hai_extent.length == -1) {
854 /* whole file, write it to tmp location and atomically
855 * replace old archived file */
856 strncat(dst, "_tmp", sizeof(dst) - strlen(dst) - 1);
857 /* we cannot rely on the same test because ct_copy_data()
858 * updates hai_extent.length */
859 rename_needed = true;
862 CT_TRACE("'%s' archived to %s\n", src, dst);
869 rc = ct_mkdir_p(dst);
871 CT_ERROR("'%s' mkdir_p failed (%s)\n", dst, strerror(-rc));
875 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
877 CT_ERROR("'%s' open read failed (%s)\n", src, strerror(errno));
882 open_flags = O_WRONLY | O_NOFOLLOW | O_NONBLOCK;
883 /* If extent is specified, don't truncate an old archived copy */
884 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
886 dst_fd = open(dst, open_flags, FILE_PERM);
888 CT_ERROR("'%s' open write failed (%s)\n", dst, strerror(errno));
893 /* saving stripe is not critical */
894 rc = ct_save_stripe(src_fd, src, dst);
896 CT_ERROR("'%s' cannot save file striping info in '%s' (%s)\n",
897 src, dst, strerror(-rc));
899 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
901 CT_ERROR("'%s' data copy failed to '%s' (%s)\n",
902 src, dst, strerror(-rc));
906 CT_TRACE("'%s' data archived to '%s' done\n", src, dst);
908 /* attrs will remain on the MDS; no need to copy them, except possibly
909 for disaster recovery */
910 if (opt.o_copy_attrs) {
911 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
913 CT_ERROR("'%s' attr copy failed to '%s' (%s)\n",
914 src, dst, strerror(-rc));
917 CT_TRACE("'%s' attr file copied to archive '%s'\n",
921 /* xattrs will remain on the MDS; no need to copy them, except possibly
922 for disaster recovery */
923 if (opt.o_copy_xattrs) {
924 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
926 CT_ERROR("'%s' xattr copy failed to '%s' (%s)\n",
927 src, dst, strerror(-rc));
928 rcf = rcf ? rcf : rc;
930 CT_ERROR("'%s' xattr file copied to archive '%s'\n",
934 if (rename_needed == true) {
935 char tmp_src[PATH_MAX];
936 char tmp_dst[PATH_MAX];
938 /* atomically replace old archived file */
939 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
941 rc = rename(dst, src);
943 CT_ERROR("'%s' renamed to '%s' failed (%s)\n", dst, src,
948 /* rename lov file */
949 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
950 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
951 rc = rename(tmp_dst, tmp_src);
953 CT_ERROR("'%s' renamed to '%s' failed (%s)\n",
954 tmp_dst, tmp_src, strerror(errno));
957 if (opt.o_shadow_tree) {
958 /* Create a namespace of softlinks that shadows the original
959 * Lustre namespace. This will only be current at
960 * time-of-archive (won't follow renames).
961 * WARNING: release won't kill these links; a manual
962 * cleanup of dead links would be required.
965 long long recno = -1;
971 sprintf(buf, DFID, PFID(&hai->hai_fid));
972 sprintf(src, "%s/shadow/", opt.o_hsm_root);
974 ptr = opt.o_hsm_root;
976 (*ptr++ == '/') ? depth-- : 0;
978 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
979 sizeof(src) - strlen(src), &recno, &linkno);
981 CT_ERROR("'%s' fid2path failed (%s)\n", buf,
983 rcf = rcf ? rcf : rc;
987 /* Figure out how many parent dirs to symlink back */
990 (*ptr++ == '/') ? depth++ : 0;
995 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
997 if (ct_mkdir_p(src)) {
998 CT_ERROR("'%s' mkdir_p failed (%s)\n", src,
1000 rcf = rcf ? rcf : -errno;
1003 /* symlink already exists ? */
1004 sz = readlink(src, buf, sizeof(buf));
1007 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1008 if (unlink(src) && errno != ENOENT) {
1009 CT_ERROR("'%s' unlink symlink failed "
1012 rcf = rcf ? rcf : -errno;
1014 /* unlink old symlink done */
1015 CT_TRACE("'%s' remove old symlink pointing"
1016 " to '%s'\n", src, buf);
1019 /* symlink already ok */
1020 CT_TRACE("'%s' symlink already pointing"
1021 " to '%s'\n", src, dst);
1026 if (symlink(dst, src)) {
1027 CT_ERROR("'%s' symlink to '%s' failed (%s)\n", src, dst,
1029 rcf = rcf ? rcf : -errno;
1032 CT_TRACE("'%s' symlink to '%s' done\n", src, dst);
1044 if (ct_is_retryable(rc))
1045 ct_flags |= HP_FLAG_RETRY;
1057 rc = ct_fini(&hcp, hai, ct_flags, rcf);
1062 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1064 struct hsm_copyaction_private *hcp = NULL;
1073 rc = ct_begin(&hcp, hai);
1077 /* we fill lustre so:
1078 * source = lustre FID in the backend
1079 * destination = data FID = volatile file
1082 /* build backend file name from released file FID */
1083 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1085 /* get the FID of the volatile file */
1086 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1088 CT_ERROR("restoring "DFID", cannot get FID of created volatile"
1089 " file (%s)\n", PFID(&hai->hai_fid), strerror(-rc));
1093 /* build volatile "file name", for messages */
1094 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1096 CT_TRACE("'%s' restore data to '%s'\n", src, dst);
1098 if (opt.o_dry_run) {
1103 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
1105 CT_ERROR("'%s' open for read failed (%s)\n", src,
1111 dst_fd = llapi_hsm_action_get_fd(hcp);
1113 /* the layout cannot be allocated through .fid so we have to
1114 * restore a layout */
1115 rc = ct_restore_stripe(src, dst, dst_fd);
1117 CT_ERROR("'%s' cannot restore file striping info from '%s'"
1118 " (%s)\n", dst, src, strerror(-rc));
1123 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1125 CT_ERROR("'%s' data copy to '%s' failed (%s)\n", src, dst,
1128 if (ct_is_retryable(rc))
1129 flags |= HP_FLAG_RETRY;
1133 CT_TRACE("'%s' data restore done to %s\n", src, dst);
1137 rc = ct_fini(&hcp, hai, flags, rc);
1139 /* object swaping is done by cdt at copy end, so close of volatile file
1140 * cannot be done before */
1150 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1152 struct hsm_copyaction_private *hcp = NULL;
1156 rc = ct_begin(&hcp, hai);
1160 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1162 CT_TRACE("'%s' removed file\n", dst);
1164 if (opt.o_dry_run) {
1172 CT_ERROR("'%s' unlink failed (%s)\n", dst, strerror(-rc));
1179 rc = ct_fini(&hcp, hai, 0, rc);
1184 static int ct_report_error(const struct hsm_action_item *hai, int flags,
1187 struct hsm_copyaction_private *hcp;
1190 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, true);
1194 rc = llapi_hsm_action_end(&hcp, &hai->hai_extent, flags, abs(errval));
1199 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1203 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1204 /* Print the original path */
1206 char path[PATH_MAX];
1207 long long recno = -1;
1210 sprintf(fid, DFID, PFID(&hai->hai_fid));
1211 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64"\n",
1212 fid, hsm_copytool_action2name(hai->hai_action),
1213 hai->hai_len, hai->hai_cookie);
1214 rc = llapi_fid2path(opt.o_mnt, fid, path,
1215 sizeof(path), &recno, &linkno);
1217 CT_ERROR("'%s' fid2path failed (%s)\n", fid,
1220 CT_TRACE("'%s' processing file\n", path);
1223 switch (hai->hai_action) {
1224 /* set err_major, minor inside these functions */
1226 rc = ct_archive(hai, hal_flags);
1229 rc = ct_restore(hai, hal_flags);
1232 rc = ct_remove(hai, hal_flags);
1235 CT_TRACE("'%s' cancel not implemented\n", opt.o_mnt);
1236 /* Don't report progress to coordinator for this cookie:
1237 * the copy function will get ECANCELED when reporting
1243 CT_ERROR("'%s' unknown action %d\n", opt.o_mnt,
1246 ct_report_error(hai, 0, -EINVAL);
1254 struct hsm_action_item *hai;
1257 static void *ct_thread(void *data)
1259 struct ct_th_data *cttd = data;
1262 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1266 pthread_exit((void *)(intptr_t)rc);
1269 static int ct_process_item_async(const struct hsm_action_item *hai,
1272 pthread_attr_t attr;
1274 struct ct_th_data *data;
1277 data = malloc(sizeof(*data));
1281 data->hai = malloc(hai->hai_len);
1282 if (data->hai == NULL) {
1287 memcpy(data->hai, hai, hai->hai_len);
1288 data->hal_flags = hal_flags;
1290 rc = pthread_attr_init(&attr);
1292 CT_ERROR("'%s' pthread_attr_init: %s\n", opt.o_mnt,
1299 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1301 rc = pthread_create(&thread, &attr, ct_thread, data);
1303 CT_ERROR("'%s' thread create: (%s)\n", opt.o_mnt, strerror(rc));
1305 pthread_attr_destroy(&attr);
1309 static int ct_import_one(const char *src, const char *dst)
1311 char newarc[PATH_MAX];
1316 CT_TRACE("'%s' importing from %s\n", dst, src);
1318 if (stat(src, &st) < 0) {
1319 CT_ERROR("'%s' stat failed (%s)\n", src, strerror(errno));
1326 rc = llapi_hsm_import(dst,
1327 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1328 &st, 0, 0, 0, 0, NULL, &fid);
1330 CT_ERROR("'%s' import from '%s' failed (%s)\n", dst, src,
1335 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1337 rc = ct_mkdir_p(newarc);
1339 CT_ERROR("'%s' mkdir_p failed (%s)\n", newarc, strerror(-rc));
1345 /* Lots of choices now: mv, ln, ln -s ? */
1346 rc = link(src, newarc); /* hardlink */
1348 CT_ERROR("'%s' link to '%s' failed (%s)\n", newarc, src,
1353 CT_TRACE("'%s' imported from '%s'=='%s'\n", dst, newarc, src);
1358 static char *path_concat(const char *dirname, const char *basename)
1361 int dirlen = strlen(dirname);
1363 result = malloc(dirlen + strlen(basename) + 2);
1367 memcpy(result, dirname, dirlen);
1368 result[dirlen] = '/';
1369 strcpy(result + dirlen + 1, basename);
1374 static int ct_import_recurse(const char *relpath)
1377 struct dirent ent, *cookie = NULL;
1378 char *srcpath, *newpath;
1381 if (relpath == NULL)
1384 srcpath = path_concat(opt.o_hsm_root, relpath);
1385 if (srcpath == NULL) {
1390 dir = opendir(srcpath);
1392 /* Not a dir, or error */
1393 if (errno == ENOTDIR) {
1394 /* Single regular file case, treat o_dst as absolute
1396 rc = ct_import_one(srcpath, opt.o_dst);
1398 CT_ERROR("'%s' opendir failed (%s)\n", srcpath,
1409 rc = readdir_r(dir, &ent, &cookie);
1411 CT_ERROR("'%s' readdir_r failed (%s)\n", relpath,
1416 } else if ((rc == 0) && (cookie == NULL)) {
1417 /* end of directory */
1421 if (!strcmp(ent.d_name, ".") ||
1422 !strcmp(ent.d_name, ".."))
1425 /* New relative path */
1426 newpath = path_concat(relpath, ent.d_name);
1427 if (newpath == NULL) {
1433 if (ent.d_type == DT_DIR) {
1434 rc = ct_import_recurse(newpath);
1439 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1440 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1441 /* Make the target dir in the Lustre fs */
1442 rc = ct_mkdir_p(dst);
1444 /* Import the file */
1445 rc = ct_import_one(src, dst);
1447 CT_ERROR("'%s' ct_mkdir_p failed (%s)\n", dst,
1454 CT_ERROR("'%s' importing failed\n", newpath);
1455 if (err_major && opt.o_abort_on_error) {
1469 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1474 CT_TRACE("rebind "DFID" to "DFID"\n", PFID(old_fid), PFID(new_fid));
1476 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1477 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1479 if (!opt.o_dry_run) {
1481 if (rename(src, dst)) {
1482 CT_ERROR("'%s' rename to '%s' failed (%s)\n", src, dst,
1486 /* rename lov file */
1487 strncat(src, ".lov", sizeof(src) - strlen(src) - 1);
1488 strncat(dst, ".lov", sizeof(dst) - strlen(dst) - 1);
1489 if (rename(src, dst))
1490 CT_ERROR("'%s' rename to '%s' failed (%s)\n", src, dst,
1497 static bool fid_is_file(lustre_fid *fid)
1499 return fid_is_norm(fid) || fid_is_igif(fid);
1502 static bool should_ignore_line(const char *line)
1506 for (i = 0; line[i] != '\0'; i++) {
1507 if (isspace(line[i]))
1509 else if (line[i] == '#')
1518 static int ct_rebind_list(const char *list)
1524 size_t line_size = 0;
1525 unsigned int nl = 0;
1526 unsigned int ok = 0;
1528 filp = fopen(list, "r");
1530 CT_ERROR("'%s' open failed (%s)\n", list, strerror(errno));
1534 /* each line consists of 2 FID */
1535 while ((r = getline(&line, &line_size, filp)) != -1) {
1539 /* Ignore empty and commented out ('#...') lines. */
1540 if (should_ignore_line(line))
1545 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1546 if (rc != 6 || !fid_is_file(&old_fid) ||
1547 !fid_is_file(&new_fid)) {
1548 CT_ERROR("'%s' FID expected near '%s', line %u\n",
1554 if (ct_rebind_one(&old_fid, &new_fid))
1565 /* return 0 if all rebinds were sucessful */
1566 CT_TRACE("'%s' %u lines read, %u rebind successful\n", list, nl, ok);
1568 return ok == nl ? 0 : -1;
1571 static int ct_rebind(void)
1579 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1580 !fid_is_file(&old_fid)) {
1581 CT_ERROR("'%s' invalid FID format\n", opt.o_src);
1585 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1586 !fid_is_file(&new_fid)) {
1587 CT_ERROR("'%s' invalid FID format\n", opt.o_dst);
1591 rc = ct_rebind_one(&old_fid, &new_fid);
1596 /* o_src is a list file */
1597 rc = ct_rebind_list(opt.o_src);
1602 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1607 struct dirent ent, *cookie = NULL;
1611 dir = opendir(dirpath);
1614 CT_ERROR("'%s' failed to open directory (%s)\n", opt.o_hsm_root,
1619 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1621 /* end of directory.
1622 * rc is 0 and seqmax contains the max value. */
1625 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1628 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1629 CT_TRACE("'%s' unexpected dirname format, "
1630 "skip entry.\n", ent.d_name);
1633 if (sub_seq > *sub_seqmax)
1634 *sub_seqmax = sub_seq;
1637 CT_ERROR("'%s' readdir_r failed (%s)\n", dirpath, strerror(-rc));
1644 static int ct_max_sequence(void)
1647 char path[PATH_MAX];
1651 strncpy(path, opt.o_hsm_root, sizeof(path));
1652 /* FID sequence is stored in top-level directory names:
1653 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1655 for (i = 0; i < 4; i++) {
1656 rc = ct_dir_level_max(path, &subseq);
1659 seq |= ((__u64)subseq << ((3 - i) * 16));
1660 sprintf(path + strlen(path), "/%04x", subseq);
1663 printf("max_sequence: %016Lx\n", seq);
1668 static void handler(int signal)
1670 psignal(signal, "exiting");
1671 /* If we don't clean up upon interrupt, umount thinks there's a ref
1672 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1673 * does successfully unmount and the mount is actually gone, but the
1674 * mtab entry remains. So this just makes mtab happier. */
1675 llapi_hsm_copytool_unregister(&ctdata);
1679 /* Daemon waits for messages from the kernel; run it in the background. */
1680 static int ct_daemon(void)
1686 CT_ERROR("%d: cannot start as daemon (%s)", getpid(),
1691 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt, 0,
1692 opt.o_archive_cnt, opt.o_archive_id);
1694 CT_ERROR("%d: cannot start copytool interface: %s\n", getpid(),
1699 signal(SIGINT, handler);
1700 signal(SIGTERM, handler);
1703 struct hsm_action_list *hal;
1704 struct hsm_action_item *hai;
1708 CT_TRACE("%d: waiting for message from kernel\n", getpid());
1710 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1711 if (rc == -ESHUTDOWN) {
1712 CT_TRACE("%d: shutting down", getpid());
1714 } else if (rc == -EAGAIN) {
1715 continue; /* msg not for us */
1716 } else if (rc < 0) {
1717 CT_WARN("%d: message receive: (%s)\n", getpid(),
1720 if (opt.o_abort_on_error)
1726 CT_TRACE("%d: copytool fs=%s archive#=%d item_count=%d\n",
1727 getpid(), hal->hal_fsname, hal->hal_archive_id,
1730 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1731 CT_ERROR("'%s' invalid fs name, expecting: %s\n",
1732 hal->hal_fsname, fs_name);
1734 if (opt.o_abort_on_error)
1740 hai = hai_zero(hal);
1741 while (++i <= hal->hal_count) {
1742 if ((char *)hai - (char *)hal > msgsize) {
1743 CT_ERROR("'%s' item %d past end of message!\n",
1749 rc = ct_process_item_async(hai, hal->hal_flags);
1751 CT_ERROR("'%s' item %d process err: %s\n",
1752 opt.o_mnt, i, strerror(-rc));
1753 if (opt.o_abort_on_error && err_major)
1755 hai = hai_next(hai);
1758 llapi_hsm_action_list_free(&hal);
1760 if (opt.o_abort_on_error && err_major)
1764 llapi_hsm_copytool_unregister(&ctdata);
1769 static int ct_setup(void)
1773 /* set llapi message level */
1774 llapi_msg_set_level(opt.o_verbose);
1776 arc_fd = open(opt.o_hsm_root, O_DIRECTORY);
1778 CT_ERROR("cannot open archive at '%s': %s\n", opt.o_hsm_root,
1783 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1785 CT_ERROR("cannot find a Lustre filesystem mounted at: %s\n",
1793 static int ct_cleanup(void)
1798 if (close(arc_fd) < 0) {
1799 CT_ERROR("cannot close archive: %s.\n", strerror(errno));
1806 int main(int argc, char **argv)
1810 strncpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1811 rc = ct_parseopts(argc, argv);
1813 CT_ERROR("try '%s --help' for more information.\n", cmd_name);
1819 switch (opt.o_action) {
1821 rc = ct_import_recurse(opt.o_src);
1827 rc = ct_max_sequence();
1833 CT_ERROR("no action specified. Try '%s --help' for more "
1834 "information.\n", cmd_name);
1839 if (opt.o_action != CA_MAXSEQ)
1840 CT_TRACE("%s(%d) finished, errs: %d major, %d minor, "
1841 "rc=%d (%s)\n", argv[0], getpid(), err_major,
1842 err_minor, rc, strerror(-rc));