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 " --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, &opt.o_daemonize, 1},
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:hiMp:qrv",
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;
228 opt.o_action = CA_IMPORT;
231 opt.o_action = CA_MAXSEQ;
234 opt.o_hsm_root = optarg;
240 opt.o_action = CA_REBIND;
248 CT_ERROR("unrecognized option '%s'\n",
254 switch (opt.o_action) {
256 /* src dst mount_point */
257 if (argc != optind + 3) {
258 CT_ERROR("--import requires 2 arguments\n");
261 opt.o_src = argv[optind++];
262 opt.o_dst = argv[optind++];
265 /* FID1 FID2 mount_point or FILE mount_point */
266 if (argc == optind + 2) {
267 opt.o_src = argv[optind++];
269 } else if (argc == optind + 3) {
270 opt.o_src = argv[optind++];
271 opt.o_dst = argv[optind++];
273 CT_ERROR("--rebind requires 1 or 2 arguments\n");
279 /* just mount point */
283 if (argc != optind + 1) {
284 CT_ERROR("no mount point specified\n");
288 opt.o_mnt = argv[optind];
290 CT_TRACE("action=%d src=%s dst=%s mount_point=%s\n",
291 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
293 if (!opt.o_dry_run && opt.o_hsm_root == NULL) {
294 CT_ERROR("must specify a HSM root\n");
298 if (opt.o_action == CA_IMPORT) {
299 if (opt.o_src && opt.o_src[0] == '/') {
300 CT_ERROR("source path must be relative to HSM root.\n");
304 if (opt.o_dst && opt.o_dst[0] != '/') {
305 CT_ERROR("destination path must be absolute.\n");
314 static int ct_mkdir_p(const char *path)
324 while ((ptr = strchr(ptr, '/')) != NULL) {
326 rc = mkdir(saved, DIR_PERM);
328 if (rc < 0 && errno != EEXIST) {
330 CT_ERROR("'%s' mkdir failed (%s)\n", path,
343 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
345 char lov_file[PATH_MAX];
346 char lov_buf[XATTR_SIZE_MAX];
347 struct lov_user_md *lum;
352 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
353 CT_TRACE("saving stripe info of '%s' in %s\n", src, lov_file);
355 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
357 if (xattr_size < 0) {
359 CT_ERROR("'%s' cannot get stripe info on (%s)\n", src,
364 lum = (struct lov_user_md *)lov_buf;
366 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
367 lum->lmm_magic == LOV_USER_MAGIC_V3) {
368 /* Set stripe_offset to -1 so that it is not interpreted as a
369 * hint on restore. */
370 lum->lmm_stripe_offset = -1;
373 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
376 CT_ERROR("'%s' cannot open (%s)\n", lov_file, strerror(-rc));
380 rc = write(fd, lum, xattr_size);
383 CT_ERROR("'%s' cannot write %zd bytes (%s)\n",
384 lov_file, xattr_size, strerror(-rc));
392 CT_ERROR("'%s' cannot close (%s)\n", lov_file, strerror(-rc));
404 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
406 char lov_file[PATH_MAX];
410 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
411 CT_TRACE("reading stripe rules from '%s' for '%s'\n", lov_file, src);
413 fd = open(lov_file, O_RDONLY);
415 CT_ERROR("'%s' cannot open (%s)\n", lov_file, strerror(errno));
419 rc = read(fd, lovea, *lovea_size);
421 CT_ERROR("'%s' cannot read %zu bytes (%s)\n", lov_file,
422 *lovea_size, strerror(errno));
433 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
434 const void *lovea, size_t lovea_size)
438 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
441 CT_ERROR("'%s' cannot set striping (%s)\n",
442 dst, strerror(errno));
449 /* non-blocking read or write */
450 static int nonblock_rw(bool wr, int fd, char *buf, int size)
455 rc = write(fd, buf, size);
457 rc = read(fd, buf, size);
459 if ((rc < 0) && (errno == -EAGAIN)) {
461 struct timeval timeout;
463 timeout.tv_sec = opt.o_report_int;
468 rc = select(FD_SETSIZE, NULL, &set, NULL, &timeout);
470 rc = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
474 /* Timed out, we read nothing */
477 /* Should be available now */
479 rc = write(fd, buf, size);
481 rc = read(fd, buf, size);
490 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
491 const char *dst, int src_fd, int dst_fd,
492 const struct hsm_action_item *hai, long hal_flags)
494 struct hsm_extent he;
501 time_t last_print_time = time(0);
507 CT_TRACE("going to copy data from '%s' to %s\n", src, dst);
509 buf = malloc(opt.o_chunk_size);
513 if (fstat(src_fd, &src_st) < 0) {
514 CT_ERROR("'%s' stat failed (%s)\n", src, strerror(errno));
518 if (!S_ISREG(src_st.st_mode)) {
519 CT_ERROR("'%s' not a regular file\n", src);
523 rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
525 CT_ERROR("'%s' seek to read to "LPU64" (len %zu)"
527 src, hai->hai_extent.offset, src_st.st_size,
533 if (fstat(dst_fd, &dst_st) < 0) {
534 CT_ERROR("'%s' stat failed (%s)\n", dst, strerror(errno));
538 if (!S_ISREG(dst_st.st_mode)) {
539 CT_ERROR("'%s' not a regular file\n", dst);
543 rc = lseek(dst_fd, hai->hai_extent.offset, SEEK_SET);
545 CT_ERROR("'%s' seek to write to "LPU64" failed (%s)\n", src,
546 hai->hai_extent.offset, strerror(errno));
551 he.offset = hai->hai_extent.offset;
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("%s->'%s' progress returned err %d\n", src, dst, rc);
562 /* Don't read beyond a given extent */
563 rlen = (hai->hai_extent.length == -1LL) ?
564 src_st.st_size : hai->hai_extent.length;
566 while (wpos < rlen) {
567 int chunk = (rlen - wpos > opt.o_chunk_size) ?
568 opt.o_chunk_size : rlen - wpos;
570 /* Only read more if we wrote everything in the buffer */
572 rsize = nonblock_rw(0, src_fd, buf, chunk);
577 if (rsize == -EAGAIN) {
581 /* Haven't read anything yet, let's
582 * give it back to the coordinator
583 * for rescheduling */
590 CT_ERROR("'%s' read failed (%s)\n", src,
600 wsize = nonblock_rw(1, dst_fd, buf + bufoff, rpos - wpos);
601 if (wsize == -EAGAIN)
606 CT_ERROR("'%s' write failed (%s)\n", dst,
615 if (opt.o_bandwidth != 0) {
616 static unsigned long long tot_bytes;
617 static time_t start_time, last_time;
618 time_t now = time(0);
619 double tot_time, excess;
620 unsigned int sleep_time;
622 if (now > last_time + 5) {
624 start_time = last_time = now;
628 tot_time = now - start_time;
632 excess = tot_bytes - tot_time * opt.o_bandwidth;
633 sleep_time = excess * 1000000 / opt.o_bandwidth;
634 if ((now - start_time) % 10 == 1)
635 CT_TRACE("bandwith control: excess=%E"
645 if (time(0) >= last_print_time + opt.o_report_int) {
646 last_print_time = time(0);
647 CT_TRACE("%%"LPU64" ", 100 * wpos / rlen);
649 rc = llapi_hsm_action_progress(hcp, &he, 0);
651 /* Action has been canceled or something wrong
652 * is happening. Stop copying data. */
653 CT_ERROR("%s->'%s' progress returned err %d\n",
664 * truncate restored file
665 * size is taken from the archive this is done to support
666 * restore after a force release which leaves the file with the
667 * wrong size (can big bigger than the new size)
669 if ((hai->hai_action == HSMA_RESTORE) &&
670 (src_st.st_size < dst_st.st_size)) {
672 * make sure the file is on disk before reporting success.
674 rc = ftruncate(dst_fd, src_st.st_size);
677 CT_ERROR("'%s' final truncate to %lu failed (%s)\n",
678 dst, src_st.st_size, strerror(-rc));
687 CT_ERROR("'%s' fsync failed (%s)\n", dst,
698 /* Copy file attributes from file src to file dest */
699 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
703 struct timeval times[2];
705 if (fstat(src_fd, &st) < 0) {
706 CT_ERROR("'%s' stat failed (%s)\n",
707 src, strerror(errno));
711 times[0].tv_sec = st.st_atime;
712 times[0].tv_usec = 0;
713 times[1].tv_sec = st.st_mtime;
714 times[1].tv_usec = 0;
715 if (fchmod(dst_fd, st.st_mode) < 0 ||
716 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
717 futimes(dst_fd, times) < 0) {
718 CT_ERROR("'%s' fchmod fchown or futimes failed (%s)\n", src,
726 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
727 int dst_fd, bool is_restore)
729 char list[XATTR_LIST_MAX];
730 char value[XATTR_SIZE_MAX];
735 list_len = flistxattr(src_fd, list, sizeof(list));
740 while (name < list + list_len) {
741 rc = fgetxattr(src_fd, name, value, sizeof(value));
745 /* when we restore, we do not restore lustre xattr */
747 (strncmp(XATTR_TRUSTED_PREFIX, name,
748 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
749 rc = fsetxattr(dst_fd, name, value, rc, 0);
750 CT_TRACE("'%s' fsetxattr of '%s' rc=%d (%s)\n",
751 dst, name, rc, strerror(errno));
752 /* lustre.* attrs aren't supported on other FS's */
753 if (rc < 0 && errno != EOPNOTSUPP) {
754 CT_ERROR("'%s' fsetxattr of '%s' failed (%s)\n",
755 dst, name, strerror(errno));
759 name += strlen(name) + 1;
765 static int ct_path_lustre(char *buf, int sz, const char *mnt,
766 const lustre_fid *fid)
768 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
769 dot_lustre_name, PFID(fid));
772 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
773 const lustre_fid *fid)
775 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
776 DFID_NOBRACE, archive_dir,
777 (fid)->f_oid & 0xFFFF,
778 (fid)->f_oid >> 16 & 0xFFFF,
779 (unsigned int)((fid)->f_seq & 0xFFFF),
780 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
781 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
782 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
786 static bool ct_is_retryable(int err)
788 return err == -ETIMEDOUT;
791 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
792 const struct hsm_action_item *hai,
793 int mdt_index, int open_flags)
798 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
801 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
802 CT_ERROR("'%s' copy start failed (%s)\n", src, strerror(-rc));
808 static int ct_begin(struct hsm_copyaction_private **phcp,
809 const struct hsm_action_item *hai)
811 /* Restore takes specific parameters. Call the same function w/ default
812 * values for all other operations. */
813 return ct_begin_restore(phcp, hai, -1, 0);
816 static int ct_fini(struct hsm_copyaction_private **phcp,
817 const struct hsm_action_item *hai, int flags, int ct_rc)
822 CT_TRACE("Action completed, notifying coordinator "
823 "cookie="LPX64", FID="DFID", flags=%d err=%d\n",
824 hai->hai_cookie, PFID(&hai->hai_fid),
827 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
828 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, flags, abs(ct_rc));
829 if (rc == -ECANCELED)
830 CT_ERROR("'%s' completed action has been canceled: "
831 "cookie="LPX64", FID="DFID"\n", lstr, hai->hai_cookie,
832 PFID(&hai->hai_fid));
834 CT_ERROR("'%s' copy end failed (%s)\n", lstr, strerror(-rc));
836 CT_TRACE("'%s' copy end ok (rc=%d)\n", lstr, rc);
841 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
843 struct hsm_copyaction_private *hcp = NULL;
848 bool rename_needed = false;
854 rc = ct_begin(&hcp, hai);
858 /* we fill archive so:
860 * destination = lustre FID
862 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
863 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
864 if (hai->hai_extent.length == -1) {
865 /* whole file, write it to tmp location and atomically
866 * replace old archived file */
867 strncat(dst, "_tmp", sizeof(dst) - strlen(dst) - 1);
868 /* we cannot rely on the same test because ct_copy_data()
869 * updates hai_extent.length */
870 rename_needed = true;
873 CT_TRACE("'%s' archived to %s\n", src, dst);
880 rc = ct_mkdir_p(dst);
882 CT_ERROR("'%s' mkdir_p failed (%s)\n", dst, strerror(-rc));
886 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
888 CT_ERROR("'%s' open read failed (%s)\n", src, strerror(errno));
893 open_flags = O_WRONLY | O_NOFOLLOW | O_NONBLOCK;
894 /* If extent is specified, don't truncate an old archived copy */
895 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
897 dst_fd = open(dst, open_flags, FILE_PERM);
899 CT_ERROR("'%s' open write failed (%s)\n", dst, strerror(errno));
904 /* saving stripe is not critical */
905 rc = ct_save_stripe(src_fd, src, dst);
907 CT_ERROR("'%s' cannot save file striping info in '%s' (%s)\n",
908 src, dst, strerror(-rc));
910 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
912 CT_ERROR("'%s' data copy failed to '%s' (%s)\n",
913 src, dst, strerror(-rc));
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 ct_flags |= HP_FLAG_RETRY;
1068 rc = ct_fini(&hcp, hai, ct_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 */
1089 /* we fill lustre so:
1090 * source = lustre FID in the backend
1091 * destination = data FID = volatile file
1094 /* build backend file name from released file FID */
1095 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1097 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1098 * dependency on the structure format. */
1099 rc = ct_load_stripe(src, lov_buf, &lov_size);
1101 CT_WARN("'%s' cannot get stripe rules (%s), use default\n",
1102 src, strerror(-rc));
1105 open_flags |= O_LOV_DELAY_CREATE;
1109 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1113 /* get the FID of the volatile file */
1114 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1116 CT_ERROR("restoring "DFID", cannot get FID of created volatile"
1117 " file (%s)\n", PFID(&hai->hai_fid), strerror(-rc));
1121 /* build volatile "file name", for messages */
1122 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1124 CT_TRACE("'%s' restore data to '%s'\n", src, dst);
1126 if (opt.o_dry_run) {
1131 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
1133 CT_ERROR("'%s' open for read failed (%s)\n", src,
1139 dst_fd = llapi_hsm_action_get_fd(hcp);
1142 /* the layout cannot be allocated through .fid so we have to
1143 * restore a layout */
1144 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1146 CT_ERROR("'%s' cannot restore file striping info from "
1147 "'%s' (%s)\n", dst, src, strerror(-rc));
1153 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1155 CT_ERROR("'%s' data copy to '%s' failed (%s)\n", src, dst,
1158 if (ct_is_retryable(rc))
1159 flags |= HP_FLAG_RETRY;
1163 CT_TRACE("'%s' data restore done to %s\n", src, dst);
1167 rc = ct_fini(&hcp, hai, flags, rc);
1169 /* object swaping is done by cdt at copy end, so close of volatile file
1170 * cannot be done before */
1180 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1182 struct hsm_copyaction_private *hcp = NULL;
1186 rc = ct_begin(&hcp, hai);
1190 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1192 CT_TRACE("'%s' removed file\n", dst);
1194 if (opt.o_dry_run) {
1202 CT_ERROR("'%s' unlink failed (%s)\n", dst, strerror(-rc));
1207 strncat(dst, ".lov", sizeof(dst) - strlen(dst) - 1);
1211 CT_ERROR("'%s' unlink failed (%s)\n", dst, strerror(-rc));
1218 rc = ct_fini(&hcp, hai, 0, rc);
1223 static int ct_report_error(const struct hsm_action_item *hai, int flags,
1226 struct hsm_copyaction_private *hcp;
1229 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
1233 rc = llapi_hsm_action_end(&hcp, &hai->hai_extent, flags, abs(errval));
1238 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1242 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1243 /* Print the original path */
1245 char path[PATH_MAX];
1246 long long recno = -1;
1249 sprintf(fid, DFID, PFID(&hai->hai_fid));
1250 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64"\n",
1251 fid, hsm_copytool_action2name(hai->hai_action),
1252 hai->hai_len, hai->hai_cookie);
1253 rc = llapi_fid2path(opt.o_mnt, fid, path,
1254 sizeof(path), &recno, &linkno);
1256 CT_ERROR("'%s' fid2path failed (%s)\n", fid,
1259 CT_TRACE("'%s' processing file\n", path);
1262 switch (hai->hai_action) {
1263 /* set err_major, minor inside these functions */
1265 rc = ct_archive(hai, hal_flags);
1268 rc = ct_restore(hai, hal_flags);
1271 rc = ct_remove(hai, hal_flags);
1274 CT_TRACE("'%s' cancel not implemented\n", opt.o_mnt);
1275 /* Don't report progress to coordinator for this cookie:
1276 * the copy function will get ECANCELED when reporting
1282 CT_ERROR("'%s' unknown action %d\n", opt.o_mnt,
1285 ct_report_error(hai, 0, -EINVAL);
1293 struct hsm_action_item *hai;
1296 static void *ct_thread(void *data)
1298 struct ct_th_data *cttd = data;
1301 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1305 pthread_exit((void *)(intptr_t)rc);
1308 static int ct_process_item_async(const struct hsm_action_item *hai,
1311 pthread_attr_t attr;
1313 struct ct_th_data *data;
1316 data = malloc(sizeof(*data));
1320 data->hai = malloc(hai->hai_len);
1321 if (data->hai == NULL) {
1326 memcpy(data->hai, hai, hai->hai_len);
1327 data->hal_flags = hal_flags;
1329 rc = pthread_attr_init(&attr);
1331 CT_ERROR("'%s' pthread_attr_init: %s\n", opt.o_mnt,
1338 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1340 rc = pthread_create(&thread, &attr, ct_thread, data);
1342 CT_ERROR("'%s' thread create: (%s)\n", opt.o_mnt, strerror(rc));
1344 pthread_attr_destroy(&attr);
1348 static int ct_import_one(const char *src, const char *dst)
1350 char newarc[PATH_MAX];
1355 CT_TRACE("'%s' importing from %s\n", dst, src);
1357 if (stat(src, &st) < 0) {
1358 CT_ERROR("'%s' stat failed (%s)\n", src, strerror(errno));
1365 rc = llapi_hsm_import(dst,
1366 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1367 &st, 0, 0, 0, 0, NULL, &fid);
1369 CT_ERROR("'%s' import from '%s' failed (%s)\n", dst, src,
1374 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1376 rc = ct_mkdir_p(newarc);
1378 CT_ERROR("'%s' mkdir_p failed (%s)\n", newarc, strerror(-rc));
1384 /* Lots of choices now: mv, ln, ln -s ? */
1385 rc = link(src, newarc); /* hardlink */
1387 CT_ERROR("'%s' link to '%s' failed (%s)\n", newarc, src,
1392 CT_TRACE("'%s' imported from '%s'=='%s'\n", dst, newarc, src);
1397 static char *path_concat(const char *dirname, const char *basename)
1400 int dirlen = strlen(dirname);
1402 result = malloc(dirlen + strlen(basename) + 2);
1406 memcpy(result, dirname, dirlen);
1407 result[dirlen] = '/';
1408 strcpy(result + dirlen + 1, basename);
1413 static int ct_import_recurse(const char *relpath)
1416 struct dirent ent, *cookie = NULL;
1417 char *srcpath, *newpath;
1420 if (relpath == NULL)
1423 srcpath = path_concat(opt.o_hsm_root, relpath);
1424 if (srcpath == NULL) {
1429 dir = opendir(srcpath);
1431 /* Not a dir, or error */
1432 if (errno == ENOTDIR) {
1433 /* Single regular file case, treat o_dst as absolute
1435 rc = ct_import_one(srcpath, opt.o_dst);
1437 CT_ERROR("'%s' opendir failed (%s)\n", srcpath,
1448 rc = readdir_r(dir, &ent, &cookie);
1450 CT_ERROR("'%s' readdir_r failed (%s)\n", relpath,
1455 } else if ((rc == 0) && (cookie == NULL)) {
1456 /* end of directory */
1460 if (!strcmp(ent.d_name, ".") ||
1461 !strcmp(ent.d_name, ".."))
1464 /* New relative path */
1465 newpath = path_concat(relpath, ent.d_name);
1466 if (newpath == NULL) {
1472 if (ent.d_type == DT_DIR) {
1473 rc = ct_import_recurse(newpath);
1478 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1479 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1480 /* Make the target dir in the Lustre fs */
1481 rc = ct_mkdir_p(dst);
1483 /* Import the file */
1484 rc = ct_import_one(src, dst);
1486 CT_ERROR("'%s' ct_mkdir_p failed (%s)\n", dst,
1493 CT_ERROR("'%s' importing failed\n", newpath);
1494 if (err_major && opt.o_abort_on_error) {
1508 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1513 CT_TRACE("rebind "DFID" to "DFID"\n", PFID(old_fid), PFID(new_fid));
1515 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1516 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1518 if (!opt.o_dry_run) {
1520 if (rename(src, dst)) {
1521 CT_ERROR("'%s' rename to '%s' failed (%s)\n", src, dst,
1525 /* rename lov file */
1526 strncat(src, ".lov", sizeof(src) - strlen(src) - 1);
1527 strncat(dst, ".lov", sizeof(dst) - strlen(dst) - 1);
1528 if (rename(src, dst))
1529 CT_ERROR("'%s' rename to '%s' failed (%s)\n", src, dst,
1536 static bool fid_is_file(lustre_fid *fid)
1538 return fid_is_norm(fid) || fid_is_igif(fid);
1541 static bool should_ignore_line(const char *line)
1545 for (i = 0; line[i] != '\0'; i++) {
1546 if (isspace(line[i]))
1548 else if (line[i] == '#')
1557 static int ct_rebind_list(const char *list)
1563 size_t line_size = 0;
1564 unsigned int nl = 0;
1565 unsigned int ok = 0;
1567 filp = fopen(list, "r");
1569 CT_ERROR("'%s' open failed (%s)\n", list, strerror(errno));
1573 /* each line consists of 2 FID */
1574 while ((r = getline(&line, &line_size, filp)) != -1) {
1578 /* Ignore empty and commented out ('#...') lines. */
1579 if (should_ignore_line(line))
1584 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1585 if (rc != 6 || !fid_is_file(&old_fid) ||
1586 !fid_is_file(&new_fid)) {
1587 CT_ERROR("'%s' FID expected near '%s', line %u\n",
1593 if (ct_rebind_one(&old_fid, &new_fid))
1604 /* return 0 if all rebinds were sucessful */
1605 CT_TRACE("'%s' %u lines read, %u rebind successful\n", list, nl, ok);
1607 return ok == nl ? 0 : -1;
1610 static int ct_rebind(void)
1618 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1619 !fid_is_file(&old_fid)) {
1620 CT_ERROR("'%s' invalid FID format\n", opt.o_src);
1624 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1625 !fid_is_file(&new_fid)) {
1626 CT_ERROR("'%s' invalid FID format\n", opt.o_dst);
1630 rc = ct_rebind_one(&old_fid, &new_fid);
1635 /* o_src is a list file */
1636 rc = ct_rebind_list(opt.o_src);
1641 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1646 struct dirent ent, *cookie = NULL;
1650 dir = opendir(dirpath);
1653 CT_ERROR("'%s' failed to open directory (%s)\n", opt.o_hsm_root,
1658 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1660 /* end of directory.
1661 * rc is 0 and seqmax contains the max value. */
1664 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1667 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1668 CT_TRACE("'%s' unexpected dirname format, "
1669 "skip entry.\n", ent.d_name);
1672 if (sub_seq > *sub_seqmax)
1673 *sub_seqmax = sub_seq;
1676 CT_ERROR("'%s' readdir_r failed (%s)\n", dirpath, strerror(-rc));
1683 static int ct_max_sequence(void)
1686 char path[PATH_MAX];
1690 strncpy(path, opt.o_hsm_root, sizeof(path));
1691 /* FID sequence is stored in top-level directory names:
1692 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1694 for (i = 0; i < 4; i++) {
1695 rc = ct_dir_level_max(path, &subseq);
1698 seq |= ((__u64)subseq << ((3 - i) * 16));
1699 sprintf(path + strlen(path), "/%04x", subseq);
1702 printf("max_sequence: %016Lx\n", seq);
1707 static void handler(int signal)
1709 psignal(signal, "exiting");
1710 /* If we don't clean up upon interrupt, umount thinks there's a ref
1711 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1712 * does successfully unmount and the mount is actually gone, but the
1713 * mtab entry remains. So this just makes mtab happier. */
1714 llapi_hsm_copytool_unregister(&ctdata);
1718 /* Daemon waits for messages from the kernel; run it in the background. */
1719 static int ct_run(void)
1723 if (opt.o_daemonize) {
1726 CT_ERROR("%d: cannot start as daemon (%s)", getpid(),
1732 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt, 0,
1733 opt.o_archive_cnt, opt.o_archive_id);
1735 CT_ERROR("%d: cannot start copytool interface: %s\n", getpid(),
1740 signal(SIGINT, handler);
1741 signal(SIGTERM, handler);
1744 struct hsm_action_list *hal;
1745 struct hsm_action_item *hai;
1749 CT_TRACE("%d: waiting for message from kernel\n", getpid());
1751 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1752 if (rc == -ESHUTDOWN) {
1753 CT_TRACE("%d: shutting down", getpid());
1755 } else if (rc == -EAGAIN) {
1756 continue; /* msg not for us */
1757 } else if (rc < 0) {
1758 CT_WARN("%d: message receive: (%s)\n", getpid(),
1761 if (opt.o_abort_on_error)
1767 CT_TRACE("%d: copytool fs=%s archive#=%d item_count=%d\n",
1768 getpid(), hal->hal_fsname, hal->hal_archive_id,
1771 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1772 CT_ERROR("'%s' invalid fs name, expecting: %s\n",
1773 hal->hal_fsname, fs_name);
1775 if (opt.o_abort_on_error)
1781 hai = hai_first(hal);
1782 while (++i <= hal->hal_count) {
1783 if ((char *)hai - (char *)hal > msgsize) {
1784 CT_ERROR("'%s' item %d past end of message!\n",
1790 rc = ct_process_item_async(hai, hal->hal_flags);
1792 CT_ERROR("'%s' item %d process err: %s\n",
1793 opt.o_mnt, i, strerror(-rc));
1794 if (opt.o_abort_on_error && err_major)
1796 hai = hai_next(hai);
1799 llapi_hsm_action_list_free(&hal);
1801 if (opt.o_abort_on_error && err_major)
1805 llapi_hsm_copytool_unregister(&ctdata);
1810 static int ct_setup(void)
1814 /* set llapi message level */
1815 llapi_msg_set_level(opt.o_verbose);
1817 arc_fd = open(opt.o_hsm_root, O_DIRECTORY);
1819 CT_ERROR("cannot open archive at '%s': %s\n", opt.o_hsm_root,
1824 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1826 CT_ERROR("cannot find a Lustre filesystem mounted at: %s\n",
1834 static int ct_cleanup(void)
1839 if (close(arc_fd) < 0) {
1840 CT_ERROR("cannot close archive: %s.\n", strerror(errno));
1847 int main(int argc, char **argv)
1851 strncpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1852 rc = ct_parseopts(argc, argv);
1854 CT_ERROR("try '%s --help' for more information.\n", cmd_name);
1860 switch (opt.o_action) {
1862 rc = ct_import_recurse(opt.o_src);
1868 rc = ct_max_sequence();
1875 if (opt.o_action != CA_MAXSEQ)
1876 CT_TRACE("%s(%d) finished, errs: %d major, %d minor, "
1877 "rc=%d (%s)\n", argv[0], getpid(), err_major,
1878 err_minor, rc, strerror(-rc));