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
26 * Copyright (c) 2013, 2016, Intel Corporation.
28 /* HSM copytool program for POSIX filesystem-based HSM's.
30 * An HSM copytool daemon acts on action requests from Lustre to copy files
31 * to and from an HSM archive system. This one in particular makes regular
32 * POSIX filesystem calls to a given path, where an HSM is presumably mounted.
34 * This particular tool can also import an existing HSM archive.
55 #include <sys/xattr.h>
56 #include <sys/syscall.h>
57 #include <sys/types.h>
59 #include <libcfs/util/string.h>
60 #include <linux/lustre_fid.h>
61 #include <lustre/lustreapi.h>
63 /* Progress reporting period */
64 #define REPORT_INTERVAL_DEFAULT 30
65 /* HSM hash subdir permissions */
66 #define DIR_PERM S_IRWXU
67 /* HSM hash file permissions */
68 #define FILE_PERM (S_IRUSR | S_IWUSR)
70 #define ONE_MB 0x100000
73 # define NSEC_PER_SEC 1000000000UL
91 int o_archive_id[LL_HSM_MAX_ARCHIVE];
93 unsigned long long o_bandwidth;
95 enum ct_action o_action;
100 char *o_src; /* for import, or rebind */
101 char *o_dst; /* for import, or rebind */
104 /* everything else is zeroed */
105 struct options opt = {
108 .o_verbose = LLAPI_MSG_INFO,
110 .o_report_int = REPORT_INTERVAL_DEFAULT,
111 .o_chunk_size = ONE_MB,
114 /* hsm_copytool_private will hold an open FD on the lustre mount point
115 * for us. Additionally open one on the archive FS root to make sure
116 * it doesn't drop out from under us (and remind the admin to shutdown
117 * the copytool before unmounting). */
119 static int arc_fd = -1;
121 static int err_major;
122 static int err_minor;
124 static char cmd_name[PATH_MAX];
125 static char fs_name[MAX_OBD_NAME + 1];
127 static struct hsm_copytool_private *ctdata;
129 static inline double ct_now(void)
133 gettimeofday(&tv, NULL);
135 return tv.tv_sec + 0.000001 * tv.tv_usec;
138 #define CT_ERROR(_rc, _format, ...) \
139 llapi_error(LLAPI_MSG_ERROR, _rc, \
140 "%f %s[%ld]: "_format, \
141 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
143 #define CT_DEBUG(_format, ...) \
144 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
145 "%f %s[%ld]: "_format, \
146 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
148 #define CT_WARN(_format, ...) \
149 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
150 "%f %s[%ld]: "_format, \
151 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
153 #define CT_TRACE(_format, ...) \
154 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
155 "%f %s[%ld]: "_format, \
156 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
158 static void usage(const char *name, int rc)
161 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
162 "The Lustre HSM Posix copy tool can be used as a daemon or "
163 "as a command line tool\n"
164 "The Lustre HSM daemon acts on action requests from Lustre\n"
165 "to copy files to and from an HSM archive system.\n"
166 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
167 "to an HSM mounted at a given hsm_root.\n"
168 " --daemon Daemon mode, run in background\n"
170 " --no-attr Don't copy file attributes\n"
171 " --no-shadow Don't create shadow namespace in archive\n"
172 " --no-xattr Don't copy file extended attributes\n"
173 "The Lustre HSM tool performs administrator-type actions\n"
174 "on a Lustre HSM archive.\n"
175 "This POSIX-flavored tool can link an existing HSM namespace\n"
176 "into a Lustre filesystem.\n"
178 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
179 " import an archived subtree from\n"
180 " <src> (FID or relative path to hsm_root) into the Lustre\n"
182 " <dst> (absolute path)\n"
183 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
184 " rebind an entry in the HSM to a new FID\n"
185 " <old_FID> old FID the HSM entry is bound to\n"
186 " <new_FID> new FID to bind the HSM entry to\n"
187 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
188 " perform the rebind operation for all FID in the list file\n"
189 " each line of <list_file> consists of <old_FID> <new_FID>\n"
190 " %s [options] --max-sequence <fsname>\n"
191 " return the max fid sequence of archived files\n"
192 " --abort-on-error Abort operation on major error\n"
193 " -A, --archive <#> Archive number (repeatable)\n"
194 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
196 " --dry-run Don't run, just show what would be done\n"
197 " -c, --chunk-size <sz> I/O size used during data copy\n"
198 " (unit can be used, default is MB)\n"
199 " -f, --event-fifo <path> Write events stream to fifo\n"
200 " -p, --hsm-root <path> Target HSM mount point\n"
201 " -q, --quiet Produce less verbose output\n"
202 " -u, --update-interval <s> Interval between progress reports sent\n"
204 " -v, --verbose Produce more verbose output\n",
205 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
210 static int ct_parseopts(int argc, char * const *argv)
212 struct option long_opts[] = {
213 { .val = 1, .name = "abort-on-error",
214 .flag = &opt.o_abort_on_error, .has_arg = no_argument },
215 { .val = 1, .name = "abort_on_error",
216 .flag = &opt.o_abort_on_error, .has_arg = no_argument },
217 { .val = 'A', .name = "archive", .has_arg = required_argument },
218 { .val = 'b', .name = "bandwidth", .has_arg = required_argument },
219 { .val = 'c', .name = "chunk-size", .has_arg = required_argument },
220 { .val = 'c', .name = "chunk_size", .has_arg = required_argument },
221 { .val = 1, .name = "daemon", .has_arg = no_argument,
222 .flag = &opt.o_daemonize },
223 { .val = 'f', .name = "event-fifo", .has_arg = required_argument },
224 { .val = 'f', .name = "event_fifo", .has_arg = required_argument },
225 { .val = 1, .name = "dry-run", .has_arg = no_argument,
226 .flag = &opt.o_dry_run },
227 { .val = 'h', .name = "help", .has_arg = no_argument },
228 { .val = 'i', .name = "import", .has_arg = no_argument },
229 { .val = 'M', .name = "max-sequence", .has_arg = no_argument },
230 { .val = 'M', .name = "max_sequence", .has_arg = no_argument },
231 { .val = 0, .name = "no-attr", .has_arg = no_argument,
232 .flag = &opt.o_copy_attrs },
233 { .val = 0, .name = "no_attr", .has_arg = no_argument,
234 .flag = &opt.o_copy_attrs },
235 { .val = 0, .name = "no-shadow", .has_arg = no_argument,
236 .flag = &opt.o_shadow_tree },
237 { .val = 0, .name = "no_shadow", .has_arg = no_argument,
238 .flag = &opt.o_shadow_tree },
239 { .val = 0, .name = "no-xattr", .has_arg = no_argument,
240 .flag = &opt.o_copy_xattrs },
241 { .val = 0, .name = "no_xattr", .has_arg = no_argument,
242 .flag = &opt.o_copy_xattrs },
243 { .val = 'p', .name = "hsm-root", .has_arg = required_argument },
244 { .val = 'p', .name = "hsm_root", .has_arg = required_argument },
245 { .val = 'q', .name = "quiet", .has_arg = no_argument },
246 { .val = 'r', .name = "rebind", .has_arg = no_argument },
247 { .val = 'u', .name = "update-interval",
248 .has_arg = required_argument },
249 { .val = 'u', .name = "update_interval",
250 .has_arg = required_argument },
251 { .val = 'v', .name = "verbose", .has_arg = no_argument },
254 unsigned long long value;
255 unsigned long long unit;
258 while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:qru:v",
259 long_opts, NULL)) != -1) {
262 if ((opt.o_archive_cnt >= LL_HSM_MAX_ARCHIVE) ||
263 (atoi(optarg) >= LL_HSM_MAX_ARCHIVE)) {
265 CT_ERROR(rc, "archive number must be less"
266 "than %zu", LL_HSM_MAX_ARCHIVE);
269 opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
272 case 'b': /* -b and -c have both a number with unit as arg */
275 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
277 CT_ERROR(rc, "bad value for -%c '%s'", c,
282 opt.o_chunk_size = value;
284 opt.o_bandwidth = value;
287 opt.o_event_fifo = optarg;
292 opt.o_action = CA_IMPORT;
295 opt.o_action = CA_MAXSEQ;
298 opt.o_hsm_root = optarg;
304 opt.o_action = CA_REBIND;
307 opt.o_report_int = atoi(optarg);
308 if (opt.o_report_int < 0) {
310 CT_ERROR(rc, "bad value for -%c '%s'", c,
325 switch (opt.o_action) {
327 /* src dst mount_point */
328 if (argc != optind + 3) {
330 CT_ERROR(rc, "--import requires 2 arguments");
333 opt.o_src = argv[optind++];
334 opt.o_dst = argv[optind++];
337 /* FID1 FID2 mount_point or FILE mount_point */
338 if (argc == optind + 2) {
339 opt.o_src = argv[optind++];
341 } else if (argc == optind + 3) {
342 opt.o_src = argv[optind++];
343 opt.o_dst = argv[optind++];
346 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
352 /* just mount point */
356 if (argc != optind + 1) {
358 CT_ERROR(rc, "no mount point specified");
362 opt.o_mnt = argv[optind];
365 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
366 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
368 if (opt.o_hsm_root == NULL) {
370 CT_ERROR(rc, "must specify a root directory for the backend");
374 if (opt.o_action == CA_IMPORT) {
375 if (opt.o_src && opt.o_src[0] == '/') {
378 "source path must be relative to HSM root");
382 if (opt.o_dst && opt.o_dst[0] != '/') {
384 CT_ERROR(rc, "destination path must be absolute");
393 static int ct_mkdir_p(const char *path)
406 while ((ptr = strchr(ptr, '/')) != NULL) {
408 rc = mkdir(saved, DIR_PERM);
410 if (rc < 0 && errno != EEXIST) {
412 CT_ERROR(rc, "cannot mkdir '%s'", path);
424 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
426 char lov_file[PATH_MAX];
427 char lov_buf[XATTR_SIZE_MAX];
428 struct lov_user_md *lum;
433 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
434 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
436 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
438 if (xattr_size < 0) {
440 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
444 lum = (struct lov_user_md *)lov_buf;
446 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
447 lum->lmm_magic == LOV_USER_MAGIC_V3) {
448 /* Set stripe_offset to -1 so that it is not interpreted as a
449 * hint on restore. */
450 lum->lmm_stripe_offset = -1;
453 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
456 CT_ERROR(rc, "cannot open '%s'", lov_file);
460 rc = write(fd, lum, xattr_size);
463 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
464 xattr_size, lov_file);
472 CT_ERROR(rc, "cannot close '%s'", lov_file);
484 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
486 char lov_file[PATH_MAX];
490 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
491 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
493 fd = open(lov_file, O_RDONLY);
495 CT_ERROR(errno, "cannot open '%s'", lov_file);
499 rc = read(fd, lovea, *lovea_size);
501 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
502 *lovea_size, lov_file);
513 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
514 const void *lovea, size_t lovea_size)
518 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
522 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
528 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
529 const char *dst, int src_fd, int dst_fd,
530 const struct hsm_action_item *hai, long hal_flags)
532 struct hsm_extent he;
533 __u64 offset = hai->hai_extent.offset;
537 __u64 write_total = 0;
538 __u64 length = hai->hai_extent.length;
539 time_t last_report_time;
541 double start_ct_now = ct_now();
542 /* Bandwidth Control */
545 time_t last_bw_print;
547 if (fstat(src_fd, &src_st) < 0) {
549 CT_ERROR(rc, "cannot stat '%s'", src);
553 if (!S_ISREG(src_st.st_mode)) {
555 CT_ERROR(rc, "'%s' is not a regular file", src);
559 if (hai->hai_extent.offset > (__u64)src_st.st_size) {
561 CT_ERROR(rc, "Trying to start reading past end (%ju > "
562 "%jd) of '%s' source file",
563 (uintmax_t)hai->hai_extent.offset,
564 (intmax_t)src_st.st_size, src);
568 if (fstat(dst_fd, &dst_st) < 0) {
570 CT_ERROR(rc, "cannot stat '%s'", dst);
574 if (!S_ISREG(dst_st.st_mode)) {
576 CT_ERROR(rc, "'%s' is not a regular file", dst);
580 /* Don't read beyond a given extent */
581 if (length > src_st.st_size - hai->hai_extent.offset)
582 length = src_st.st_size - hai->hai_extent.offset;
584 start_time = last_bw_print = last_report_time = time(NULL);
588 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
590 /* Action has been canceled or something wrong
591 * is happening. Stop copying data. */
592 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
599 buf = malloc(opt.o_chunk_size);
605 CT_TRACE("start copy of %ju bytes from '%s' to '%s'",
606 (uintmax_t)length, src, dst);
608 while (write_total < length) {
611 int chunk = (length - write_total > opt.o_chunk_size) ?
612 opt.o_chunk_size : length - write_total;
614 rsize = pread(src_fd, buf, chunk, offset);
621 CT_ERROR(rc, "cannot read from '%s'", src);
625 wsize = pwrite(dst_fd, buf, rsize, offset);
628 CT_ERROR(rc, "cannot write to '%s'", dst);
632 write_total += wsize;
636 /* sleep if needed, to honor bandwidth limits */
637 if (opt.o_bandwidth != 0) {
638 unsigned long long write_theory;
640 write_theory = (now - start_time) * opt.o_bandwidth;
642 if (write_theory < write_total) {
643 unsigned long long excess;
644 struct timespec delay;
646 excess = write_total - write_theory;
648 delay.tv_sec = excess / opt.o_bandwidth;
649 delay.tv_nsec = (excess % opt.o_bandwidth) *
650 NSEC_PER_SEC / opt.o_bandwidth;
652 if (now >= last_bw_print + opt.o_report_int) {
653 CT_TRACE("bandwith control: %lluB/s "
654 "excess=%llu sleep for "
656 opt.o_bandwidth, excess,
657 (long long)delay.tv_sec,
663 rc = nanosleep(&delay, &delay);
664 } while (rc < 0 && errno == EINTR);
666 CT_ERROR(errno, "delay for bandwidth "
667 "control failed to sleep: "
668 "residual=%lld.%09lds",
669 (long long)delay.tv_sec,
677 if (now >= last_report_time + opt.o_report_int) {
678 last_report_time = now;
679 CT_TRACE("%%%ju ", (uintmax_t)(100 * write_total / length));
680 /* only give the length of the write since the last
682 he.length = offset - he.offset;
683 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
685 /* Action has been canceled or something wrong
686 * is happening. Stop copying data. */
687 CT_ERROR(rc, "progress ioctl for copy"
688 " '%s'->'%s' failed", src, dst);
698 * truncate restored file
699 * size is taken from the archive this is done to support
700 * restore after a force release which leaves the file with the
701 * wrong size (can big bigger than the new size)
703 if ((hai->hai_action == HSMA_RESTORE) &&
704 (src_st.st_size < dst_st.st_size)) {
706 * make sure the file is on disk before reporting success.
708 rc = ftruncate(dst_fd, src_st.st_size);
711 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
712 dst, (intmax_t)src_st.st_size);
720 CT_TRACE("copied %ju bytes in %f seconds",
721 (uintmax_t)length, ct_now() - start_ct_now);
726 /* Copy file attributes from file src to file dest */
727 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
731 struct timeval times[2];
734 if (fstat(src_fd, &st) < 0) {
736 CT_ERROR(rc, "cannot stat '%s'", src);
740 times[0].tv_sec = st.st_atime;
741 times[0].tv_usec = 0;
742 times[1].tv_sec = st.st_mtime;
743 times[1].tv_usec = 0;
744 if (fchmod(dst_fd, st.st_mode) < 0 ||
745 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
746 futimes(dst_fd, times) < 0) {
748 CT_ERROR(rc, "cannot set attributes of '%s'", src);
755 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
756 int dst_fd, bool is_restore)
758 char list[XATTR_LIST_MAX];
759 char value[XATTR_SIZE_MAX];
764 list_len = flistxattr(src_fd, list, sizeof(list));
769 while (name < list + list_len) {
770 rc = fgetxattr(src_fd, name, value, sizeof(value));
774 /* when we restore, we do not restore lustre xattr */
776 (strncmp(XATTR_TRUSTED_PREFIX, name,
777 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
778 rc = fsetxattr(dst_fd, name, value, rc, 0);
779 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
780 name, dst, rc, strerror(errno));
781 /* lustre.* attrs aren't supported on other FS's */
782 if (rc < 0 && errno != EOPNOTSUPP) {
784 CT_ERROR(rc, "cannot set extended attribute"
790 name += strlen(name) + 1;
796 static int ct_path_lustre(char *buf, int sz, const char *mnt,
797 const lustre_fid *fid)
799 return snprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
800 dot_lustre_name, PFID(fid));
803 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
804 const lustre_fid *fid)
806 return snprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
807 DFID_NOBRACE, archive_dir,
808 (fid)->f_oid & 0xFFFF,
809 (fid)->f_oid >> 16 & 0xFFFF,
810 (unsigned int)((fid)->f_seq & 0xFFFF),
811 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
812 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
813 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
817 static bool ct_is_retryable(int err)
819 return err == -ETIMEDOUT;
822 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
823 const struct hsm_action_item *hai,
824 int mdt_index, int open_flags)
829 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
832 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
833 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
839 static int ct_begin(struct hsm_copyaction_private **phcp,
840 const struct hsm_action_item *hai)
842 /* Restore takes specific parameters. Call the same function w/ default
843 * values for all other operations. */
844 return ct_begin_restore(phcp, hai, -1, 0);
847 static int ct_fini(struct hsm_copyaction_private **phcp,
848 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
850 struct hsm_copyaction_private *hcp;
854 CT_TRACE("Action completed, notifying coordinator "
855 "cookie=%#jx, FID="DFID", hp_flags=%d err=%d",
856 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid),
859 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
861 if (phcp == NULL || *phcp == NULL) {
862 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
864 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
871 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
872 if (rc == -ECANCELED)
873 CT_ERROR(rc, "completed action on '%s' has been canceled: "
874 "cookie=%#jx, FID="DFID, lstr,
875 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid));
877 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
879 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
885 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
887 struct hsm_copyaction_private *hcp = NULL;
889 char dst[PATH_MAX] = "";
892 bool rename_needed = false;
898 rc = ct_begin(&hcp, hai);
902 /* we fill archive so:
904 * destination = lustre FID
906 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
907 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
908 if (hai->hai_extent.length == -1) {
909 /* whole file, write it to tmp location and atomically
910 * replace old archived file */
911 strlcat(dst, "_tmp", sizeof(dst));
912 /* we cannot rely on the same test because ct_copy_data()
913 * updates hai_extent.length */
914 rename_needed = true;
917 CT_TRACE("archiving '%s' to '%s'", src, dst);
924 rc = ct_mkdir_p(dst);
926 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
930 src_fd = llapi_hsm_action_get_fd(hcp);
933 CT_ERROR(rc, "cannot open '%s' for read", src);
937 open_flags = O_WRONLY | O_NOFOLLOW;
938 /* If extent is specified, don't truncate an old archived copy */
939 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
941 dst_fd = open(dst, open_flags, FILE_PERM);
944 CT_ERROR(rc, "cannot open '%s' for write", dst);
948 /* saving stripe is not critical */
949 rc = ct_save_stripe(src_fd, src, dst);
951 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
954 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
956 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
963 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
967 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
969 /* attrs will remain on the MDS; no need to copy them, except possibly
970 for disaster recovery */
971 if (opt.o_copy_attrs) {
972 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
974 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
978 CT_TRACE("attr file for '%s' saved to archive '%s'",
982 /* xattrs will remain on the MDS; no need to copy them, except possibly
983 for disaster recovery */
984 if (opt.o_copy_xattrs) {
985 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
987 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
989 rcf = rcf ? rcf : rc;
991 CT_TRACE("xattr file for '%s' saved to archive '%s'",
995 if (rename_needed == true) {
996 char tmp_src[PATH_MAX];
997 char tmp_dst[PATH_MAX];
999 /* atomically replace old archived file */
1000 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
1002 rc = rename(dst, src);
1005 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
1008 /* rename lov file */
1009 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
1010 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
1011 rc = rename(tmp_dst, tmp_src);
1013 CT_ERROR(errno, "cannot rename '%s' to '%s'",
1017 if (opt.o_shadow_tree) {
1018 /* Create a namespace of softlinks that shadows the original
1019 * Lustre namespace. This will only be current at
1020 * time-of-archive (won't follow renames).
1021 * WARNING: release won't kill these links; a manual
1022 * cleanup of dead links would be required.
1025 long long recno = -1;
1031 sprintf(buf, DFID, PFID(&hai->hai_fid));
1032 sprintf(src, "%s/shadow/", opt.o_hsm_root);
1034 ptr = opt.o_hsm_root;
1036 (*ptr++ == '/') ? depth-- : 0;
1038 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
1039 sizeof(src) - strlen(src), &recno, &linkno);
1041 CT_ERROR(rc, "cannot get FID of '%s'", buf);
1042 rcf = rcf ? rcf : rc;
1046 /* Figure out how many parent dirs to symlink back */
1049 (*ptr++ == '/') ? depth++ : 0;
1054 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1056 if (ct_mkdir_p(src)) {
1057 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1058 rcf = rcf ? rcf : -errno;
1061 /* symlink already exists ? */
1062 sz = readlink(src, buf, sizeof(buf));
1063 /* detect truncation */
1064 if (sz == sizeof(buf)) {
1065 rcf = rcf ? rcf : -E2BIG;
1066 CT_ERROR(rcf, "readlink '%s' truncated", src);
1071 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1072 if (unlink(src) && errno != ENOENT) {
1074 "cannot unlink symlink '%s'",
1076 rcf = rcf ? rcf : -errno;
1078 /* unlink old symlink done */
1079 CT_TRACE("remove old symlink '%s' pointing"
1080 " to '%s'", src, buf);
1083 /* symlink already ok */
1084 CT_TRACE("symlink '%s' already pointing"
1085 " to '%s'", src, dst);
1090 if (symlink(dst, src)) {
1091 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1093 rcf = rcf ? rcf : -errno;
1096 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1108 if (ct_is_retryable(rc))
1109 hp_flags |= HP_FLAG_RETRY;
1120 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1125 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1127 struct hsm_copyaction_private *hcp = NULL;
1130 char lov_buf[XATTR_SIZE_MAX];
1131 size_t lov_size = sizeof(lov_buf);
1140 /* we fill lustre so:
1141 * source = lustre FID in the backend
1142 * destination = data FID = volatile file
1145 /* build backend file name from released file FID */
1146 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1148 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1151 CT_ERROR(rc, "cannot get mdt index "DFID"",
1152 PFID(&hai->hai_fid));
1155 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1156 * dependency on the structure format. */
1157 rc = ct_load_stripe(src, lov_buf, &lov_size);
1159 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1160 src, strerror(-rc));
1163 open_flags |= O_LOV_DELAY_CREATE;
1167 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1171 /* get the FID of the volatile file */
1172 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1174 CT_ERROR(rc, "restoring "DFID
1175 ", cannot get FID of created volatile file",
1176 PFID(&hai->hai_fid));
1180 /* build volatile "file name", for messages */
1181 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1183 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1185 if (opt.o_dry_run) {
1190 src_fd = open(src, O_RDONLY | O_NOATIME | O_NOFOLLOW);
1193 CT_ERROR(rc, "cannot open '%s' for read", src);
1197 dst_fd = llapi_hsm_action_get_fd(hcp);
1200 CT_ERROR(rc, "cannot open '%s' for write", dst);
1205 /* the layout cannot be allocated through .fid so we have to
1206 * restore a layout */
1207 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1209 CT_ERROR(rc, "cannot restore file striping info"
1210 " for '%s' from '%s'", dst, src);
1216 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1218 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1221 if (ct_is_retryable(rc))
1222 hp_flags |= HP_FLAG_RETRY;
1226 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1229 rc = ct_fini(&hcp, hai, hp_flags, rc);
1231 /* object swaping is done by cdt at copy end, so close of volatile file
1232 * cannot be done before */
1242 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1244 struct hsm_copyaction_private *hcp = NULL;
1248 rc = ct_begin(&hcp, hai);
1252 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1254 CT_TRACE("removing file '%s'", dst);
1256 if (opt.o_dry_run) {
1264 CT_ERROR(rc, "cannot unlink '%s'", dst);
1269 strlcat(dst, ".lov", sizeof(dst));
1273 CT_ERROR(rc, "cannot unlink '%s'", dst);
1279 rc = ct_fini(&hcp, hai, 0, rc);
1284 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1288 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1289 /* Print the original path */
1291 char path[PATH_MAX];
1292 long long recno = -1;
1295 sprintf(fid, DFID, PFID(&hai->hai_fid));
1296 CT_TRACE("'%s' action %s reclen %d, cookie=%#jx",
1297 fid, hsm_copytool_action2name(hai->hai_action),
1298 hai->hai_len, (uintmax_t)hai->hai_cookie);
1299 rc = llapi_fid2path(opt.o_mnt, fid, path,
1300 sizeof(path), &recno, &linkno);
1302 CT_ERROR(rc, "cannot get path of FID %s", fid);
1304 CT_TRACE("processing file '%s'", path);
1307 switch (hai->hai_action) {
1308 /* set err_major, minor inside these functions */
1310 rc = ct_archive(hai, hal_flags);
1313 rc = ct_restore(hai, hal_flags);
1316 rc = ct_remove(hai, hal_flags);
1319 CT_TRACE("cancel not implemented for file system '%s'",
1321 /* Don't report progress to coordinator for this cookie:
1322 * the copy function will get ECANCELED when reporting
1329 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1332 ct_fini(NULL, hai, 0, rc);
1340 struct hsm_action_item *hai;
1343 static void *ct_thread(void *data)
1345 struct ct_th_data *cttd = data;
1348 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1352 pthread_exit((void *)(intptr_t)rc);
1355 static int ct_process_item_async(const struct hsm_action_item *hai,
1358 pthread_attr_t attr;
1360 struct ct_th_data *data;
1363 data = malloc(sizeof(*data));
1367 data->hai = malloc(hai->hai_len);
1368 if (data->hai == NULL) {
1373 memcpy(data->hai, hai, hai->hai_len);
1374 data->hal_flags = hal_flags;
1376 rc = pthread_attr_init(&attr);
1378 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1385 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1387 rc = pthread_create(&thread, &attr, ct_thread, data);
1389 CT_ERROR(rc, "cannot create thread for '%s' service",
1392 pthread_attr_destroy(&attr);
1396 static int ct_import_one(const char *src, const char *dst)
1398 char newarc[PATH_MAX];
1403 CT_TRACE("importing '%s' from '%s'", dst, src);
1405 if (stat(src, &st) < 0) {
1407 CT_ERROR(rc, "cannot stat '%s'", src);
1414 rc = llapi_hsm_import(dst,
1415 opt.o_archive_cnt ? opt.o_archive_id[0] : 0,
1416 &st, 0, 0, 0, 0, NULL, &fid);
1418 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1422 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1424 rc = ct_mkdir_p(newarc);
1426 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1432 /* Lots of choices now: mv, ln, ln -s ? */
1433 rc = link(src, newarc); /* hardlink */
1436 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1440 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1445 static char *path_concat(const char *dirname, const char *basename)
1450 rc = asprintf(&result, "%s/%s", dirname, basename);
1457 static int ct_import_fid(const lustre_fid *import_fid)
1459 char fid_path[PATH_MAX];
1462 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1463 rc = access(fid_path, F_OK);
1464 if (rc == 0 || errno != ENOENT) {
1465 rc = (errno == 0) ? -EEXIST : -errno;
1466 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1470 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1473 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1475 return ct_import_one(fid_path, opt.o_dst);
1478 static int ct_import_recurse(const char *relpath)
1482 char *srcpath, *newpath;
1483 lustre_fid import_fid;
1486 if (relpath == NULL)
1489 /* Is relpath a FID? In which case SFID should expand to three
1491 rc = sscanf(relpath, SFID, RFID(&import_fid));
1493 return ct_import_fid(&import_fid);
1495 srcpath = path_concat(opt.o_hsm_root, relpath);
1496 if (srcpath == NULL) {
1501 dir = opendir(srcpath);
1503 /* Not a dir, or error */
1504 if (errno == ENOTDIR) {
1505 /* Single regular file case, treat o_dst as absolute
1507 rc = ct_import_one(srcpath, opt.o_dst);
1510 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1518 while ((ent = readdir(dir)) != NULL) {
1519 if (!strcmp(ent->d_name, ".") ||
1520 !strcmp(ent->d_name, ".."))
1523 /* New relative path */
1524 newpath = path_concat(relpath, ent->d_name);
1525 if (newpath == NULL) {
1531 if (ent->d_type == DT_DIR) {
1532 rc = ct_import_recurse(newpath);
1537 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1538 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1539 /* Make the target dir in the Lustre fs */
1540 rc = ct_mkdir_p(dst);
1542 /* Import the file */
1543 rc = ct_import_one(src, dst);
1545 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1551 CT_ERROR(rc, "cannot import '%s'", newpath);
1552 if (err_major && opt.o_abort_on_error) {
1566 static int ct_rebind_one(const lustre_fid *old_fid, const lustre_fid *new_fid)
1572 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1574 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1575 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1577 if (!opt.o_dry_run) {
1579 if (rename(src, dst)) {
1581 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1584 /* rename lov file */
1585 strlcat(src, ".lov", sizeof(src));
1586 strlcat(dst, ".lov", sizeof(dst));
1587 if (rename(src, dst))
1588 CT_ERROR(errno, "cannot rename '%s' to '%s'", src, dst);
1594 static bool fid_is_file(lustre_fid *fid)
1596 return fid_is_norm(fid) || fid_is_igif(fid);
1599 static bool should_ignore_line(const char *line)
1603 for (i = 0; line[i] != '\0'; i++) {
1604 if (isspace(line[i]))
1606 else if (line[i] == '#')
1615 static int ct_rebind_list(const char *list)
1621 size_t line_size = 0;
1622 unsigned int nl = 0;
1623 unsigned int ok = 0;
1625 filp = fopen(list, "r");
1628 CT_ERROR(rc, "cannot open '%s'", list);
1632 /* each line consists of 2 FID */
1633 while ((r = getline(&line, &line_size, filp)) != -1) {
1637 /* Ignore empty and commented out ('#...') lines. */
1638 if (should_ignore_line(line))
1643 rc = sscanf(line, SFID" "SFID, RFID(&old_fid), RFID(&new_fid));
1644 if (rc != 6 || !fid_is_file(&old_fid) ||
1645 !fid_is_file(&new_fid)) {
1647 "'%s' FID expected near '%s', line %u",
1653 if (ct_rebind_one(&old_fid, &new_fid))
1664 /* return 0 if all rebinds were successful */
1665 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1667 return ok == nl ? 0 : -1;
1670 static int ct_rebind(void)
1678 if (sscanf(opt.o_src, SFID, RFID(&old_fid)) != 3 ||
1679 !fid_is_file(&old_fid)) {
1681 CT_ERROR(rc, "'%s' invalid FID format", opt.o_src);
1685 if (sscanf(opt.o_dst, SFID, RFID(&new_fid)) != 3 ||
1686 !fid_is_file(&new_fid)) {
1688 CT_ERROR(rc, "'%s' invalid FID format", opt.o_dst);
1692 rc = ct_rebind_one(&old_fid, &new_fid);
1697 /* o_src is a list file */
1698 rc = ct_rebind_list(opt.o_src);
1703 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1712 dir = opendir(dirpath);
1715 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1723 /* end of directory.
1724 * rc is 0 and seqmax contains the max value. */
1727 CT_ERROR(rc, "cannot readdir '%s'", dirpath);
1731 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
1734 if (sscanf(ent->d_name, "%hx", &sub_seq) != 1) {
1735 CT_TRACE("'%s' has an unexpected dirname format, "
1736 "skip entry", ent->d_name);
1739 if (sub_seq > *sub_seqmax)
1740 *sub_seqmax = sub_seq;
1747 static int ct_max_sequence(void)
1750 char path[PATH_MAX];
1754 strlcpy(path, opt.o_hsm_root, sizeof(path));
1755 /* FID sequence is stored in top-level directory names:
1756 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1758 for (i = 0; i < 4; i++) {
1761 rc = ct_dir_level_max(path, &subseq);
1764 seq |= ((__u64)subseq << ((3 - i) * 16));
1765 path_len = strlen(path);
1766 rc = snprintf(path + path_len, sizeof(path) - path_len,
1768 if (rc >= (sizeof(path) - path_len))
1770 path[sizeof(path) - 1] = '\0';
1773 printf("max_sequence: %#jx\n", (uintmax_t)seq);
1778 static void handler(int signal)
1780 psignal(signal, "exiting");
1781 /* If we don't clean up upon interrupt, umount thinks there's a ref
1782 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1783 * does successfully unmount and the mount is actually gone, but the
1784 * mtab entry remains. So this just makes mtab happier. */
1785 llapi_hsm_copytool_unregister(&ctdata);
1787 /* Also remove fifo upon signal as during normal/error exit */
1788 if (opt.o_event_fifo != NULL)
1789 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1793 /* Daemon waits for messages from the kernel; run it in the background. */
1794 static int ct_run(void)
1796 struct sigaction cleanup_sigaction;
1799 if (opt.o_daemonize) {
1803 CT_ERROR(rc, "cannot daemonize");
1808 setbuf(stdout, NULL);
1810 if (opt.o_event_fifo != NULL) {
1811 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
1813 CT_ERROR(rc, "failed to register event fifo");
1816 llapi_error_callback_set(llapi_hsm_log_error);
1819 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
1821 opt.o_archive_id, 0);
1823 CT_ERROR(rc, "cannot start copytool interface");
1827 memset(&cleanup_sigaction, 0, sizeof(cleanup_sigaction));
1828 cleanup_sigaction.sa_handler = handler;
1829 sigemptyset(&cleanup_sigaction.sa_mask);
1830 sigaction(SIGINT, &cleanup_sigaction, NULL);
1831 sigaction(SIGTERM, &cleanup_sigaction, NULL);
1834 struct hsm_action_list *hal;
1835 struct hsm_action_item *hai;
1839 CT_TRACE("waiting for message from kernel");
1841 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1842 if (rc == -ESHUTDOWN) {
1843 CT_TRACE("shutting down");
1845 } else if (rc < 0) {
1846 CT_WARN("cannot receive action list: %s",
1849 if (opt.o_abort_on_error)
1855 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1856 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1858 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1860 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
1861 hal->hal_fsname, fs_name);
1863 if (opt.o_abort_on_error)
1869 hai = hai_first(hal);
1870 while (++i <= hal->hal_count) {
1871 if ((char *)hai - (char *)hal > msgsize) {
1874 "'%s' item %d past end of message!",
1879 rc = ct_process_item_async(hai, hal->hal_flags);
1881 CT_ERROR(rc, "'%s' item %d process",
1883 if (opt.o_abort_on_error && err_major)
1885 hai = hai_next(hai);
1888 if (opt.o_abort_on_error && err_major)
1892 llapi_hsm_copytool_unregister(&ctdata);
1893 if (opt.o_event_fifo != NULL)
1894 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1899 static int ct_setup(void)
1903 /* set llapi message level */
1904 llapi_msg_set_level(opt.o_verbose);
1906 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1909 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1913 rc = llapi_search_fsname(opt.o_mnt, fs_name);
1915 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
1920 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
1921 if (opt.o_mnt_fd < 0) {
1923 CT_ERROR(rc, "cannot open mount point at '%s'",
1931 static int ct_cleanup(void)
1935 if (opt.o_mnt_fd >= 0) {
1936 rc = close(opt.o_mnt_fd);
1939 CT_ERROR(rc, "cannot close mount point");
1948 CT_ERROR(rc, "cannot close archive root directory");
1956 int main(int argc, char **argv)
1960 strlcpy(cmd_name, basename(argv[0]), sizeof(cmd_name));
1961 rc = ct_parseopts(argc, argv);
1963 CT_WARN("try '%s --help' for more information", cmd_name);
1971 switch (opt.o_action) {
1973 rc = ct_import_recurse(opt.o_src);
1979 rc = ct_max_sequence();
1986 if (opt.o_action != CA_MAXSEQ)
1987 CT_TRACE("process finished, errs: %d major, %d minor,"
1988 " rc=%d (%s)", err_major, err_minor, rc,