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/lustre_fid.h>
61 #include <lustre/lustreapi.h>
64 /* Progress reporting period */
65 #define REPORT_INTERVAL_DEFAULT 30
66 /* HSM hash subdir permissions */
67 #define DIR_PERM S_IRWXU
68 /* HSM hash file permissions */
69 #define FILE_PERM (S_IRUSR | S_IWUSR)
71 #define ONE_MB 0x100000
74 # define NSEC_PER_SEC 1000000000UL
91 int o_archive_id_used;
95 unsigned long long o_bandwidth;
97 enum ct_action o_action;
102 char *o_src; /* for import, or rebind */
103 char *o_dst; /* for import, or rebind */
107 /* everything else is zeroed */
108 struct options opt = {
111 .o_verbose = LLAPI_MSG_INFO,
113 .o_report_int = REPORT_INTERVAL_DEFAULT,
114 .o_chunk_size = ONE_MB,
117 /* hsm_copytool_private will hold an open FD on the lustre mount point
118 * for us. Additionally open one on the archive FS root to make sure
119 * it doesn't drop out from under us (and remind the admin to shutdown
120 * the copytool before unmounting). */
122 static int arc_fd = -1;
124 static int err_major;
125 static int err_minor;
127 static char cmd_name[PATH_MAX];
128 static char fs_name[MAX_OBD_NAME + 1];
129 static int pid_file_fd = -1;
131 static struct hsm_copytool_private *ctdata;
133 static inline double ct_now(void)
137 gettimeofday(&tv, NULL);
139 return tv.tv_sec + 0.000001 * tv.tv_usec;
142 #define CT_ERROR(_rc, _format, ...) \
143 llapi_error(LLAPI_MSG_ERROR, _rc, \
144 "%f %s[%ld]: "_format, \
145 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
147 #define CT_DEBUG(_format, ...) \
148 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
149 "%f %s[%ld]: "_format, \
150 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
152 #define CT_WARN(_format, ...) \
153 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
154 "%f %s[%ld]: "_format, \
155 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
157 #define CT_TRACE(_format, ...) \
158 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
159 "%f %s[%ld]: "_format, \
160 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
162 static void usage(const char *name, int rc)
165 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
166 "The Lustre HSM Posix copy tool can be used as a daemon or "
167 "as a command line tool\n"
168 "The Lustre HSM daemon acts on action requests from Lustre\n"
169 "to copy files to and from an HSM archive system.\n"
170 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
171 "to an HSM mounted at a given hsm_root.\n"
172 " --daemon Daemon mode, run in background\n"
174 " --no-attr Don't copy file attributes\n"
175 " --no-shadow Don't create shadow namespace in archive\n"
176 " --no-xattr Don't copy file extended attributes\n"
177 "The Lustre HSM tool performs administrator-type actions\n"
178 "on a Lustre HSM archive.\n"
179 "This POSIX-flavored tool can link an existing HSM namespace\n"
180 "into a Lustre filesystem.\n"
182 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
183 " import an archived subtree from\n"
184 " <src> (FID or relative path to hsm_root) into the Lustre\n"
186 " <dst> (absolute path)\n"
187 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
188 " rebind an entry in the HSM to a new FID\n"
189 " <old_FID> old FID the HSM entry is bound to\n"
190 " <new_FID> new FID to bind the HSM entry to\n"
191 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
192 " perform the rebind operation for all FID in the list file\n"
193 " each line of <list_file> consists of <old_FID> <new_FID>\n"
194 " %s [options] --max-sequence <fsname>\n"
195 " return the max fid sequence of archived files\n"
196 " --abort-on-error Abort operation on major error\n"
197 " -A, --archive <#> Archive number (repeatable)\n"
198 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
200 " --dry-run Don't run, just show what would be done\n"
201 " -c, --chunk-size <sz> I/O size used during data copy\n"
202 " (unit can be used, default is MB)\n"
203 " -f, --event-fifo <path> Write events stream to fifo\n"
204 " -P, --pid-file=PATH Lock and write PID to PATH\n"
205 " -p, --hsm-root <path> Target HSM mount point\n"
206 " -q, --quiet Produce less verbose output\n"
207 " -u, --update-interval <s> Interval between progress reports sent\n"
209 " -v, --verbose Produce more verbose output\n",
210 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
215 static int ct_parseopts(int argc, char * const *argv)
217 struct option long_opts[] = {
218 { .val = 1, .name = "abort-on-error",
219 .flag = &opt.o_abort_on_error, .has_arg = no_argument },
220 { .val = 1, .name = "abort_on_error",
221 .flag = &opt.o_abort_on_error, .has_arg = no_argument },
222 { .val = 'A', .name = "archive", .has_arg = required_argument },
223 { .val = 'b', .name = "bandwidth", .has_arg = required_argument },
224 { .val = 'c', .name = "chunk-size", .has_arg = required_argument },
225 { .val = 'c', .name = "chunk_size", .has_arg = required_argument },
226 { .val = 1, .name = "daemon", .has_arg = no_argument,
227 .flag = &opt.o_daemonize },
228 { .val = 'f', .name = "event-fifo", .has_arg = required_argument },
229 { .val = 'f', .name = "event_fifo", .has_arg = required_argument },
230 { .val = 1, .name = "dry-run", .has_arg = no_argument,
231 .flag = &opt.o_dry_run },
232 { .val = 'h', .name = "help", .has_arg = no_argument },
233 { .val = 'i', .name = "import", .has_arg = no_argument },
234 { .val = 'M', .name = "max-sequence", .has_arg = no_argument },
235 { .val = 'M', .name = "max_sequence", .has_arg = no_argument },
236 { .val = 0, .name = "no-attr", .has_arg = no_argument,
237 .flag = &opt.o_copy_attrs },
238 { .val = 0, .name = "no_attr", .has_arg = no_argument,
239 .flag = &opt.o_copy_attrs },
240 { .val = 0, .name = "no-shadow", .has_arg = no_argument,
241 .flag = &opt.o_shadow_tree },
242 { .val = 0, .name = "no_shadow", .has_arg = no_argument,
243 .flag = &opt.o_shadow_tree },
244 { .val = 0, .name = "no-xattr", .has_arg = no_argument,
245 .flag = &opt.o_copy_xattrs },
246 { .val = 0, .name = "no_xattr", .has_arg = no_argument,
247 .flag = &opt.o_copy_xattrs },
248 { .val = 'P', .name = "pid-file", .has_arg = required_argument },
249 { .val = 'p', .name = "hsm-root", .has_arg = required_argument },
250 { .val = 'p', .name = "hsm_root", .has_arg = required_argument },
251 { .val = 'q', .name = "quiet", .has_arg = no_argument },
252 { .val = 'r', .name = "rebind", .has_arg = no_argument },
253 { .val = 'u', .name = "update-interval",
254 .has_arg = required_argument },
255 { .val = 'u', .name = "update_interval",
256 .has_arg = required_argument },
257 { .val = 'v', .name = "verbose", .has_arg = no_argument },
260 unsigned long long value;
261 unsigned long long unit;
268 opt.o_archive_id_cnt = LL_HSM_ORIGIN_MAX_ARCHIVE;
269 opt.o_archive_id = malloc(opt.o_archive_id_cnt *
270 sizeof(*opt.o_archive_id));
271 if (opt.o_archive_id == NULL)
274 while ((c = getopt_long(argc, argv, "A:b:c:f:hiMp:P:qru:v",
275 long_opts, NULL)) != -1) {
279 int val = strtol(optarg, &end, 10);
283 CT_ERROR(rc, "invalid archive-id: '%s'",
287 /* if archiveID is zero, any archiveID is accepted */
292 free(opt.o_archive_id);
293 opt.o_archive_id = NULL;
294 opt.o_archive_id_cnt = 0;
295 opt.o_archive_id_used = 0;
297 CT_WARN("archive-id = 0 is found, any backend will be served\n");
301 /* skip the duplicated id */
302 for (i = 0; i < opt.o_archive_id_used; i++) {
303 if (opt.o_archive_id[i] == val)
306 /* extend the space */
307 if (opt.o_archive_id_used >= opt.o_archive_id_cnt) {
310 opt.o_archive_id_cnt *= 2;
311 tmp = realloc(opt.o_archive_id,
312 sizeof(*opt.o_archive_id) *
313 opt.o_archive_id_cnt);
317 opt.o_archive_id = tmp;
320 opt.o_archive_id[opt.o_archive_id_used++] = val;
323 case 'b': /* -b and -c have both a number with unit as arg */
326 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
328 CT_ERROR(rc, "bad value for -%c '%s'", c,
333 opt.o_chunk_size = value;
335 opt.o_bandwidth = value;
338 opt.o_event_fifo = optarg;
343 opt.o_action = CA_IMPORT;
346 opt.o_action = CA_MAXSEQ;
349 opt.o_pid_file = optarg;
352 opt.o_hsm_root = optarg;
358 opt.o_action = CA_REBIND;
361 opt.o_report_int = atoi(optarg);
362 if (opt.o_report_int < 0) {
364 CT_ERROR(rc, "bad value for -%c '%s'", c,
379 switch (opt.o_action) {
381 /* src dst mount_point */
382 if (argc != optind + 3) {
384 CT_ERROR(rc, "--import requires 2 arguments");
387 opt.o_src = argv[optind++];
388 opt.o_dst = argv[optind++];
391 /* FID1 FID2 mount_point or FILE mount_point */
392 if (argc == optind + 2) {
393 opt.o_src = argv[optind++];
395 } else if (argc == optind + 3) {
396 opt.o_src = argv[optind++];
397 opt.o_dst = argv[optind++];
400 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
406 /* just mount point */
410 if (argc != optind + 1) {
412 CT_ERROR(rc, "no mount point specified");
416 opt.o_mnt = argv[optind];
419 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
420 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
422 if (opt.o_hsm_root == NULL) {
424 CT_ERROR(rc, "must specify a root directory for the backend");
428 if (opt.o_action == CA_IMPORT) {
429 if (opt.o_src && opt.o_src[0] == '/') {
432 "source path must be relative to HSM root");
436 if (opt.o_dst && opt.o_dst[0] != '/') {
438 CT_ERROR(rc, "destination path must be absolute");
447 static int ct_mkdir_p(const char *path)
460 while ((ptr = strchr(ptr, '/')) != NULL) {
462 rc = mkdir(saved, DIR_PERM);
464 if (rc < 0 && errno != EEXIST) {
466 CT_ERROR(rc, "cannot mkdir '%s'", path);
478 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
480 char lov_file[PATH_MAX + 8];
481 char lov_buf[XATTR_SIZE_MAX];
482 struct lov_user_md *lum;
487 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
488 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
490 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
492 if (xattr_size < 0) {
494 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
498 lum = (struct lov_user_md *)lov_buf;
500 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
501 lum->lmm_magic == LOV_USER_MAGIC_V3) {
502 /* Set stripe_offset to -1 so that it is not interpreted as a
503 * hint on restore. */
504 lum->lmm_stripe_offset = -1;
507 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
510 CT_ERROR(rc, "cannot open '%s'", lov_file);
514 rc = write(fd, lum, xattr_size);
517 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
518 xattr_size, lov_file);
526 CT_ERROR(rc, "cannot close '%s'", lov_file);
538 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
540 char lov_file[PATH_MAX + 4];
544 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
545 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
547 fd = open(lov_file, O_RDONLY);
549 CT_ERROR(errno, "cannot open '%s'", lov_file);
553 rc = read(fd, lovea, *lovea_size);
555 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
556 *lovea_size, lov_file);
567 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
568 const void *lovea, size_t lovea_size)
572 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
576 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
582 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
583 const char *dst, int src_fd, int dst_fd,
584 const struct hsm_action_item *hai, long hal_flags)
586 struct hsm_extent he;
587 __u64 offset = hai->hai_extent.offset;
591 __u64 write_total = 0;
592 __u64 length = hai->hai_extent.length;
593 time_t last_report_time;
595 double start_ct_now = ct_now();
596 /* Bandwidth Control */
599 time_t last_bw_print;
601 if (fstat(src_fd, &src_st) < 0) {
603 CT_ERROR(rc, "cannot stat '%s'", src);
607 if (!S_ISREG(src_st.st_mode)) {
609 CT_ERROR(rc, "'%s' is not a regular file", src);
613 if (hai->hai_extent.offset > (__u64)src_st.st_size) {
615 CT_ERROR(rc, "Trying to start reading past end (%ju > "
616 "%jd) of '%s' source file",
617 (uintmax_t)hai->hai_extent.offset,
618 (intmax_t)src_st.st_size, src);
622 if (fstat(dst_fd, &dst_st) < 0) {
624 CT_ERROR(rc, "cannot stat '%s'", dst);
628 if (!S_ISREG(dst_st.st_mode)) {
630 CT_ERROR(rc, "'%s' is not a regular file", dst);
634 /* Don't read beyond a given extent */
635 if (length > src_st.st_size - hai->hai_extent.offset)
636 length = src_st.st_size - hai->hai_extent.offset;
638 start_time = last_bw_print = last_report_time = time(NULL);
642 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
644 /* Action has been canceled or something wrong
645 * is happening. Stop copying data. */
646 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
653 buf = malloc(opt.o_chunk_size);
659 CT_TRACE("start copy of %ju bytes from '%s' to '%s'",
660 (uintmax_t)length, src, dst);
662 while (write_total < length) {
665 int chunk = (length - write_total > opt.o_chunk_size) ?
666 opt.o_chunk_size : length - write_total;
668 rsize = pread(src_fd, buf, chunk, offset);
675 CT_ERROR(rc, "cannot read from '%s'", src);
679 wsize = pwrite(dst_fd, buf, rsize, offset);
682 CT_ERROR(rc, "cannot write to '%s'", dst);
686 write_total += wsize;
690 /* sleep if needed, to honor bandwidth limits */
691 if (opt.o_bandwidth != 0) {
692 unsigned long long write_theory;
694 write_theory = (now - start_time) * opt.o_bandwidth;
696 if (write_theory < write_total) {
697 unsigned long long excess;
698 struct timespec delay;
700 excess = write_total - write_theory;
702 delay.tv_sec = excess / opt.o_bandwidth;
703 delay.tv_nsec = (excess % opt.o_bandwidth) *
704 NSEC_PER_SEC / opt.o_bandwidth;
706 if (now >= last_bw_print + opt.o_report_int) {
707 CT_TRACE("bandwith control: %lluB/s "
708 "excess=%llu sleep for "
710 (unsigned long long)opt.o_bandwidth,
711 (unsigned long long)excess,
712 (long long)delay.tv_sec,
718 rc = nanosleep(&delay, &delay);
719 } while (rc < 0 && errno == EINTR);
721 CT_ERROR(errno, "delay for bandwidth "
722 "control failed to sleep: "
723 "residual=%lld.%09lds",
724 (long long)delay.tv_sec,
732 if (now >= last_report_time + opt.o_report_int) {
733 last_report_time = now;
734 CT_TRACE("%%%ju ", (uintmax_t)(100 * write_total / length));
735 /* only give the length of the write since the last
737 he.length = offset - he.offset;
738 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
740 /* Action has been canceled or something wrong
741 * is happening. Stop copying data. */
742 CT_ERROR(rc, "progress ioctl for copy"
743 " '%s'->'%s' failed", src, dst);
753 * truncate restored file
754 * size is taken from the archive this is done to support
755 * restore after a force release which leaves the file with the
756 * wrong size (can big bigger than the new size)
758 if ((hai->hai_action == HSMA_RESTORE) &&
759 (src_st.st_size < dst_st.st_size)) {
761 * make sure the file is on disk before reporting success.
763 rc = ftruncate(dst_fd, src_st.st_size);
766 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
767 dst, (intmax_t)src_st.st_size);
775 CT_TRACE("copied %ju bytes in %f seconds",
776 (uintmax_t)length, ct_now() - start_ct_now);
781 /* Copy file attributes from file src to file dest */
782 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
786 struct timeval times[2];
789 if (fstat(src_fd, &st) < 0) {
791 CT_ERROR(rc, "cannot stat '%s'", src);
795 times[0].tv_sec = st.st_atime;
796 times[0].tv_usec = 0;
797 times[1].tv_sec = st.st_mtime;
798 times[1].tv_usec = 0;
799 if (fchmod(dst_fd, st.st_mode) < 0 ||
800 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
801 futimes(dst_fd, times) < 0) {
803 CT_ERROR(rc, "cannot set attributes of '%s'", src);
810 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
811 int dst_fd, bool is_restore)
813 char list[XATTR_LIST_MAX];
814 char value[XATTR_SIZE_MAX];
819 list_len = flistxattr(src_fd, list, sizeof(list));
824 while (name < list + list_len) {
825 rc = fgetxattr(src_fd, name, value, sizeof(value));
829 /* when we restore, we do not restore lustre xattr */
831 (strncmp(XATTR_TRUSTED_PREFIX, name,
832 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
833 rc = fsetxattr(dst_fd, name, value, rc, 0);
834 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
835 name, dst, rc, strerror(errno));
836 /* lustre.* attrs aren't supported on other FS's */
837 if (rc < 0 && errno != EOPNOTSUPP) {
839 CT_ERROR(rc, "cannot set extended attribute"
845 name += strlen(name) + 1;
851 static int ct_path_lustre(char *buf, int sz, const char *mnt,
852 const struct lu_fid *fid)
854 return scnprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
855 dot_lustre_name, PFID(fid));
858 static int ct_path_archive(char *buf, int sz, const char *archive_dir,
859 const struct lu_fid *fid)
861 return scnprintf(buf, sz, "%s/%04x/%04x/%04x/%04x/%04x/%04x/"
862 DFID_NOBRACE, archive_dir,
863 (fid)->f_oid & 0xFFFF,
864 (fid)->f_oid >> 16 & 0xFFFF,
865 (unsigned int)((fid)->f_seq & 0xFFFF),
866 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
867 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
868 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
872 static bool ct_is_retryable(int err)
874 return err == -ETIMEDOUT;
877 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
878 const struct hsm_action_item *hai,
879 int mdt_index, int open_flags)
884 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
887 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
888 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
894 static int ct_begin(struct hsm_copyaction_private **phcp,
895 const struct hsm_action_item *hai)
897 /* Restore takes specific parameters. Call the same function w/ default
898 * values for all other operations. */
899 return ct_begin_restore(phcp, hai, -1, 0);
902 static int ct_fini(struct hsm_copyaction_private **phcp,
903 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
905 struct hsm_copyaction_private *hcp;
909 CT_TRACE("Action completed, notifying coordinator "
910 "cookie=%#jx, FID="DFID", hp_flags=%d err=%d",
911 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid),
914 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
916 if (phcp == NULL || *phcp == NULL) {
917 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
919 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
926 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
927 if (rc == -ECANCELED)
928 CT_ERROR(rc, "completed action on '%s' has been canceled: "
929 "cookie=%#jx, FID="DFID, lstr,
930 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid));
932 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
934 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
940 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
942 struct hsm_copyaction_private *hcp = NULL;
944 char dst[PATH_MAX + 4] = "";
945 char root[PATH_MAX] = "";
948 bool rename_needed = false;
954 rc = ct_begin(&hcp, hai);
958 /* we fill archive so:
960 * destination = lustre FID
962 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
963 ct_path_archive(root, sizeof(root), opt.o_hsm_root, &hai->hai_fid);
964 if (hai->hai_extent.length == -1) {
965 /* whole file, write it to tmp location and atomically
966 * replace old archived file */
967 snprintf(dst, sizeof(dst), "%s_tmp", root);
968 /* we cannot rely on the same test because ct_copy_data()
969 * updates hai_extent.length */
970 rename_needed = true;
972 snprintf(dst, sizeof(dst), "%s", root);
975 CT_TRACE("archiving '%s' to '%s'", src, dst);
982 rc = ct_mkdir_p(dst);
984 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
988 src_fd = llapi_hsm_action_get_fd(hcp);
991 CT_ERROR(rc, "cannot open '%s' for read", src);
995 open_flags = O_WRONLY | O_NOFOLLOW;
996 /* If extent is specified, don't truncate an old archived copy */
997 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
999 dst_fd = open(dst, open_flags, FILE_PERM);
1002 CT_ERROR(rc, "cannot open '%s' for write", dst);
1006 /* saving stripe is not critical */
1007 rc = ct_save_stripe(src_fd, src, dst);
1009 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
1012 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1014 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
1021 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
1025 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
1027 /* attrs will remain on the MDS; no need to copy them, except possibly
1028 for disaster recovery */
1029 if (opt.o_copy_attrs) {
1030 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
1032 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
1036 CT_TRACE("attr file for '%s' saved to archive '%s'",
1040 /* xattrs will remain on the MDS; no need to copy them, except possibly
1041 for disaster recovery */
1042 if (opt.o_copy_xattrs) {
1043 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
1045 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
1047 rcf = rcf ? rcf : rc;
1049 CT_TRACE("xattr file for '%s' saved to archive '%s'",
1053 if (rename_needed == true) {
1054 char tmp_src[PATH_MAX + 8];
1055 char tmp_dst[PATH_MAX + 8];
1057 /* atomically replace old archived file */
1058 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
1060 rc = rename(dst, src);
1063 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
1066 /* rename lov file */
1067 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
1068 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
1069 rc = rename(tmp_dst, tmp_src);
1071 CT_ERROR(errno, "cannot rename '%s' to '%s'",
1075 if (opt.o_shadow_tree) {
1076 /* Create a namespace of softlinks that shadows the original
1077 * Lustre namespace. This will only be current at
1078 * time-of-archive (won't follow renames).
1079 * WARNING: release won't kill these links; a manual
1080 * cleanup of dead links would be required.
1083 long long recno = -1;
1089 sprintf(buf, DFID, PFID(&hai->hai_fid));
1090 sprintf(src, "%s/shadow/", opt.o_hsm_root);
1092 ptr = opt.o_hsm_root;
1094 (*ptr++ == '/') ? depth-- : 0;
1096 rc = llapi_fid2path(opt.o_mnt, buf, src + strlen(src),
1097 sizeof(src) - strlen(src), &recno, &linkno);
1099 CT_ERROR(rc, "cannot get FID of '%s'", buf);
1100 rcf = rcf ? rcf : rc;
1104 /* Figure out how many parent dirs to symlink back */
1107 (*ptr++ == '/') ? depth++ : 0;
1112 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1114 if (ct_mkdir_p(src)) {
1115 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1116 rcf = rcf ? rcf : -errno;
1119 /* symlink already exists ? */
1120 sz = readlink(src, buf, sizeof(buf));
1121 /* detect truncation */
1122 if (sz == sizeof(buf)) {
1123 rcf = rcf ? rcf : -E2BIG;
1124 CT_ERROR(rcf, "readlink '%s' truncated", src);
1129 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1130 if (unlink(src) && errno != ENOENT) {
1132 "cannot unlink symlink '%s'",
1134 rcf = rcf ? rcf : -errno;
1137 /* unlink old symlink done */
1138 CT_TRACE("remove old symlink '%s' pointing"
1139 " to '%s'", src, buf);
1141 /* symlink already ok */
1142 CT_TRACE("symlink '%s' already pointing"
1143 " to '%s'", src, dst);
1148 if (symlink(dst, src)) {
1149 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1151 rcf = rcf ? rcf : -errno;
1154 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1166 if (ct_is_retryable(rc))
1167 hp_flags |= HP_FLAG_RETRY;
1178 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1183 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1185 struct hsm_copyaction_private *hcp = NULL;
1188 char lov_buf[XATTR_SIZE_MAX];
1189 size_t lov_size = sizeof(lov_buf);
1198 /* we fill lustre so:
1199 * source = lustre FID in the backend
1200 * destination = data FID = volatile file
1203 /* build backend file name from released file FID */
1204 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1206 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1209 CT_ERROR(rc, "cannot get mdt index "DFID"",
1210 PFID(&hai->hai_fid));
1213 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1214 * dependency on the structure format. */
1215 rc = ct_load_stripe(src, lov_buf, &lov_size);
1217 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1218 src, strerror(-rc));
1221 open_flags |= O_LOV_DELAY_CREATE;
1225 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1229 /* get the FID of the volatile file */
1230 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1232 CT_ERROR(rc, "restoring "DFID
1233 ", cannot get FID of created volatile file",
1234 PFID(&hai->hai_fid));
1238 /* build volatile "file name", for messages */
1239 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1241 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1243 if (opt.o_dry_run) {
1248 src_fd = open(src, O_RDONLY | O_NOATIME | O_NOFOLLOW);
1251 CT_ERROR(rc, "cannot open '%s' for read", src);
1255 dst_fd = llapi_hsm_action_get_fd(hcp);
1258 CT_ERROR(rc, "cannot open '%s' for write", dst);
1263 /* the layout cannot be allocated through .fid so we have to
1264 * restore a layout */
1265 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1267 CT_ERROR(rc, "cannot restore file striping info"
1268 " for '%s' from '%s'", dst, src);
1274 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1276 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1279 if (ct_is_retryable(rc))
1280 hp_flags |= HP_FLAG_RETRY;
1284 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1287 rc = ct_fini(&hcp, hai, hp_flags, rc);
1289 /* object swaping is done by cdt at copy end, so close of volatile file
1290 * cannot be done before */
1300 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1302 struct hsm_copyaction_private *hcp = NULL;
1303 char dst[PATH_MAX], attr[PATH_MAX + 4];
1306 rc = ct_begin(&hcp, hai);
1310 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1312 CT_TRACE("removing file '%s'", dst);
1314 if (opt.o_dry_run) {
1322 CT_ERROR(rc, "cannot unlink '%s'", dst);
1327 snprintf(attr, sizeof(attr), "%s.lov", dst);
1331 CT_ERROR(rc, "cannot unlink '%s'", attr);
1334 /* ignore the error when lov file does not exist. */
1342 rc = ct_fini(&hcp, hai, 0, rc);
1347 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1351 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1352 /* Print the original path */
1354 char path[PATH_MAX];
1355 long long recno = -1;
1358 sprintf(fid, DFID, PFID(&hai->hai_fid));
1359 CT_TRACE("'%s' action %s reclen %d, cookie=%#jx",
1360 fid, hsm_copytool_action2name(hai->hai_action),
1361 hai->hai_len, (uintmax_t)hai->hai_cookie);
1362 rc = llapi_fid2path(opt.o_mnt, fid, path,
1363 sizeof(path), &recno, &linkno);
1365 CT_ERROR(rc, "cannot get path of FID %s", fid);
1367 CT_TRACE("processing file '%s'", path);
1370 switch (hai->hai_action) {
1371 /* set err_major, minor inside these functions */
1373 rc = ct_archive(hai, hal_flags);
1376 rc = ct_restore(hai, hal_flags);
1379 rc = ct_remove(hai, hal_flags);
1382 CT_TRACE("cancel not implemented for file system '%s'",
1384 /* Don't report progress to coordinator for this cookie:
1385 * the copy function will get ECANCELED when reporting
1392 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1395 ct_fini(NULL, hai, 0, rc);
1403 struct hsm_action_item *hai;
1406 static void *ct_thread(void *data)
1408 struct ct_th_data *cttd = data;
1411 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1415 pthread_exit((void *)(intptr_t)rc);
1418 static int ct_process_item_async(const struct hsm_action_item *hai,
1421 pthread_attr_t attr;
1423 struct ct_th_data *data;
1426 data = malloc(sizeof(*data));
1430 data->hai = malloc(hai->hai_len);
1431 if (data->hai == NULL) {
1436 memcpy(data->hai, hai, hai->hai_len);
1437 data->hal_flags = hal_flags;
1439 rc = pthread_attr_init(&attr);
1441 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1448 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1450 rc = pthread_create(&thread, &attr, ct_thread, data);
1452 CT_ERROR(rc, "cannot create thread for '%s' service",
1455 pthread_attr_destroy(&attr);
1459 static int ct_import_one(const char *src, const char *dst)
1461 char newarc[PATH_MAX];
1466 CT_TRACE("importing '%s' from '%s'", dst, src);
1468 if (stat(src, &st) < 0) {
1470 CT_ERROR(rc, "cannot stat '%s'", src);
1477 rc = llapi_hsm_import(dst,
1478 opt.o_archive_id_used ?
1479 opt.o_archive_id[0] : 0,
1480 &st, 0, 0, 0, 0, NULL, &fid);
1482 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1486 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1488 rc = ct_mkdir_p(newarc);
1490 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1496 /* Lots of choices now: mv, ln, ln -s ? */
1497 rc = link(src, newarc); /* hardlink */
1500 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1504 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1509 static char *path_concat(const char *dirname, const char *basename)
1514 rc = asprintf(&result, "%s/%s", dirname, basename);
1521 static int ct_import_fid(const struct lu_fid *import_fid)
1523 char fid_path[PATH_MAX];
1526 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1527 rc = access(fid_path, F_OK);
1528 if (rc == 0 || errno != ENOENT) {
1529 rc = (errno == 0) ? -EEXIST : -errno;
1530 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1534 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1537 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1539 return ct_import_one(fid_path, opt.o_dst);
1542 static int ct_import_recurse(const char *relpath)
1546 char *srcpath, *newpath;
1547 struct lu_fid import_fid;
1550 if (relpath == NULL)
1553 /* Is relpath a FID? */
1554 rc = llapi_fid_parse(relpath, &import_fid, NULL);
1556 return ct_import_fid(&import_fid);
1558 srcpath = path_concat(opt.o_hsm_root, relpath);
1559 if (srcpath == NULL) {
1564 dir = opendir(srcpath);
1566 /* Not a dir, or error */
1567 if (errno == ENOTDIR) {
1568 /* Single regular file case, treat o_dst as absolute
1570 rc = ct_import_one(srcpath, opt.o_dst);
1573 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1581 while ((ent = readdir(dir)) != NULL) {
1582 if (!strcmp(ent->d_name, ".") ||
1583 !strcmp(ent->d_name, ".."))
1586 /* New relative path */
1587 newpath = path_concat(relpath, ent->d_name);
1588 if (newpath == NULL) {
1594 if (ent->d_type == DT_DIR) {
1595 rc = ct_import_recurse(newpath);
1600 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1601 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1602 /* Make the target dir in the Lustre fs */
1603 rc = ct_mkdir_p(dst);
1605 /* Import the file */
1606 rc = ct_import_one(src, dst);
1608 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1614 CT_ERROR(rc, "cannot import '%s'", newpath);
1615 if (err_major && opt.o_abort_on_error) {
1629 static int ct_rebind_one(const struct lu_fid *old_fid,
1630 const struct lu_fid *new_fid)
1636 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1638 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1639 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1641 if (!opt.o_dry_run) {
1642 char src_attr[PATH_MAX + 4];
1643 char dst_attr[PATH_MAX + 4];
1646 if (rename(src, dst)) {
1648 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1651 /* rename lov file */
1652 snprintf(src_attr, sizeof(src_attr), "%s.lov", src);
1653 snprintf(dst_attr, sizeof(dst_attr), "%s.lov", dst);
1654 if (rename(src_attr, dst_attr))
1655 CT_ERROR(errno, "cannot rename '%s' to '%s'",
1656 src_attr, dst_attr);
1662 static bool fid_is_file(struct lu_fid *fid)
1664 return fid_is_norm(fid) || fid_is_igif(fid);
1667 static bool should_ignore_line(const char *line)
1671 for (i = 0; line[i] != '\0'; i++) {
1672 if (isspace(line[i]))
1674 else if (line[i] == '#')
1683 static int ct_rebind_list(const char *list)
1689 size_t line_size = 0;
1690 unsigned int nl = 0;
1691 unsigned int ok = 0;
1693 filp = fopen(list, "r");
1696 CT_ERROR(rc, "cannot open '%s'", list);
1700 /* each line consists of 2 FID */
1701 while ((r = getline(&line, &line_size, filp)) != -1) {
1702 struct lu_fid old_fid;
1703 struct lu_fid new_fid;
1706 /* Ignore empty and commented out ('#...') lines. */
1707 if (should_ignore_line(line))
1712 rc = llapi_fid_parse(line, &old_fid, &next_fid);
1715 rc = llapi_fid_parse(next_fid, &new_fid, NULL);
1718 if (!fid_is_file(&old_fid) || !fid_is_file(&new_fid))
1721 error: CT_ERROR(rc, "%s:%u: two FIDs expected in '%s'",
1727 if (ct_rebind_one(&old_fid, &new_fid))
1738 /* return 0 if all rebinds were successful */
1739 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1741 return ok == nl ? 0 : -1;
1744 static int ct_rebind(void)
1749 struct lu_fid old_fid;
1750 struct lu_fid new_fid;
1752 rc = llapi_fid_parse(opt.o_src, &old_fid, NULL);
1753 if (!rc && !fid_is_file(&old_fid))
1756 CT_ERROR(rc, "invalid source FID '%s'", opt.o_src);
1760 rc = llapi_fid_parse(opt.o_dst, &new_fid, NULL);
1761 if (!rc && !fid_is_file(&new_fid))
1764 CT_ERROR(rc, "invalid destination FID '%s'", opt.o_dst);
1768 rc = ct_rebind_one(&old_fid, &new_fid);
1773 /* o_src is a list file */
1774 rc = ct_rebind_list(opt.o_src);
1779 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
1788 dir = opendir(dirpath);
1791 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
1799 /* end of directory.
1800 * rc is 0 and seqmax contains the max value. */
1803 CT_ERROR(rc, "cannot readdir '%s'", dirpath);
1807 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
1810 if (sscanf(ent->d_name, "%hx", &sub_seq) != 1) {
1811 CT_TRACE("'%s' has an unexpected dirname format, "
1812 "skip entry", ent->d_name);
1815 if (sub_seq > *sub_seqmax)
1816 *sub_seqmax = sub_seq;
1823 static int ct_max_sequence(void)
1826 char path[PATH_MAX];
1830 snprintf(path, sizeof(path), "%s", opt.o_hsm_root);
1831 /* FID sequence is stored in top-level directory names:
1832 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
1834 for (i = 0; i < 4; i++) {
1837 rc = ct_dir_level_max(path, &subseq);
1840 seq |= ((__u64)subseq << ((3 - i) * 16));
1841 path_len = strlen(path);
1842 rc = snprintf(path + path_len, sizeof(path) - path_len,
1844 if (rc >= (sizeof(path) - path_len))
1846 path[sizeof(path) - 1] = '\0';
1849 printf("max_sequence: %#jx\n", (uintmax_t)seq);
1854 static void handler(int signal)
1856 psignal(signal, "exiting");
1857 /* If we don't clean up upon interrupt, umount thinks there's a ref
1858 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
1859 * does successfully unmount and the mount is actually gone, but the
1860 * mtab entry remains. So this just makes mtab happier. */
1861 llapi_hsm_copytool_unregister(&ctdata);
1863 /* Also remove fifo upon signal as during normal/error exit */
1864 if (opt.o_event_fifo != NULL)
1865 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1869 /* Daemon waits for messages from the kernel; run it in the background. */
1870 static int ct_run(void)
1872 struct sigaction cleanup_sigaction;
1875 if (opt.o_daemonize) {
1879 CT_ERROR(rc, "cannot daemonize");
1884 if (opt.o_pid_file != NULL) {
1885 pid_file_fd = create_pid_file(opt.o_pid_file);
1886 if (pid_file_fd < 0) {
1888 CT_ERROR(rc, "cannot create PID file");
1893 setbuf(stdout, NULL);
1895 if (opt.o_event_fifo != NULL) {
1896 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
1898 CT_ERROR(rc, "failed to register event fifo");
1901 llapi_error_callback_set(llapi_hsm_log_error);
1904 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
1905 opt.o_archive_id_used,
1906 opt.o_archive_id, 0);
1908 CT_ERROR(rc, "cannot start copytool interface");
1912 memset(&cleanup_sigaction, 0, sizeof(cleanup_sigaction));
1913 cleanup_sigaction.sa_handler = handler;
1914 sigemptyset(&cleanup_sigaction.sa_mask);
1915 sigaction(SIGINT, &cleanup_sigaction, NULL);
1916 sigaction(SIGTERM, &cleanup_sigaction, NULL);
1919 struct hsm_action_list *hal;
1920 struct hsm_action_item *hai;
1924 CT_TRACE("waiting for message from kernel");
1926 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
1927 if (rc == -ESHUTDOWN) {
1928 CT_TRACE("shutting down");
1930 } else if (rc < 0) {
1931 CT_WARN("cannot receive action list: %s",
1934 if (opt.o_abort_on_error)
1940 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
1941 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
1943 if (strcmp(hal->hal_fsname, fs_name) != 0) {
1945 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
1946 hal->hal_fsname, fs_name);
1948 if (opt.o_abort_on_error)
1954 hai = hai_first(hal);
1955 while (++i <= hal->hal_count) {
1956 if ((char *)hai - (char *)hal > msgsize) {
1959 "'%s' item %d past end of message!",
1964 rc = ct_process_item_async(hai, hal->hal_flags);
1966 CT_ERROR(rc, "'%s' item %d process",
1968 if (opt.o_abort_on_error && err_major)
1970 hai = hai_next(hai);
1973 if (opt.o_abort_on_error && err_major)
1977 llapi_hsm_copytool_unregister(&ctdata);
1978 if (opt.o_event_fifo != NULL)
1979 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
1984 static int ct_setup(void)
1988 /* set llapi message level */
1989 llapi_msg_set_level(opt.o_verbose);
1991 arc_fd = open(opt.o_hsm_root, O_RDONLY);
1994 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
1998 rc = llapi_search_fsname(opt.o_mnt, fs_name);
2000 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
2005 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
2006 if (opt.o_mnt_fd < 0) {
2008 CT_ERROR(rc, "cannot open mount point at '%s'",
2016 static int ct_cleanup(void)
2020 if (opt.o_mnt_fd >= 0) {
2021 rc = close(opt.o_mnt_fd);
2024 CT_ERROR(rc, "cannot close mount point");
2033 CT_ERROR(rc, "cannot close archive root directory");
2038 if (opt.o_archive_id_cnt > 0) {
2039 free(opt.o_archive_id);
2040 opt.o_archive_id = NULL;
2041 opt.o_archive_id_cnt = 0;
2047 int main(int argc, char **argv)
2051 snprintf(cmd_name, sizeof(cmd_name), "%s", basename(argv[0]));
2052 rc = ct_parseopts(argc, argv);
2054 CT_WARN("try '%s --help' for more information", cmd_name);
2062 switch (opt.o_action) {
2064 rc = ct_import_recurse(opt.o_src);
2070 rc = ct_max_sequence();
2077 if (opt.o_action != CA_MAXSEQ)
2078 CT_TRACE("process finished, errs: %d major, %d minor,"
2079 " rc=%d (%s)", err_major, err_minor, rc,