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.
45 #include <sys/xattr.h>
46 #include <sys/syscall.h>
47 #include <sys/types.h>
48 #include <lustre/lustre_idl.h>
49 #include <lustre/lustreapi.h>
51 /* Progress reporting period */
52 #define REPORT_INTERVAL_DEFAULT 30
53 /* HSM hash subdir permissions */
54 #define DIR_PERM S_IRWXU
55 /* HSM hash file permissions */
56 #define FILE_PERM (S_IRUSR | S_IWUSR)
58 #define ONE_MB 0x100000
60 /* copytool uses a 32b bitmask field to register with kuc
61 * archive num = 0 => all
62 * archive num from 1 to 32
64 #define MAX_ARCHIVE_CNT (sizeof(__u32) * 8)
81 int o_archive_id[MAX_ARCHIVE_CNT];
83 unsigned long long o_bandwidth;
85 enum ct_action o_action;
90 char *o_src; /* for import, or rebind */
91 char *o_dst; /* for import, or rebind */
94 /* everything else is zeroed */
95 struct options opt = {
98 .o_verbose = LLAPI_MSG_INFO,
100 .o_report_int = REPORT_INTERVAL_DEFAULT,
101 .o_chunk_size = ONE_MB,
104 /* hsm_copytool_private will hold an open FD on the lustre mount point
105 * for us. Additionally open one on the archive FS root to make sure
106 * it doesn't drop out from under us (and remind the admin to shutdown
107 * the copytool before unmounting). */
109 static int arc_fd = -1;
111 static int err_major;
112 static int err_minor;
114 static char cmd_name[PATH_MAX];
115 static char fs_name[MAX_OBD_NAME + 1];
117 static struct hsm_copytool_private *ctdata;
120 #define CT_ERROR(_rc, _format, ...) \
121 llapi_error(LLAPI_MSG_ERROR, _rc, \
122 "%s[%ld]: "_format, \
123 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
124 #define CT_DEBUG(_format, ...) \
125 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
126 "%s[%ld]: "_format, \
127 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
128 #define CT_WARN(_format, ...) \
129 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
130 "%s[%ld]: "_format, \
131 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
132 #define CT_TRACE(_format, ...) \
133 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
134 "%s[%ld]: "_format, \
135 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
136 #define CT_PRINTF(_format, ...) \
137 llapi_printf(LLAPI_MSG_NORMAL, \
138 "%s[%ld]: "_format, \
139 cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
141 static void usage(const char *name, int rc)
144 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
145 "The Lustre HSM Posix copy tool can be used as a daemon or "
146 "as a command line tool\n"
147 "The Lustre HSM daemon acts on action requests from Lustre\n"
148 "to copy files to and from an HSM archive system.\n"
149 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
150 "to an HSM mounted at a given hsm_root.\n"
151 " --daemon Daemon mode, run in background\n"
153 " --no-attr Don't copy file attributes\n"
154 " --no-shadow Don't create shadow namespace in archive\n"
155 " --no-xattr Don't copy file extended attributes\n"
156 "The Lustre HSM tool performs administrator-type actions\n"
157 "on a Lustre HSM archive.\n"
158 "This POSIX-flavored tool can link an existing HSM namespace\n"
159 "into a Lustre filesystem.\n"
161 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
162 " import an archived subtree from\n"
163 " <src> (FID or relative path to hsm_root) into the Lustre\n"
165 " <dst> (absolute path)\n"
166 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
167 " rebind an entry in the HSM to a new FID\n"
168 " <old_FID> old FID the HSM entry is bound to\n"
169 " <new_FID> new FID to bind the HSM entry to\n"
170 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
171 " perform the rebind operation for all FID in the list file\n"
172 " each line of <list_file> consists of <old_FID> <new_FID>\n"
173 " %s [options] --max-sequence <fsname>\n"
174 " return the max fid sequence of archived files\n"
175 " --abort-on-error Abort operation on major error\n"
176 " -A, --archive <#> Archive number (repeatable)\n"
177 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
179 " --dry-run Don't run, just show what would be done\n"
180 " -c, --chunk-size <sz> I/O size used during data copy\n"
181 " (unit can be used, default is MB)\n"
182 " -f, --event-fifo <path> Write events stream to fifo\n"
183 " -p, --hsm-root <path> Target HSM mount point\n"
184 " -q, --quiet Produce less verbose output\n"
185 " -u, --update-interval <s> Interval between progress reports sent\n"
187 " -v, --verbose Produce more verbose output\n",
188 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
193 static int ct_parseopts(int argc, char * const *argv)
195 struct option long_opts[] = {
196 {"abort-on-error", no_argument, &opt.o_abort_on_error, 1},
197 {"abort_on_error", no_argument, &opt.o_abort_on_error, 1},
198 {"archive", required_argument, NULL, 'A'},
199 {"bandwidth", required_argument, NULL, 'b'},
200 {"chunk-size", required_argument, NULL, 'c'},
201 {"chunk_size", required_argument, NULL, 'c'},
202 {"daemon", no_argument, &opt.o_daemonize, 1},
203 {"event-fifo", required_argument, NULL, 'f'},
204 {"event_fifo", required_argument, NULL, 'f'},
205 {"dry-run", no_argument, &opt.o_dry_run, 1},
206 {"help", no_argument, NULL, 'h'},
207 {"hsm-root", required_argument, NULL, 'p'},
208 {"hsm_root", required_argument, NULL, 'p'},
209 {"import", no_argument, NULL, 'i'},
210 {"max-sequence", no_argument, NULL, 'M'},
211 {"max_sequence", no_argument, NULL, 'M'},
212 {"no-attr", no_argument, &opt.o_copy_attrs, 0},
213 {"no_attr", no_argument, &opt.o_copy_attrs, 0},
214 {"no-shadow", no_argument, &opt.o_shadow_tree, 0},
215 {"no_shadow", no_argument, &opt.o_shadow_tree, 0},
216 {"no-xattr", no_argument, &opt.o_copy_xattrs, 0},
217 {"no_xattr", no_argument, &opt.o_copy_xattrs, 0},
218 {"quiet", no_argument, NULL, 'q'},
219 {"rebind", no_argument, NULL, 'r'},
220 {"update-interval", required_argument, NULL, 'u'},
221 {"update_interval", required_argument, NULL, 'u'},
222 {"verbose", no_argument, NULL, 'v'},
226 unsigned long long value;
227 unsigned long long unit;
230 while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
231 long_opts, NULL)) != -1) {
234 if ((opt.o_archive_cnt >= MAX_ARCHIVE_CNT) ||
235 (atoi(optarg) >= MAX_ARCHIVE_CNT)) {
237 CT_ERROR(rc, "archive number must be less"
238 "than %zu", MAX_ARCHIVE_CNT);
241 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
244 case 'b': /* -b and -c have both a number with unit as arg */
247 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
249 CT_ERROR(rc, "bad value for -%c '%s'", c,
254 opt.o_chunk_size = value;
256 opt.o_bandwidth = value;
259 opt.o_event_fifo = optarg;
264 opt.o_action = CA_IMPORT;
267 opt.o_action = CA_MAXSEQ;
270 opt.o_hsm_root = optarg;
276 opt.o_action = CA_REBIND;
279 opt.o_report_int = atoi(optarg);
280 if (opt.o_report_int < 0) {
282 CT_ERROR(rc, "bad value for -%c '%s'", c,
297 switch (opt.o_action) {
299 /* src dst mount_point */
300 if (argc != optind + 3) {
302 CT_ERROR(rc, "--import requires 2 arguments");
305 opt.o_src = argv[optind++];
306 opt.o_dst = argv[optind++];
309 /* FID1 FID2 mount_point or FILE mount_point */
310 if (argc == optind + 2) {
311 opt.o_src = argv[optind++];
313 } else if (argc == optind + 3) {
314 opt.o_src = argv[optind++];
315 opt.o_dst = argv[optind++];
318 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
324 /* just mount point */
328 if (argc != optind + 1) {
330 CT_ERROR(rc, "no mount point specified");
334 opt.o_mnt = argv[optind];
337 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
338 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
340 if (!opt.o_dry_run && opt.o_hsm_root == NULL) {
342 CT_ERROR(rc, "must specify a root directory for the backend");
346 if (opt.o_action == CA_IMPORT) {
347 if (opt.o_src && opt.o_src[0] == '/') {
350 "source path must be relative to HSM root");
354 if (opt.o_dst && opt.o_dst[0] != '/') {
356 CT_ERROR(rc, "destination path must be absolute");
365 static int ct_mkdir_p(const char *path)
378 while ((ptr = strchr(ptr, '/')) != NULL) {
380 rc = mkdir(saved, DIR_PERM);
382 if (rc < 0 && errno != EEXIST) {
384 CT_ERROR(rc, "cannot mkdir '%s'", path);
396 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
398 char lov_file[PATH_MAX];
399 char lov_buf[XATTR_SIZE_MAX];
400 struct lov_user_md *lum;
405 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
406 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
408 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
410 if (xattr_size < 0) {
412 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
416 lum = (struct lov_user_md *)lov_buf;
418 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
419 lum->lmm_magic == LOV_USER_MAGIC_V3) {
420 /* Set stripe_offset to -1 so that it is not interpreted as a
421 * hint on restore. */
422 lum->lmm_stripe_offset = -1;
425 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
428 CT_ERROR(rc, "cannot open '%s'", lov_file);
432 rc = write(fd, lum, xattr_size);
435 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
436 xattr_size, lov_file);
444 CT_ERROR(rc, "cannot close '%s'", lov_file);
456 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
458 char lov_file[PATH_MAX];
462 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
463 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
465 fd = open(lov_file, O_RDONLY);
467 CT_ERROR(errno, "cannot open '%s'", lov_file);
471 rc = read(fd, lovea, *lovea_size);
473 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
474 *lovea_size, lov_file);
485 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
486 const void *lovea, size_t lovea_size)
490 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
494 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
500 static void bandwidth_ctl_delay(int wsize)
502 static unsigned long long tot_bytes;
503 static time_t start_time;
504 static time_t last_time;
505 time_t now = time(0);
508 unsigned int sleep_time;
510 if (now > last_time + 5) {
512 start_time = last_time = now;
516 tot_time = now - start_time;
520 excess = tot_bytes - tot_time * opt.o_bandwidth;
521 sleep_time = excess * 1000000 / opt.o_bandwidth;
522 if ((now - start_time) % 10 == 1)
523 CT_TRACE("bandwith control: excess=%E sleep for %dus", excess,
532 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
533 const char *dst, int src_fd, int dst_fd,
534 const struct hsm_action_item *hai, long hal_flags)
536 struct hsm_extent he;
537 __u64 offset = hai->hai_extent.offset;
541 __u64 write_total = 0;
543 time_t last_print_time = time(NULL);
546 CT_TRACE("going to copy data from '%s' to '%s'", src, dst);
548 if (fstat(src_fd, &src_st) < 0) {
550 CT_ERROR(rc, "cannot stat '%s'", src);
554 if (!S_ISREG(src_st.st_mode)) {
556 CT_ERROR(rc, "'%s' is not a regular file", src);
560 if (fstat(dst_fd, &dst_st) < 0) {
562 CT_ERROR(rc, "cannot stat '%s'", dst);
566 if (!S_ISREG(dst_st.st_mode)) {
568 CT_ERROR(rc, "'%s' is not a regular file", dst);
572 rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
576 "cannot seek for read to "LPU64" (len %jd) in '%s'",
577 hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
581 /* Don't read beyond a given extent */
582 length = min(hai->hai_extent.length, src_st.st_size);
586 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
588 /* Action has been canceled or something wrong
589 * is happening. Stop copying data. */
590 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
597 buf = malloc(opt.o_chunk_size);
603 CT_DEBUG("Going to copy "LPU64" bytes %s -> %s\n", length, src, dst);
605 while (write_total < length) {
608 int chunk = (length - write_total > opt.o_chunk_size) ?
609 opt.o_chunk_size : length - write_total;
611 rsize = pread(src_fd, buf, chunk, offset);
618 CT_ERROR(rc, "cannot read from '%s'", src);
622 wsize = pwrite(dst_fd, buf, rsize, offset);
625 CT_ERROR(rc, "cannot write to '%s'", dst);
629 write_total += wsize;
632 if (opt.o_bandwidth != 0)
633 /* sleep if needed, to honor bandwidth limits */
634 bandwidth_ctl_delay(wsize);
636 if (time(0) >= last_print_time + opt.o_report_int) {
637 last_print_time = time(0);
638 CT_TRACE("%%"LPU64" ", 100 * write_total / length);
639 he.length = write_total;
640 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
642 /* Action has been canceled or something wrong
643 * is happening. Stop copying data. */
644 CT_ERROR(rc, "progress ioctl for copy"
645 " '%s'->'%s' failed", src, dst);
654 * truncate restored file
655 * size is taken from the archive this is done to support
656 * restore after a force release which leaves the file with the
657 * wrong size (can big bigger than the new size)
659 if ((hai->hai_action == HSMA_RESTORE) &&
660 (src_st.st_size < dst_st.st_size)) {
662 * make sure the file is on disk before reporting success.
664 rc = ftruncate(dst_fd, src_st.st_size);
667 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
668 dst, (intmax_t)src_st.st_size);
679 /* Copy file attributes from file src to file dest */
680 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
684 struct timeval times[2];
687 if (fstat(src_fd, &st) < 0) {
689 CT_ERROR(rc, "cannot stat '%s'", src);
693 times[0].tv_sec = st.st_atime;
694 times[0].tv_usec = 0;
695 times[1].tv_sec = st.st_mtime;
696 times[1].tv_usec = 0;
697 if (fchmod(dst_fd, st.st_mode) < 0 ||
698 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
699 futimes(dst_fd, times) < 0) {
701 CT_ERROR(rc, "cannot set attributes of '%s'", src);
708 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
709 int dst_fd, bool is_restore)
711 char list[XATTR_LIST_MAX];
712 char value[XATTR_SIZE_MAX];
717 list_len = flistxattr(src_fd, list, sizeof(list));
722 while (name < list + list_len) {
723 rc = fgetxattr(src_fd, name, value, sizeof(value));
727 /* when we restore, we do not restore lustre xattr */
729 (strncmp(XATTR_TRUSTED_PREFIX, name,
730 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
731 rc = fsetxattr(dst_fd, name, value, rc, 0);
732 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
733 name, dst, rc, strerror(errno));
734 /* lustre.* attrs aren't supported on other FS's */
735 if (rc < 0 && errno != EOPNOTSUPP) {
737 CT_ERROR(rc, "cannot set extended attribute"
743 name += strlen(name) + 1;
749 static int ct_path_lustre(char *buf, int sz, const char *mnt,
750 const lustre_fid *fid)
752 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
753 dot_lustre_name, PFID(fid));
756 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
757 const lustre_fid *fid)
759 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
760 DFID_NOBRACE, archive_dir,
761 (fid)->f_oid & 0xFFFF,
762 (fid)->f_oid >> 16 & 0xFFFF,
763 (unsigned int)((fid)->f_seq & 0xFFFF),
764 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
765 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
766 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
770 static bool ct_is_retryable(int err)
772 return err == -ETIMEDOUT;
775 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
776 const struct hsm_action_item *hai,
777 int mdt_index, int open_flags)
782 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
785 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
786 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
792 static int ct_begin(struct hsm_copyaction_private **phcp,
793 const struct hsm_action_item *hai)
795 /* Restore takes specific parameters. Call the same function w/ default
796 * values for all other operations. */
797 return ct_begin_restore(phcp, hai, -1, 0);
800 static int ct_fini(struct hsm_copyaction_private **phcp,
801 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
803 struct hsm_copyaction_private *hcp;
807 CT_TRACE("Action completed, notifying coordinator "
808 "cookie="LPX64", FID="DFID", hp_flags=%d err=%d",
809 hai->hai_cookie, PFID(&hai->hai_fid),
812 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
814 if (phcp == NULL || *phcp == NULL) {
815 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
817 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
824 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
825 if (rc == -ECANCELED)
826 CT_ERROR(rc, "completed action on '%s' has been canceled: "
827 "cookie="LPX64", FID="DFID, lstr, hai->hai_cookie,
828 PFID(&hai->hai_fid));
830 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
832 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
838 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
840 struct hsm_copyaction_private *hcp = NULL;
842 char dst[PATH_MAX] = "";
845 bool rename_needed = false;
851 rc = ct_begin(&hcp, hai);
855 /* we fill archive so:
857 * destination = lustre FID
859 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
860 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
861 if (hai->hai_extent.length == -1) {
862 /* whole file, write it to tmp location and atomically
863 * replace old archived file */
864 strlcat(dst, "_tmp", sizeof(dst));
865 /* we cannot rely on the same test because ct_copy_data()
866 * updates hai_extent.length */
867 rename_needed = true;
870 CT_TRACE("archiving '%s' to '%s'", src, dst);
877 rc = ct_mkdir_p(dst);
879 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
883 src_fd = llapi_hsm_action_get_fd(hcp);
886 CT_ERROR(rc, "cannot open '%s' for read", src);
890 open_flags = O_WRONLY | O_NOFOLLOW | O_NONBLOCK;
891 /* If extent is specified, don't truncate an old archived copy */
892 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
894 dst_fd = open(dst, open_flags, FILE_PERM);
897 CT_ERROR(rc, "cannot open '%s' for write", dst);
901 /* saving stripe is not critical */
902 rc = ct_save_stripe(src_fd, src, dst);
904 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
907 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
909 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
916 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
920 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
922 /* attrs will remain on the MDS; no need to copy them, except possibly
923 for disaster recovery */
924 if (opt.o_copy_attrs) {
925 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
927 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
931 CT_TRACE("attr file for '%s' saved to archive '%s'",
935 /* xattrs will remain on the MDS; no need to copy them, except possibly
936 for disaster recovery */
937 if (opt.o_copy_xattrs) {
938 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
940 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
942 rcf = rcf ? rcf : rc;
944 CT_TRACE("xattr file for '%s' saved to archive '%s'",
948 if (rename_needed == true) {
949 char tmp_src[PATH_MAX];
950 char tmp_dst[PATH_MAX];
952 /* atomically replace old archived file */
953 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
955 rc = rename(dst, src);
958 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
961 /* rename lov file */
962 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
963 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
964 rc = rename(tmp_dst, tmp_src);
966 CT_ERROR(errno, "cannot rename '%s' to '%s'",
970 if (opt.o_shadow_tree) {
971 /* Create a namespace of softlinks that shadows the original
972 * Lustre namespace. This will only be current at
973 * time-of-archive (won't follow renames).
974 * WARNING: release won't kill these links; a manual
975 * cleanup of dead links would be required.
978 long long recno = -1;
984 sprintf(buf, DFID, PFID(&hai->hai_fid));
985 sprintf(src, "%s/shadow/", opt.o_hsm_root);
987 ptr = opt.o_hsm_root;
989 (*ptr++ == '/') ? depth-- : 0;
991 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
992 sizeof(src) - strlen(src), &recno, &linkno);
994 CT_ERROR(rc, "cannot get FID of '%s'", buf);
995 rcf = rcf ? rcf : rc;
999 /* Figure out how many parent dirs to symlink back */
1002 (*ptr++ == '/') ? depth++ : 0;
1007 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1009 if (ct_mkdir_p(src)) {
1010 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1011 rcf = rcf ? rcf : -errno;
1014 /* symlink already exists ? */
1015 sz = readlink(src, buf, sizeof(buf));
1016 /* detect truncation */
1017 if (sz == sizeof(buf)) {
1018 rcf = rcf ? rcf : -E2BIG;
1019 CT_ERROR(rcf, "readlink '%s' truncated", src);
1024 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1025 if (unlink(src) && errno != ENOENT) {
1027 "cannot unlink symlink '%s'",
1029 rcf = rcf ? rcf : -errno;
1031 /* unlink old symlink done */
1032 CT_TRACE("remove old symlink '%s' pointing"
1033 " to '%s'", src, buf);
1036 /* symlink already ok */
1037 CT_TRACE("symlink '%s' already pointing"
1038 " to '%s'", src, dst);
1043 if (symlink(dst, src)) {
1044 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1046 rcf = rcf ? rcf : -errno;
1049 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1061 if (ct_is_retryable(rc))
1062 hp_flags |= HP_FLAG_RETRY;
1073 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1078 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1080 struct hsm_copyaction_private *hcp = NULL;
1083 char lov_buf[XATTR_SIZE_MAX];
1084 size_t lov_size = sizeof(lov_buf);
1093 /* we fill lustre so:
1094 * source = lustre FID in the backend
1095 * destination = data FID = volatile file
1098 /* build backend file name from released file FID */
1099 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1101 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1104 CT_ERROR(rc, "cannot get mdt index "DFID"",
1105 PFID(&hai->hai_fid));
1108 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1109 * dependency on the structure format. */
1110 rc = ct_load_stripe(src, lov_buf, &lov_size);
1112 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1113 src, strerror(-rc));
1116 open_flags |= O_LOV_DELAY_CREATE;
1120 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1124 /* get the FID of the volatile file */
1125 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1127 CT_ERROR(rc, "restoring "DFID
1128 ", cannot get FID of created volatile file",
1129 PFID(&hai->hai_fid));
1133 /* build volatile "file name", for messages */
1134 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1136 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1138 if (opt.o_dry_run) {
1143 src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
1146 CT_ERROR(rc, "cannot open '%s' for read", src);
1150 dst_fd = llapi_hsm_action_get_fd(hcp);
1153 CT_ERROR(rc, "cannot open '%s' for write", dst);
1158 /* the layout cannot be allocated through .fid so we have to
1159 * restore a layout */
1160 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1162 CT_ERROR(rc, "cannot restore file striping info"
1163 " for '%s' from '%s'", dst, src);
1169 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1171 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1174 if (ct_is_retryable(rc))
1175 hp_flags |= HP_FLAG_RETRY;
1179 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1182 rc = ct_fini(&hcp, hai, hp_flags, rc);
1184 /* object swaping is done by cdt at copy end, so close of volatile file
1185 * cannot be done before */
1195 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1197 struct hsm_copyaction_private *hcp = NULL;
1201 rc = ct_begin(&hcp, hai);
1205 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1207 CT_TRACE("removing file '%s'", dst);
1209 if (opt.o_dry_run) {
1217 CT_ERROR(rc, "cannot unlink '%s'", dst);
1222 strlcat(dst, ".lov", sizeof(dst));
1226 CT_ERROR(rc, "cannot unlink '%s'", dst);
1232 rc = ct_fini(&hcp, hai, 0, rc);
1237 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1241 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1242 /* Print the original path */
1244 char path[PATH_MAX];
1245 long long recno = -1;
1248 sprintf(fid, DFID, PFID(&hai->hai_fid));
1249 CT_TRACE("'%s' action %s reclen %d, cookie="LPX64,
1250 fid, hsm_copytool_action2name(hai->hai_action),
1251 hai->hai_len, hai->hai_cookie);
1252 rc = llapi_fid2path(opt.o_mnt, fid, path,
1253 sizeof(path), &recno, &linkno);
1255 CT_ERROR(rc, "cannot get path of FID %s", fid);
1257 CT_TRACE("processing file '%s'", path);
1260 switch (hai->hai_action) {
1261 /* set err_major, minor inside these functions */
1263 rc = ct_archive(hai, hal_flags);
1266 rc = ct_restore(hai, hal_flags);
1269 rc = ct_remove(hai, hal_flags);
1272 CT_TRACE("cancel not implemented for file system '%s'",
1274 /* Don't report progress to coordinator for this cookie:
1275 * the copy function will get ECANCELED when reporting
1282 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1285 ct_fini(NULL, hai, 0, rc);
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(rc, "pthread_attr_init failed for '%s' service",
1338 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1340 rc = pthread_create(&thread, &attr, ct_thread, data);
1342 CT_ERROR(rc, "cannot create thread for '%s' service",
1345 pthread_attr_destroy(&attr);
1349 static int ct_import_one(const char *src, const char *dst)
1351 char newarc[PATH_MAX];
1356 CT_TRACE("importing '%s' from '%s'", dst, src);
1358 if (stat(src, &st) < 0) {
1360 CT_ERROR(rc, "cannot stat '%s'", src);
1367 rc = llapi_hsm_import(dst,
1368 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1369 &st, 0, 0, 0, 0, NULL, &fid);
1371 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1375 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1377 rc = ct_mkdir_p(newarc);
1379 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1385 /* Lots of choices now: mv, ln, ln -s ? */
1386 rc = link(src, newarc); /* hardlink */
1389 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1393 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1398 static char *path_concat(const char *dirname, const char *basename)
1403 rc = asprintf(&result, "%s/%s", dirname, basename);
1410 static int ct_import_fid(const lustre_fid *import_fid)
1412 char fid_path[PATH_MAX];
1415 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1416 rc = access(fid_path, F_OK);
1417 if (rc == 0 || errno != ENOENT) {
1418 rc = (errno == 0) ? -EEXIST : -errno;
1419 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1423 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1426 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1428 return ct_import_one(fid_path, opt.o_dst);
1431 static int ct_import_recurse(const char *relpath)
1434 struct dirent ent, *cookie = NULL;
1435 char *srcpath, *newpath;
1436 lustre_fid import_fid;
1439 if (relpath == NULL)
1442 /* Is relpath a FID? In which case SFID should expand to three
1444 rc = sscanf(relpath, SFID, RFID(&import_fid));
1446 return ct_import_fid(&import_fid);
1448 srcpath = path_concat(opt.o_hsm_root, relpath);
1449 if (srcpath == NULL) {
1454 dir = opendir(srcpath);
1456 /* Not a dir, or error */
1457 if (errno == ENOTDIR) {
1458 /* Single regular file case, treat o_dst as absolute
1460 rc = ct_import_one(srcpath, opt.o_dst);
1463 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1472 rc = readdir_r(dir, &ent, &cookie);
1475 CT_ERROR(rc, "cannot readdir_r '%s'", relpath);
1478 } else if ((rc == 0) && (cookie == NULL)) {
1479 /* end of directory */
1483 if (!strcmp(ent.d_name, ".") ||
1484 !strcmp(ent.d_name, ".."))
1487 /* New relative path */
1488 newpath = path_concat(relpath, ent.d_name);
1489 if (newpath == NULL) {
1495 if (ent.d_type == DT_DIR) {
1496 rc = ct_import_recurse(newpath);
1501 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1502 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1503 /* Make the target dir in the Lustre fs */
1504 rc = ct_mkdir_p(dst);
1506 /* Import the file */
1507 rc = ct_import_one(src, dst);
1509 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1515 CT_ERROR(rc, "cannot import '%s'", newpath);
1516 if (err_major && opt.o_abort_on_error) {
1530 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1536 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1538 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1539 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1541 if (!opt.o_dry_run) {
1543 if (rename(src, dst)) {
1545 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1548 /* rename lov file */
1549 strlcat(src, ".lov", sizeof(src));
1550 strlcat(dst, ".lov", sizeof(dst));
1551 if (rename(src, dst))
1552 CT_ERROR(errno, "cannot rename '%s' to '%s'", src, dst);
1558 static bool fid_is_file(lustre_fid *fid)
1560 return fid_is_norm(fid) || fid_is_igif(fid);
1563 static bool should_ignore_line(const char *line)
1567 for (i = 0; line[i] != '\0'; i++) {
1568 if (isspace(line[i]))
1570 else if (line[i] == '#')
1579 static int ct_rebind_list(const char *list)
1585 size_t line_size = 0;
1586 unsigned int nl = 0;
1587 unsigned int ok = 0;
1589 filp = fopen(list, "r");
1592 CT_ERROR(rc, "cannot open '%s'", list);
1596 /* each line consists of 2 FID */
1597 while ((r = getline(&line, &line_size, filp)) != -1) {
1601 /* Ignore empty and commented out ('#...') lines. */
1602 if (should_ignore_line(line))
1607 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1608 if (rc != 6 || !fid_is_file(&old_fid) ||
1609 !fid_is_file(&new_fid)) {
1611 "'%s' FID expected near '%s', line %u",
1617 if (ct_rebind_one(&old_fid, &new_fid))
1628 /* return 0 if all rebinds were sucessful */
1629 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1631 return ok == nl ? 0 : -1;
1634 static int ct_rebind(void)
1642 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1643 !fid_is_file(&old_fid)) {
1645 CT_ERROR(rc, "'%s' invalid FID format", opt.o_src);
1649 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1650 !fid_is_file(&new_fid)) {
1652 CT_ERROR(rc, "'%s' invalid FID format", opt.o_dst);
1656 rc = ct_rebind_one(&old_fid, &new_fid);
1661 /* o_src is a list file */
1662 rc = ct_rebind_list(opt.o_src);
1667 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1672 struct dirent ent, *cookie = NULL;
1676 dir = opendir(dirpath);
1679 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1683 while ((rc = readdir_r(dir, &ent, &cookie)) == 0) {
1685 /* end of directory.
1686 * rc is 0 and seqmax contains the max value. */
1689 if (!strcmp(ent.d_name, ".") || !strcmp(ent.d_name, ".."))
1692 if (sscanf(ent.d_name, "%hx", &sub_seq) != 1) {
1693 CT_TRACE("'%s' has an unexpected dirname format, "
1694 "skip entry", ent.d_name);
1697 if (sub_seq > *sub_seqmax)
1698 *sub_seqmax = sub_seq;
1701 CT_ERROR(rc, "cannot readdir_r '%s'", dirpath);
1708 static int ct_max_sequence(void)
1711 char path[PATH_MAX];
1715 strlcpy(path, opt.o_hsm_root, sizeof(path));
1716 /* FID sequence is stored in top-level directory names:
1717 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1719 for (i = 0; i < 4; i++) {
1722 rc = ct_dir_level_max(path, &subseq);
1725 seq |= ((__u64)subseq << ((3 - i) * 16));
1726 path_len = strlen(path);
1727 rc = snprintf(path + path_len, sizeof(path) - path_len,
1729 if (rc >= (sizeof(path) - path_len))
1731 path[sizeof(path) - 1] = '\0';
1734 printf("max_sequence: "LPX64"\n", seq);
1739 static void handler(int signal)
1741 psignal(signal, "exiting");
1742 /* If we don't clean up upon interrupt, umount thinks there's a ref
1743 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1744 * does successfully unmount and the mount is actually gone, but the
1745 * mtab entry remains. So this just makes mtab happier. */
1746 llapi_hsm_copytool_unregister(&ctdata);
1750 /* Daemon waits for messages from the kernel; run it in the background. */
1751 static int ct_run(void)
1755 if (opt.o_daemonize) {
1759 CT_ERROR(rc, "cannot daemonize");
1764 setbuf(stdout, NULL);
1766 if (opt.o_event_fifo != NULL) {
1767 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
1769 CT_ERROR(rc, "failed to register event fifo");
1772 llapi_error_callback_set(llapi_hsm_log_error);
1775 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
1777 opt.o_archive_id, 0);
1779 CT_ERROR(rc, "cannot start copytool interface");
1783 signal(SIGINT, handler);
1784 signal(SIGTERM, handler);
1787 struct hsm_action_list *hal;
1788 struct hsm_action_item *hai;
1792 CT_TRACE("waiting for message from kernel");
1794 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1795 if (rc == -ESHUTDOWN) {
1796 CT_TRACE("shutting down");
1798 } else if (rc < 0) {
1799 CT_WARN("cannot receive action list: %s",
1802 if (opt.o_abort_on_error)
1808 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1809 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1811 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1813 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
1814 hal->hal_fsname, fs_name);
1816 if (opt.o_abort_on_error)
1822 hai = hai_first(hal);
1823 while (++i <= hal->hal_count) {
1824 if ((char *)hai - (char *)hal > msgsize) {
1827 "'%s' item %d past end of message!",
1832 rc = ct_process_item_async(hai, hal->hal_flags);
1834 CT_ERROR(rc, "'%s' item %d process",
1836 if (opt.o_abort_on_error && err_major)
1838 hai = hai_next(hai);
1841 if (opt.o_abort_on_error && err_major)
1845 llapi_hsm_copytool_unregister(&ctdata);
1846 if (opt.o_event_fifo != NULL)
1847 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1852 static int ct_setup(void)
1856 /* set llapi message level */
1857 llapi_msg_set_level(opt.o_verbose);
1859 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1862 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1866 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1868 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
1873 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
1874 if (opt.o_mnt_fd < 0) {
1876 CT_ERROR(rc, "cannot open mount point at '%s'",
1884 static int ct_cleanup(void)
1888 if (opt.o_mnt_fd >= 0) {
1889 rc = close(opt.o_mnt_fd);
1892 CT_ERROR(rc, "cannot close mount point");
1901 CT_ERROR(rc, "cannot close archive root directory");
1909 int main(int argc, char **argv)
1913 strlcpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1914 rc = ct_parseopts(argc, argv);
1916 CT_WARN("try '%s --help' for more information", cmd_name);
1924 switch (opt.o_action) {
1926 rc = ct_import_recurse(opt.o_src);
1932 rc = ct_max_sequence();
1939 if (opt.o_action != CA_MAXSEQ)
1940 CT_TRACE("process finished, errs: %d major, %d minor,"
1941 " rc=%d (%s)", err_major, err_minor, rc,