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 <sys/syscall.h>
42 #include <sys/types.h>
43 #include <lustre/lustre_idl.h>
44 #include <lustre/lustreapi.h>
46 /* Progress reporting period */
47 #define REPORT_INTERVAL_DEFAULT 30
48 /* HSM hash subdir permissions */
49 #define DIR_PERM S_IRWXU
50 /* HSM hash file permissions */
51 #define FILE_PERM (S_IRUSR | S_IWUSR)
53 #define ONE_MB 0x100000
55 /* copytool uses a 32b bitmask field to register with kuc
56 * archive num = 0 => all
57 * archive num from 1 to 32
59 #define MAX_ARCHIVE_CNT (sizeof(__u32) * 8)
76 int o_archive_id[MAX_ARCHIVE_CNT];
78 unsigned long long o_bandwidth;
80 enum ct_action o_action;
83 char *o_src; /* for import, or rebind */
84 char *o_dst; /* for import, or rebind */
87 /* everything else is zeroed */
88 struct options opt = {
91 .o_verbose = LLAPI_MSG_INFO,
93 .o_report_int = REPORT_INTERVAL_DEFAULT,
94 .o_chunk_size = ONE_MB,
97 /* hsm_copytool_private will hold an open FD on the lustre mount point
98 * for us. Additionally open one on the archive FS root to make sure
99 * it doesn't drop out from under us (and remind the admin to shutdown
100 * the copytool before unmounting). */
102 static int arc_fd = -1;
104 static int err_major;
105 static int err_minor;
107 static char cmd_name[PATH_MAX];
108 static char fs_name[MAX_OBD_NAME + 1];
110 static struct hsm_copytool_private *ctdata;
113 #define CT_ERROR(_rc, _format, ...) \
114 llapi_error(LLAPI_MSG_ERROR, _rc, \
115 "%s[%ld]: "_format, \
116 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
117 #define CT_DEBUG(_format, ...) \
118 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
119 "%s[%ld]: "_format, \
120 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
121 #define CT_WARN(_format, ...) \
122 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
123 "%s[%ld]: "_format, \
124 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
125 #define CT_TRACE(_format, ...) \
126 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
127 "%s[%ld]: "_format, \
128 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
129 #define CT_PRINTF(_format, ...) \
130 llapi_printf(LLAPI_MSG_NORMAL, \
131 "%s[%ld]: "_format, \
132 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
134 static void usage(const char *name, int rc)
137 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
138 "The Lustre HSM Posix copy tool can be used as a daemon or "
139 "as a command line tool\n"
140 "The Lustre HSM daemon acts on action requests from Lustre\n"
141 "to copy files to and from an HSM archive system.\n"
142 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
143 "to an HSM mounted at a given hsm_root.\n"
144 " --daemon Daemon mode, run in background\n"
146 " --no-attr Don't copy file attributes\n"
147 " --no-shadow Don't create shadow namespace in archive\n"
148 " --no-xattr Don't copy file extended attributes\n"
149 "The Lustre HSM tool performs administrator-type actions\n"
150 "on a Lustre HSM archive.\n"
151 "This POSIX-flavored tool can link an existing HSM namespace\n"
152 "into a Lustre filesystem.\n"
154 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
155 " import an archived subtree at\n"
156 " <src> (relative to hsm_root) into the Lustre filesystem at\n"
157 " <dst> (absolute)\n"
158 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
159 " rebind an entry in the HSM to a new FID\n"
160 " <old_FID> old FID the HSM entry is bound to\n"
161 " <new_FID> new FID to bind the HSM entry to\n"
162 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
163 " perform the rebind operation for all FID in the list file\n"
164 " each line of <list_file> consists of <old_FID> <new_FID>\n"
165 " %s [options] --max-sequence <fsname>\n"
166 " return the max fid sequence of archived files\n"
167 " -A, --archive <#> Archive number (repeatable)\n"
168 " -p, --hsm-root <path> Target HSM mount point\n"
169 " -q, --quiet Produce less verbose output\n"
170 " -v, --verbose Produce more verbose output\n"
171 " -c, --chunk-size <sz> I/O size used during data copy\n"
172 " (unit can be used, default is MB)\n"
173 " --abort-on-error Abort operation on major error\n"
174 " --dry-run Don't run, just show what would be done\n"
175 " --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
177 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
182 static int ct_parseopts(int argc, char * const *argv)
184 struct option long_opts[] = {
185 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
186 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
187 {"archive", required_argument, NULL, 'A'},
188 {"bandwidth", required_argument, NULL, 'b'},
189 {"chunk-size", required_argument, NULL, 'c'},
190 {"chunk_size", required_argument, NULL, 'c'},
191 {"daemon", no_argument, &opt.o_daemonize, 1},
192 {"dry-run", no_argument, &opt.o_dry_run, 1},
193 {"help", no_argument, NULL, 'h'},
194 {"hsm-root", required_argument, NULL, 'p'},
195 {"hsm_root", required_argument, NULL, 'p'},
196 {"import", no_argument, NULL, 'i'},
197 {"max-sequence", no_argument, NULL, 'M'},
198 {"max_sequence", no_argument, NULL, 'M'},
199 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
200 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
201 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
202 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
203 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
204 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
205 {"quiet", no_argument, NULL, 'q'},
206 {"rebind", no_argument, NULL, 'r'},
207 {"report", required_argument, &opt.o_report_int, 0},
208 {"verbose", no_argument, NULL, 'v'},
212 unsigned long long value;
213 unsigned long long unit;
216 while ((c = getopt_long(argc, argv, "A:b:c:hiMp:qrv",
217 long_opts, NULL)) != -1) {
220 if ((opt.o_archive_cnt >= MAX_ARCHIVE_CNT) ||
221 (atoi(optarg) >= MAX_ARCHIVE_CNT)) {
223 CT_ERROR(rc, "archive number must be less"
224 "than %zu", MAX_ARCHIVE_CNT);
227 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
230 case 'b': /* -b and -c have both a number with unit as arg */
233 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
235 CT_ERROR(rc, "bad value for -%c '%s'", c,
240 opt.o_chunk_size = value;
242 opt.o_bandwidth = value;
247 opt.o_action = CA_IMPORT;
250 opt.o_action = CA_MAXSEQ;
253 opt.o_hsm_root = optarg;
259 opt.o_action = CA_REBIND;
271 switch (opt.o_action) {
273 /* src dst mount_point */
274 if (argc != optind + 3) {
276 CT_ERROR(rc, "--import requires 2 arguments");
279 opt.o_src = argv[optind++];
280 opt.o_dst = argv[optind++];
283 /* FID1 FID2 mount_point or FILE mount_point */
284 if (argc == optind + 2) {
285 opt.o_src = argv[optind++];
287 } else if (argc == optind + 3) {
288 opt.o_src = argv[optind++];
289 opt.o_dst = argv[optind++];
292 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
298 /* just mount point */
302 if (argc != optind + 1) {
304 CT_ERROR(rc, "no mount point specified");
308 opt.o_mnt = argv[optind];
310 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
311 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
313 if (!opt.o_dry_run && opt.o_hsm_root == NULL) {
315 CT_ERROR(rc, "must specify a root directory for the backend");
319 if (opt.o_action == CA_IMPORT) {
320 if (opt.o_src && opt.o_src[0] == '/') {
323 "source path must be relative to HSM root");
327 if (opt.o_dst && opt.o_dst[0] != '/') {
329 CT_ERROR(rc, "destination path must be absolute");
338 static int ct_mkdir_p(const char *path)
348 while ((ptr = strchr(ptr, '/')) != NULL) {
350 rc = mkdir(saved, DIR_PERM);
352 if (rc < 0 && errno != EEXIST) {
354 CT_ERROR(rc, "cannot mkdir '%s'", path);
366 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
368 char lov_file[PATH_MAX];
369 char lov_buf[XATTR_SIZE_MAX];
370 struct lov_user_md *lum;
375 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
376 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
378 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
380 if (xattr_size < 0) {
382 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
386 lum = (struct lov_user_md *)lov_buf;
388 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
389 lum->lmm_magic == LOV_USER_MAGIC_V3) {
390 /* Set stripe_offset to -1 so that it is not interpreted as a
391 * hint on restore. */
392 lum->lmm_stripe_offset = -1;
395 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
398 CT_ERROR(rc, "cannot open '%s'", lov_file);
402 rc = write(fd, lum, xattr_size);
405 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
406 xattr_size, lov_file);
414 CT_ERROR(rc, "cannot close '%s'", lov_file);
426 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
428 char lov_file[PATH_MAX];
432 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
433 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
435 fd = open(lov_file, O_RDONLY);
437 CT_ERROR(errno, "cannot open '%s'", lov_file);
441 rc = read(fd, lovea, *lovea_size);
443 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
444 *lovea_size, lov_file);
455 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
456 const void *lovea, size_t lovea_size)
460 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
463 CT_ERROR(errno, "cannot set lov EA on '%s'", dst);
470 static void bandwidth_ctl_delay(int wsize)
472 static unsigned long long tot_bytes;
473 static time_t start_time;
474 static time_t last_time;
475 time_t now = time(0);
478 unsigned int sleep_time;
480 if (now > last_time + 5) {
482 start_time = last_time = now;
486 tot_time = now - start_time;
490 excess = tot_bytes - tot_time * opt.o_bandwidth;
491 sleep_time = excess * 1000000 / opt.o_bandwidth;
492 if ((now - start_time) % 10 == 1)
493 CT_TRACE("bandwith control: excess=%E sleep for %dus", excess,
502 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
503 const char *dst, int src_fd, int dst_fd,
504 const struct hsm_action_item *hai, long hal_flags)
506 struct hsm_extent he;
507 __u64 offset = hai->hai_extent.offset;
511 __u64 write_total = 0;
513 time_t last_print_time = time(NULL);
516 CT_TRACE("going to copy data from '%s' to '%s'", src, dst);
518 if (fstat(src_fd, &src_st) < 0) {
520 CT_ERROR(rc, "cannot stat '%s'", src);
524 if (!S_ISREG(src_st.st_mode)) {
526 CT_ERROR(rc, "'%s' is not a regular file", src);
530 if (fstat(dst_fd, &dst_st) < 0) {
532 CT_ERROR(rc, "cannot stat '%s'", dst);
536 if (!S_ISREG(dst_st.st_mode)) {
538 CT_ERROR(rc, "'%s' is not a regular file", dst);
542 rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
546 "cannot seek for read to "LPU64" (len %jd) in '%s'",
547 hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
553 rc = llapi_hsm_action_progress(hcp, &he, 0);
555 /* Action has been canceled or something wrong
556 * is happening. Stop copying data. */
557 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
563 /* Don't read beyond a given extent */
564 length = min(hai->hai_extent.length, src_st.st_size);
566 buf = malloc(opt.o_chunk_size);
572 CT_DEBUG("Going to copy "LPU64" bytes %s -> %s\n", length, src, dst);
574 while (write_total < length) {
577 int chunk = (length - write_total > opt.o_chunk_size) ?
578 opt.o_chunk_size : length - write_total;
580 rsize = pread(src_fd, buf, chunk, offset);
587 CT_ERROR(rc, "cannot read from '%s'", src);
591 wsize = pwrite(dst_fd, buf, rsize, offset);
594 CT_ERROR(rc, "cannot write to '%s'", dst);
598 write_total += wsize;
601 if (opt.o_bandwidth != 0)
602 /* sleep if needed, to honor bandwidth limits */
603 bandwidth_ctl_delay(wsize);
605 if (time(0) >= last_print_time + opt.o_report_int) {
606 last_print_time = time(0);
607 CT_TRACE("%%"LPU64" ", 100 * write_total / length);
608 he.length = write_total;
609 rc = llapi_hsm_action_progress(hcp, &he, 0);
611 /* Action has been canceled or something wrong
612 * is happening. Stop copying data. */
613 CT_ERROR(rc, "progress ioctl for copy"
614 " '%s'->'%s' failed", src, dst);
623 * truncate restored file
624 * size is taken from the archive this is done to support
625 * restore after a force release which leaves the file with the
626 * wrong size (can big bigger than the new size)
628 if ((hai->hai_action == HSMA_RESTORE) &&
629 (src_st.st_size < dst_st.st_size)) {
631 * make sure the file is on disk before reporting success.
633 rc = ftruncate(dst_fd, src_st.st_size);
636 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
637 dst, (intmax_t)src_st.st_size);
648 /* Copy file attributes from file src to file dest */
649 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
653 struct timeval times[2];
656 if (fstat(src_fd, &st) < 0) {
658 CT_ERROR(rc, "cannot stat '%s'", src);
662 times[0].tv_sec = st.st_atime;
663 times[0].tv_usec = 0;
664 times[1].tv_sec = st.st_mtime;
665 times[1].tv_usec = 0;
666 if (fchmod(dst_fd, st.st_mode) < 0 ||
667 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
668 futimes(dst_fd, times) < 0) {
670 CT_ERROR(rc, "cannot set attributes of '%s'", src);
677 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
678 int dst_fd, bool is_restore)
680 char list[XATTR_LIST_MAX];
681 char value[XATTR_SIZE_MAX];
686 list_len = flistxattr(src_fd, list, sizeof(list));
691 while (name < list + list_len) {
692 rc = fgetxattr(src_fd, name, value, sizeof(value));
696 /* when we restore, we do not restore lustre xattr */
698 (strncmp(XATTR_TRUSTED_PREFIX, name,
699 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
700 rc = fsetxattr(dst_fd, name, value, rc, 0);
701 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
702 name, dst, rc, strerror(errno));
703 /* lustre.* attrs aren't supported on other FS's */
704 if (rc < 0 && errno != EOPNOTSUPP) {
706 CT_ERROR(rc, "cannot set extended attribute"
712 name += strlen(name) + 1;
718 static int ct_path_lustre(char *buf, int sz, const char *mnt,
719 const lustre_fid *fid)
721 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
722 dot_lustre_name, PFID(fid));
725 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
726 const lustre_fid *fid)
728 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
729 DFID_NOBRACE, archive_dir,
730 (fid)->f_oid & 0xFFFF,
731 (fid)->f_oid >> 16 & 0xFFFF,
732 (unsigned int)((fid)->f_seq & 0xFFFF),
733 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
734 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
735 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
739 static bool ct_is_retryable(int err)
741 return err == -ETIMEDOUT;
744 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
745 const struct hsm_action_item *hai,
746 int mdt_index, int open_flags)
751 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
754 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
755 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
761 static int ct_begin(struct hsm_copyaction_private **phcp,
762 const struct hsm_action_item *hai)
764 /* Restore takes specific parameters. Call the same function w/ default
765 * values for all other operations. */
766 return ct_begin_restore(phcp, hai, -1, 0);
769 static int ct_fini(struct hsm_copyaction_private **phcp,
770 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
775 CT_TRACE("Action completed, notifying coordinator "
776 "cookie="LPX64", FID="DFID", hp_flags=%d err=%d",
777 hai->hai_cookie, PFID(&hai->hai_fid),
780 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
781 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
782 if (rc == -ECANCELED)
783 CT_ERROR(rc, "completed action on '%s' has been canceled: "
784 "cookie="LPX64", FID="DFID, lstr, hai->hai_cookie,
785 PFID(&hai->hai_fid));
787 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
789 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
795 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
797 struct hsm_copyaction_private *hcp = NULL;
799 char dst[PATH_MAX] = "";
802 bool rename_needed = false;
808 rc = ct_begin(&hcp, hai);
812 /* we fill archive so:
814 * destination = lustre FID
816 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
817 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
818 if (hai->hai_extent.length == -1) {
819 /* whole file, write it to tmp location and atomically
820 * replace old archived file */
821 strncat(dst, "_tmp", sizeof(dst) - strlen(dst) - 1);
822 /* we cannot rely on the same test because ct_copy_data()
823 * updates hai_extent.length */
824 rename_needed = true;
827 CT_TRACE("archiving '%s' to '%s'", src, dst);
834 rc = ct_mkdir_p(dst);
836 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
840 src_fd = llapi_hsm_action_get_fd(hcp);
843 CT_ERROR(rc, "cannot open '%s' for read", src);
847 open_flags = O_WRONLY | O_NOFOLLOW | O_NONBLOCK;
848 /* If extent is specified, don't truncate an old archived copy */
849 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
851 dst_fd = open(dst, open_flags, FILE_PERM);
854 CT_ERROR(rc, "cannot open '%s' for write", dst);
858 /* saving stripe is not critical */
859 rc = ct_save_stripe(src_fd, src, dst);
861 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
864 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
866 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
873 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
877 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
879 /* attrs will remain on the MDS; no need to copy them, except possibly
880 for disaster recovery */
881 if (opt.o_copy_attrs) {
882 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
884 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
888 CT_TRACE("attr file for '%s' saved to archive '%s'",
892 /* xattrs will remain on the MDS; no need to copy them, except possibly
893 for disaster recovery */
894 if (opt.o_copy_xattrs) {
895 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
897 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
899 rcf = rcf ? rcf : rc;
901 CT_TRACE("xattr file for '%s' saved to archive '%s'",
905 if (rename_needed == true) {
906 char tmp_src[PATH_MAX];
907 char tmp_dst[PATH_MAX];
909 /* atomically replace old archived file */
910 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
912 rc = rename(dst, src);
915 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
918 /* rename lov file */
919 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
920 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
921 rc = rename(tmp_dst, tmp_src);
923 CT_ERROR(errno, "cannot rename '%s' to '%s'",
927 if (opt.o_shadow_tree) {
928 /* Create a namespace of softlinks that shadows the original
929 * Lustre namespace. This will only be current at
930 * time-of-archive (won't follow renames).
931 * WARNING: release won't kill these links; a manual
932 * cleanup of dead links would be required.
935 long long recno = -1;
941 sprintf(buf, DFID, PFID(&hai->hai_fid));
942 sprintf(src, "%s/shadow/", opt.o_hsm_root);
944 ptr = opt.o_hsm_root;
946 (*ptr++ == '/') ? depth-- : 0;
948 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
949 sizeof(src) - strlen(src), &recno, &linkno);
951 CT_ERROR(rc, "cannot get FID of '%s'", buf);
952 rcf = rcf ? rcf : rc;
956 /* Figure out how many parent dirs to symlink back */
959 (*ptr++ == '/') ? depth++ : 0;
964 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
966 if (ct_mkdir_p(src)) {
967 CT_ERROR(errno, "mkdir_p '%s' failed", src);
968 rcf = rcf ? rcf : -errno;
971 /* symlink already exists ? */
972 sz = readlink(src, buf, sizeof(buf));
973 /* detect truncation */
974 if (sz == sizeof(buf)) {
975 rcf = rcf ? rcf : -E2BIG;
976 CT_ERROR(rcf, "readlink '%s' truncated", src);
981 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
982 if (unlink(src) && errno != ENOENT) {
984 "cannot unlink symlink '%s'",
986 rcf = rcf ? rcf : -errno;
988 /* unlink old symlink done */
989 CT_TRACE("remove old symlink '%s' pointing"
990 " to '%s'", src, buf);
993 /* symlink already ok */
994 CT_TRACE("symlink '%s' already pointing"
995 " to '%s'", src, dst);
1000 if (symlink(dst, src)) {
1001 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1003 rcf = rcf ? rcf : -errno;
1006 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1018 if (ct_is_retryable(rc))
1019 hp_flags |= HP_FLAG_RETRY;
1031 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1036 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1038 struct hsm_copyaction_private *hcp = NULL;
1041 char lov_buf[XATTR_SIZE_MAX];
1042 size_t lov_size = sizeof(lov_buf);
1047 int mdt_index = -1; /* Not implemented */
1051 /* we fill lustre so:
1052 * source = lustre FID in the backend
1053 * destination = data FID = volatile file
1056 /* build backend file name from released file FID */
1057 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1059 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1060 * dependency on the structure format. */
1061 rc = ct_load_stripe(src, lov_buf, &lov_size);
1063 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1064 src, strerror(-rc));
1067 open_flags |= O_LOV_DELAY_CREATE;
1071 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1075 /* get the FID of the volatile file */
1076 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1078 CT_ERROR(rc, "restoring "DFID
1079 ", cannot get FID of created volatile file",
1080 PFID(&hai->hai_fid));
1084 /* build volatile "file name", for messages */
1085 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1087 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1089 if (opt.o_dry_run) {
1094 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
1097 CT_ERROR(rc, "cannot open '%s' for read", src);
1101 dst_fd = llapi_hsm_action_get_fd(hcp);
1104 /* the layout cannot be allocated through .fid so we have to
1105 * restore a layout */
1106 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1108 CT_ERROR(rc, "cannot restore file striping info"
1109 " for '%s' from '%s'", dst, src);
1115 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1117 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1120 if (ct_is_retryable(rc))
1121 hp_flags |= HP_FLAG_RETRY;
1125 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1129 rc = ct_fini(&hcp, hai, hp_flags, rc);
1131 /* object swaping is done by cdt at copy end, so close of volatile file
1132 * cannot be done before */
1142 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1144 struct hsm_copyaction_private *hcp = NULL;
1148 rc = ct_begin(&hcp, hai);
1152 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1154 CT_TRACE("removing file '%s'", dst);
1156 if (opt.o_dry_run) {
1164 CT_ERROR(rc, "cannot unlink '%s'", dst);
1169 strncat(dst, ".lov", sizeof(dst) - strlen(dst) - 1);
1173 CT_ERROR(rc, "cannot unlink '%s'", dst);
1180 rc = ct_fini(&hcp, hai, 0, rc);
1185 static int ct_report_error(const struct hsm_action_item *hai, int flags,
1188 struct hsm_copyaction_private *hcp;
1191 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
1195 rc = llapi_hsm_action_end(&hcp, &hai->hai_extent, flags, abs(errval));
1200 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1204 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1205 /* Print the original path */
1207 char path[PATH_MAX];
1208 long long recno = -1;
1211 sprintf(fid, DFID, PFID(&hai->hai_fid));
1212 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64,
1213 fid, hsm_copytool_action2name(hai->hai_action),
1214 hai->hai_len, hai->hai_cookie);
1215 rc = llapi_fid2path(opt.o_mnt, fid, path,
1216 sizeof(path), &recno, &linkno);
1218 CT_ERROR(rc, "cannot get path of FID %s", fid);
1220 CT_TRACE("processing file '%s'", 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("cancel not implemented for file system '%s'",
1237 /* Don't report progress to coordinator for this cookie:
1238 * the copy function will get ECANCELED when reporting
1245 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1248 ct_report_error(hai, 0, rc);
1256 struct hsm_action_item *hai;
1259 static void *ct_thread(void *data)
1261 struct ct_th_data *cttd = data;
1264 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1268 pthread_exit((void *)(intptr_t)rc);
1271 static int ct_process_item_async(const struct hsm_action_item *hai,
1274 pthread_attr_t attr;
1276 struct ct_th_data *data;
1279 data = malloc(sizeof(*data));
1283 data->hai = malloc(hai->hai_len);
1284 if (data->hai == NULL) {
1289 memcpy(data->hai, hai, hai->hai_len);
1290 data->hal_flags = hal_flags;
1292 rc = pthread_attr_init(&attr);
1294 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1301 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1303 rc = pthread_create(&thread, &attr, ct_thread, data);
1305 CT_ERROR(rc, "cannot create thread for '%s' service",
1308 pthread_attr_destroy(&attr);
1312 static int ct_import_one(const char *src, const char *dst)
1314 char newarc[PATH_MAX];
1319 CT_TRACE("importing '%s' from '%s'", dst, src);
1321 if (stat(src, &st) < 0) {
1323 CT_ERROR(rc, "cannot stat '%s'", src);
1330 rc = llapi_hsm_import(dst,
1331 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1332 &st, 0, 0, 0, 0, NULL, &fid);
1334 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1338 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1340 rc = ct_mkdir_p(newarc);
1342 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1348 /* Lots of choices now: mv, ln, ln -s ? */
1349 rc = link(src, newarc); /* hardlink */
1352 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1356 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1361 static char *path_concat(const char *dirname, const char *basename)
1364 int dirlen = strlen(dirname);
1366 result = malloc(dirlen + strlen(basename) + 2);
1370 memcpy(result, dirname, dirlen);
1371 result[dirlen] = '/';
1372 strcpy(result + dirlen + 1, basename);
1377 static int ct_import_recurse(const char *relpath)
1380 struct dirent ent, *cookie = NULL;
1381 char *srcpath, *newpath;
1384 if (relpath == NULL)
1387 srcpath = path_concat(opt.o_hsm_root, relpath);
1388 if (srcpath == NULL) {
1393 dir = opendir(srcpath);
1395 /* Not a dir, or error */
1396 if (errno == ENOTDIR) {
1397 /* Single regular file case, treat o_dst as absolute
1399 rc = ct_import_one(srcpath, opt.o_dst);
1402 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1411 rc = readdir_r(dir, &ent, &cookie);
1414 CT_ERROR(rc, "cannot readdir_r '%s'", relpath);
1417 } else if ((rc == 0) && (cookie == NULL)) {
1418 /* end of directory */
1422 if (!strcmp(ent.d_name, ".") ||
1423 !strcmp(ent.d_name, ".."))
1426 /* New relative path */
1427 newpath = path_concat(relpath, ent.d_name);
1428 if (newpath == NULL) {
1434 if (ent.d_type == DT_DIR) {
1435 rc = ct_import_recurse(newpath);
1440 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1441 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1442 /* Make the target dir in the Lustre fs */
1443 rc = ct_mkdir_p(dst);
1445 /* Import the file */
1446 rc = ct_import_one(src, dst);
1448 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1454 CT_ERROR(rc, "cannot import '%s'", 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)
1475 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1477 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1478 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1480 if (!opt.o_dry_run) {
1482 if (rename(src, dst)) {
1484 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1487 /* rename lov file */
1488 strncat(src, ".lov", sizeof(src) - strlen(src) - 1);
1489 strncat(dst, ".lov", sizeof(dst) - strlen(dst) - 1);
1490 if (rename(src, dst))
1491 CT_ERROR(errno, "cannot '%s' rename to '%s'", 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");
1531 CT_ERROR(rc, "cannot open '%s'", list);
1535 /* each line consists of 2 FID */
1536 while ((r = getline(&line, &line_size, filp)) != -1) {
1540 /* Ignore empty and commented out ('#...') lines. */
1541 if (should_ignore_line(line))
1546 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1547 if (rc != 6 || !fid_is_file(&old_fid) ||
1548 !fid_is_file(&new_fid)) {
1550 "'%s' FID expected near '%s', line %u",
1556 if (ct_rebind_one(&old_fid, &new_fid))
1567 /* return 0 if all rebinds were sucessful */
1568 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1570 return ok == nl ? 0 : -1;
1573 static int ct_rebind(void)
1581 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1582 !fid_is_file(&old_fid)) {
1584 CT_ERROR(rc, "'%s' invalid FID format", opt.o_src);
1588 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1589 !fid_is_file(&new_fid)) {
1591 CT_ERROR(rc, "'%s' invalid FID format", opt.o_dst);
1595 rc = ct_rebind_one(&old_fid, &new_fid);
1600 /* o_src is a list file */
1601 rc = ct_rebind_list(opt.o_src);
1606 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1611 struct dirent ent, *cookie = NULL;
1615 dir = opendir(dirpath);
1618 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1622 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1624 /* end of directory.
1625 * rc is 0 and seqmax contains the max value. */
1628 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1631 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1632 CT_TRACE("'%s' has an unexpected dirname format, "
1633 "skip entry", ent.d_name);
1636 if (sub_seq > *sub_seqmax)
1637 *sub_seqmax = sub_seq;
1640 CT_ERROR(rc, "cannot readdir_r '%s'", dirpath);
1647 static int ct_max_sequence(void)
1650 char path[PATH_MAX];
1654 strncpy(path, opt.o_hsm_root, sizeof(path));
1655 /* FID sequence is stored in top-level directory names:
1656 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1658 for (i = 0; i < 4; i++) {
1659 rc = ct_dir_level_max(path, &subseq);
1662 seq |= ((__u64)subseq << ((3 - i) * 16));
1663 sprintf(path + strlen(path), "/%04x", subseq);
1666 printf("max_sequence: %016Lx\n", seq);
1671 static void handler(int signal)
1673 psignal(signal, "exiting");
1674 /* If we don't clean up upon interrupt, umount thinks there's a ref
1675 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1676 * does successfully unmount and the mount is actually gone, but the
1677 * mtab entry remains. So this just makes mtab happier. */
1678 llapi_hsm_copytool_unregister(&ctdata);
1682 /* Daemon waits for messages from the kernel; run it in the background. */
1683 static int ct_run(void)
1687 if (opt.o_daemonize) {
1691 CT_ERROR(rc, "cannot daemonize");
1696 setbuf(stdout, NULL);
1698 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt, 0,
1699 opt.o_archive_cnt, opt.o_archive_id);
1701 CT_ERROR(rc, "cannot start copytool interface");
1705 signal(SIGINT, handler);
1706 signal(SIGTERM, handler);
1709 struct hsm_action_list *hal;
1710 struct hsm_action_item *hai;
1714 CT_TRACE("waiting for message from kernel");
1716 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1717 if (rc == -ESHUTDOWN) {
1718 CT_TRACE("shutting down");
1720 } else if (rc == -EAGAIN) {
1721 continue; /* msg not for us */
1722 } else if (rc < 0) {
1723 CT_WARN("cannot receive action list: %s",
1726 if (opt.o_abort_on_error)
1732 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1733 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1735 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1736 CT_ERROR(EINVAL, "'%s' invalid fs name, expecting: %s",
1737 hal->hal_fsname, fs_name);
1739 if (opt.o_abort_on_error)
1745 hai = hai_first(hal);
1746 while (++i <= hal->hal_count) {
1747 if ((char *)hai - (char *)hal > msgsize) {
1750 "'%s' item %d past end of message!",
1755 rc = ct_process_item_async(hai, hal->hal_flags);
1757 CT_ERROR(rc, "'%s' item %d process",
1759 if (opt.o_abort_on_error && err_major)
1761 hai = hai_next(hai);
1764 llapi_hsm_action_list_free(&hal);
1766 if (opt.o_abort_on_error && err_major)
1770 llapi_hsm_copytool_unregister(&ctdata);
1775 static int ct_setup(void)
1779 /* set llapi message level */
1780 llapi_msg_set_level(opt.o_verbose);
1782 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1785 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1789 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1791 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
1799 static int ct_cleanup(void)
1806 if (close(arc_fd) < 0) {
1808 CT_ERROR(rc, "cannot close archive root directory");
1815 int main(int argc, char **argv)
1819 strncpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1820 rc = ct_parseopts(argc, argv);
1822 CT_WARN("try '%s --help' for more information", cmd_name);
1828 switch (opt.o_action) {
1830 rc = ct_import_recurse(opt.o_src);
1836 rc = ct_max_sequence();
1843 if (opt.o_action != CA_MAXSEQ)
1844 CT_TRACE("process finished, errs: %d major, %d minor,"
1845 " rc=%d (%s)", err_major, err_minor, rc,