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 <lnetconfig/cyaml.h>
62 #include <lustre/lustreapi.h>
66 /* Progress reporting period */
67 #define REPORT_INTERVAL_DEFAULT 30
68 /* HSM hash subdir permissions */
69 #define DIR_PERM S_IRWXU
70 /* HSM hash file permissions */
71 #define FILE_PERM (S_IRUSR | S_IWUSR)
73 #define ONE_MB 0x100000
76 # define NSEC_PER_SEC 1000000000UL
86 enum ct_archive_format {
87 /* v1 (original) using 6 directories (oid & 0xffff)/-/-/-/-/-/FID.
88 * Places only one FID per directory. See ct_path_archive() below. */
89 CT_ARCHIVE_FORMAT_V1 = 1,
90 /* v2 using 1 directory (oid & 0xffff)/FID. */
91 CT_ARCHIVE_FORMAT_V2 = 2,
94 static const char *ct_archive_format_name[] = {
95 [CT_ARCHIVE_FORMAT_V1] = "v1",
96 [CT_ARCHIVE_FORMAT_V2] = "v2",
100 ct_str_to_archive_format(const char *str, enum ct_archive_format *pctaf)
102 enum ct_archive_format ctaf;
104 for (ctaf = 0; ctaf < ARRAY_SIZE(ct_archive_format_name); ctaf++) {
105 if (ct_archive_format_name[ctaf] != NULL &&
106 strcmp(ct_archive_format_name[ctaf], str) == 0) {
115 static const char *ct_archive_format_to_str(enum ct_archive_format ctaf)
118 ctaf < ARRAY_SIZE(ct_archive_format_name) &&
119 ct_archive_format_name[ctaf] != NULL)
120 return ct_archive_format_name[ctaf];
129 int o_abort_on_error;
133 const char *o_config_path;
134 enum ct_archive_format o_archive_format;
135 int o_archive_id_used;
136 int o_archive_id_cnt;
139 unsigned long long o_bandwidth;
141 enum ct_action o_action;
146 char *o_src; /* for import, or rebind */
147 char *o_dst; /* for import, or rebind */
151 /* everything else is zeroed */
152 struct options opt = {
155 .o_verbose = LLAPI_MSG_INFO,
157 .o_archive_format = CT_ARCHIVE_FORMAT_V1,
158 .o_report_int = REPORT_INTERVAL_DEFAULT,
159 .o_chunk_size = ONE_MB,
162 /* hsm_copytool_private will hold an open FD on the lustre mount point
163 * for us. Additionally open one on the archive FS root to make sure
164 * it doesn't drop out from under us (and remind the admin to shutdown
165 * the copytool before unmounting). */
167 static int arc_fd = -1;
169 static int err_major;
170 static int err_minor;
172 static char cmd_name[PATH_MAX];
173 static char fs_name[MAX_OBD_NAME + 1];
174 static int pid_file_fd = -1;
176 static struct hsm_copytool_private *ctdata;
178 static inline double ct_now(void)
182 gettimeofday(&tv, NULL);
184 return tv.tv_sec + 0.000001 * tv.tv_usec;
187 #define CT_ERROR(_rc, _format, ...) \
188 llapi_error(LLAPI_MSG_ERROR, _rc, \
189 "%f %s[%ld]: "_format, \
190 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
192 #define CT_DEBUG(_format, ...) \
193 llapi_error(LLAPI_MSG_DEBUG | LLAPI_MSG_NO_ERRNO, 0, \
194 "%f %s[%ld]: "_format, \
195 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
197 #define CT_WARN(_format, ...) \
198 llapi_error(LLAPI_MSG_WARN | LLAPI_MSG_NO_ERRNO, 0, \
199 "%f %s[%ld]: "_format, \
200 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
202 #define CT_TRACE(_format, ...) \
203 llapi_error(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, 0, \
204 "%f %s[%ld]: "_format, \
205 ct_now(), cmd_name, syscall(SYS_gettid), ## __VA_ARGS__)
207 static void usage(const char *name, int rc)
210 " Usage: %s [options]... <mode> <lustre_mount_point>\n"
211 "The Lustre HSM Posix copy tool can be used as a daemon or "
212 "as a command line tool\n"
213 "The Lustre HSM daemon acts on action requests from Lustre\n"
214 "to copy files to and from an HSM archive system.\n"
215 "This POSIX-flavored daemon makes regular POSIX filesystem calls\n"
216 "to an HSM mounted at a given hsm_root.\n"
217 " --daemon Daemon mode, run in background\n"
219 " --no-attr Don't copy file attributes\n"
220 " --no-shadow Don't create shadow namespace in archive\n"
221 " --no-xattr Don't copy file extended attributes\n"
222 "The Lustre HSM tool performs administrator-type actions\n"
223 "on a Lustre HSM archive.\n"
224 "This POSIX-flavored tool can link an existing HSM namespace\n"
225 "into a Lustre filesystem.\n"
227 " %s [options] --import <src> <dst> <lustre_mount_point>\n"
228 " import an archived subtree from\n"
229 " <src> (FID or relative path to hsm_root) into the Lustre\n"
231 " <dst> (absolute path)\n"
232 " %s [options] --rebind <old_FID> <new_FID> <lustre_mount_point>\n"
233 " rebind an entry in the HSM to a new FID\n"
234 " <old_FID> old FID the HSM entry is bound to\n"
235 " <new_FID> new FID to bind the HSM entry to\n"
236 " %s [options] --rebind <list_file> <lustre_mount_point>\n"
237 " perform the rebind operation for all FID in the list file\n"
238 " each line of <list_file> consists of <old_FID> <new_FID>\n"
239 " %s [options] --max-sequence <fsname>\n"
240 " return the max fid sequence of archived files\n"
241 " %s [options] --archive-upgrade=VER\n"
242 " Upgrade or downgrade the archive to version VER\n"
244 " --abort-on-error Abort operation on major error\n"
245 " -A, --archive <#> Archive number (repeatable)\n"
246 " -C, --config=PATH Read config from PATH\n"
247 " -F, --archive-format=VER Use archive format VER\n"
248 " -b, --bandwidth <bw> Limit I/O bandwidth (unit can be used\n,"
250 " --dry-run Don't run, just show what would be done\n"
251 " -c, --chunk-size <sz> I/O size used during data copy\n"
252 " (unit can be used, default is MB)\n"
253 " -f, --event-fifo <path> Write events stream to fifo\n"
254 " -P, --pid-file=PATH Lock and write PID to PATH\n"
255 " -p, --hsm-root <path> Target HSM mount point\n"
256 " -q, --quiet Produce less verbose output\n"
257 " -u, --update-interval <s> Interval between progress reports sent\n"
259 " -v, --verbose Produce more verbose output\n",
260 cmd_name, cmd_name, cmd_name, cmd_name, cmd_name, cmd_name);
265 static int ct_parseopts(int argc, char * const *argv)
267 struct option long_opts[] = {
268 { .val = 1, .name = "abort-on-error",
269 .flag = &opt.o_abort_on_error, .has_arg = no_argument },
270 { .val = 1, .name = "abort_on_error",
271 .flag = &opt.o_abort_on_error, .has_arg = no_argument },
272 { .val = 'A', .name = "archive", .has_arg = required_argument },
273 { .val = 'b', .name = "bandwidth", .has_arg = required_argument },
274 { .val = 'C', .name = "config", .has_arg = required_argument },
275 { .val = 'c', .name = "chunk-size", .has_arg = required_argument },
276 { .val = 'c', .name = "chunk_size", .has_arg = required_argument },
277 { .val = 1, .name = "daemon", .has_arg = no_argument,
278 .flag = &opt.o_daemonize },
279 { .val = 'F', .name = "archive-format", .has_arg = required_argument },
280 { .val = 'U', .name = "archive-upgrade", .has_arg = required_argument },
281 { .val = 'f', .name = "event-fifo", .has_arg = required_argument },
282 { .val = 'f', .name = "event_fifo", .has_arg = required_argument },
283 { .val = 1, .name = "dry-run", .has_arg = no_argument,
284 .flag = &opt.o_dry_run },
285 { .val = 'h', .name = "help", .has_arg = no_argument },
286 { .val = 'i', .name = "import", .has_arg = no_argument },
287 { .val = 'M', .name = "max-sequence", .has_arg = no_argument },
288 { .val = 'M', .name = "max_sequence", .has_arg = no_argument },
289 { .val = 0, .name = "no-attr", .has_arg = no_argument,
290 .flag = &opt.o_copy_attrs },
291 { .val = 0, .name = "no_attr", .has_arg = no_argument,
292 .flag = &opt.o_copy_attrs },
293 { .val = 0, .name = "no-shadow", .has_arg = no_argument,
294 .flag = &opt.o_shadow_tree },
295 { .val = 0, .name = "no_shadow", .has_arg = no_argument,
296 .flag = &opt.o_shadow_tree },
297 { .val = 0, .name = "no-xattr", .has_arg = no_argument,
298 .flag = &opt.o_copy_xattrs },
299 { .val = 0, .name = "no_xattr", .has_arg = no_argument,
300 .flag = &opt.o_copy_xattrs },
301 { .val = 'P', .name = "pid-file", .has_arg = required_argument },
302 { .val = 'p', .name = "hsm-root", .has_arg = required_argument },
303 { .val = 'p', .name = "hsm_root", .has_arg = required_argument },
304 { .val = 'q', .name = "quiet", .has_arg = no_argument },
305 { .val = 'r', .name = "rebind", .has_arg = no_argument },
306 { .val = 'u', .name = "update-interval",
307 .has_arg = required_argument },
308 { .val = 'u', .name = "update_interval",
309 .has_arg = required_argument },
310 { .val = 'v', .name = "verbose", .has_arg = no_argument },
313 unsigned long long value;
314 unsigned long long unit;
321 opt.o_archive_id_cnt = LL_HSM_ORIGIN_MAX_ARCHIVE;
322 opt.o_archive_id = malloc(opt.o_archive_id_cnt *
323 sizeof(*opt.o_archive_id));
324 if (opt.o_archive_id == NULL)
327 while ((c = getopt_long(argc, argv, "A:b:C:c:F:f:hiMp:P:qrU:u:v",
328 long_opts, NULL)) != -1) {
332 int val = strtol(optarg, &end, 10);
336 CT_ERROR(rc, "invalid archive-id: '%s'",
340 /* if archiveID is zero, any archiveID is accepted */
345 free(opt.o_archive_id);
346 opt.o_archive_id = NULL;
347 opt.o_archive_id_cnt = 0;
348 opt.o_archive_id_used = 0;
350 CT_WARN("archive-id = 0 is found, any backend will be served\n");
354 /* skip the duplicated id */
355 for (i = 0; i < opt.o_archive_id_used; i++) {
356 if (opt.o_archive_id[i] == val)
359 /* extend the space */
360 if (opt.o_archive_id_used >= opt.o_archive_id_cnt) {
363 opt.o_archive_id_cnt *= 2;
364 tmp = realloc(opt.o_archive_id,
365 sizeof(*opt.o_archive_id) *
366 opt.o_archive_id_cnt);
370 opt.o_archive_id = tmp;
373 opt.o_archive_id[opt.o_archive_id_used++] = val;
376 case 'b': /* -b and -c have both a number with unit as arg */
379 if (llapi_parse_size(optarg, &value, &unit, 0) < 0) {
381 CT_ERROR(rc, "bad value for -%c '%s'", c,
386 opt.o_chunk_size = value;
388 opt.o_bandwidth = value;
391 opt.o_config_path = optarg;
394 rc = ct_str_to_archive_format(optarg, &opt.o_archive_format);
396 CT_ERROR(rc, "invalid archive format '%s'", optarg);
401 rc = ct_str_to_archive_format(optarg, &opt.o_archive_format);
403 CT_ERROR(rc, "invalid archive format '%s'", optarg);
406 opt.o_action = CA_ARCHIVE_UPGRADE;
410 opt.o_event_fifo = optarg;
415 opt.o_action = CA_IMPORT;
418 opt.o_action = CA_MAXSEQ;
421 opt.o_pid_file = optarg;
424 opt.o_hsm_root = optarg;
430 opt.o_action = CA_REBIND;
433 opt.o_report_int = atoi(optarg);
434 if (opt.o_report_int < 0) {
436 CT_ERROR(rc, "bad value for -%c '%s'", c,
451 switch (opt.o_action) {
453 /* src dst mount_point */
454 if (argc != optind + 3) {
456 CT_ERROR(rc, "--import requires 2 arguments");
459 opt.o_src = argv[optind++];
460 opt.o_dst = argv[optind++];
463 /* FID1 FID2 mount_point or FILE mount_point */
464 if (argc == optind + 2) {
465 opt.o_src = argv[optind++];
467 } else if (argc == optind + 3) {
468 opt.o_src = argv[optind++];
469 opt.o_dst = argv[optind++];
472 CT_ERROR(rc, "--rebind requires 1 or 2 arguments");
478 /* just mount point */
482 if (opt.o_action == CA_ARCHIVE_UPGRADE)
485 if (argc != optind + 1) {
487 CT_ERROR(rc, "no mount point specified");
491 opt.o_mnt = argv[optind];
494 CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
495 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
497 if (opt.o_action == CA_IMPORT) {
498 if (opt.o_src && opt.o_src[0] == '/') {
501 "source path must be relative to HSM root");
505 if (opt.o_dst && opt.o_dst[0] != '/') {
507 CT_ERROR(rc, "destination path must be absolute");
515 static int ct_mkdirat_p(int fd, char *path, mode_t mode)
520 if (strlen(path) == 0 || strcmp(path, ".") == 0)
523 rc = mkdirat(fd, path, mode);
524 if (rc == 0 || errno == EEXIST)
530 split = strrchr(path, '/');
535 rc = ct_mkdirat_p(fd, path, mode);
540 rc = mkdirat(fd, path, mode);
541 if (rc == 0 || errno == EEXIST)
547 /* XXX Despite the name, this is 'mkdir -p $(dirname path)' */
548 static int ct_mkdir_p(const char *path)
554 path2 = strdup(path);
558 split = strrchr(path2, '/');
566 rc = ct_mkdirat_p(AT_FDCWD, path2, DIR_PERM);
573 static int ct_save_stripe(int src_fd, const char *src, const char *dst)
575 char lov_file[PATH_MAX + 8];
576 char lov_buf[XATTR_SIZE_MAX];
577 struct lov_user_md *lum;
582 snprintf(lov_file, sizeof(lov_file), "%s.lov", dst);
583 CT_TRACE("saving stripe info of '%s' in %s", src, lov_file);
585 xattr_size = fgetxattr(src_fd, XATTR_LUSTRE_LOV, lov_buf,
587 if (xattr_size < 0) {
589 CT_ERROR(rc, "cannot get stripe info on '%s'", src);
593 lum = (struct lov_user_md *)lov_buf;
595 if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
596 lum->lmm_magic == LOV_USER_MAGIC_V3) {
597 /* Set stripe_offset to -1 so that it is not interpreted as a
598 * hint on restore. */
599 lum->lmm_stripe_offset = -1;
602 fd = open(lov_file, O_TRUNC | O_CREAT | O_WRONLY, FILE_PERM);
605 CT_ERROR(rc, "cannot open '%s'", lov_file);
609 rc = write(fd, lum, xattr_size);
612 CT_ERROR(rc, "cannot write %zd bytes to '%s'",
613 xattr_size, lov_file);
621 CT_ERROR(rc, "cannot close '%s'", lov_file);
633 static int ct_load_stripe(const char *src, void *lovea, size_t *lovea_size)
635 char lov_file[PATH_MAX + 4];
639 snprintf(lov_file, sizeof(lov_file), "%s.lov", src);
640 CT_TRACE("reading stripe rules from '%s' for '%s'", lov_file, src);
642 fd = open(lov_file, O_RDONLY);
644 CT_ERROR(errno, "cannot open '%s'", lov_file);
648 rc = read(fd, lovea, *lovea_size);
650 CT_ERROR(errno, "cannot read %zu bytes from '%s'",
651 *lovea_size, lov_file);
662 static int ct_restore_stripe(const char *src, const char *dst, int dst_fd,
663 const void *lovea, size_t lovea_size)
667 rc = fsetxattr(dst_fd, XATTR_LUSTRE_LOV, lovea, lovea_size,
671 CT_ERROR(rc, "cannot set lov EA on '%s'", dst);
677 static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
678 const char *dst, int src_fd, int dst_fd,
679 const struct hsm_action_item *hai, long hal_flags)
681 struct hsm_extent he;
682 __u64 offset = hai->hai_extent.offset;
686 __u64 write_total = 0;
687 __u64 length = hai->hai_extent.length;
688 time_t last_report_time;
690 double start_ct_now = ct_now();
691 /* Bandwidth Control */
694 time_t last_bw_print;
696 if (fstat(src_fd, &src_st) < 0) {
698 CT_ERROR(rc, "cannot stat '%s'", src);
702 if (!S_ISREG(src_st.st_mode)) {
704 CT_ERROR(rc, "'%s' is not a regular file", src);
708 if (hai->hai_extent.offset > (__u64)src_st.st_size) {
710 CT_ERROR(rc, "Trying to start reading past end (%ju > "
711 "%jd) of '%s' source file",
712 (uintmax_t)hai->hai_extent.offset,
713 (intmax_t)src_st.st_size, src);
717 if (fstat(dst_fd, &dst_st) < 0) {
719 CT_ERROR(rc, "cannot stat '%s'", dst);
723 if (!S_ISREG(dst_st.st_mode)) {
725 CT_ERROR(rc, "'%s' is not a regular file", dst);
729 /* Don't read beyond a given extent */
730 if (length > src_st.st_size - hai->hai_extent.offset)
731 length = src_st.st_size - hai->hai_extent.offset;
733 start_time = last_bw_print = last_report_time = time(NULL);
737 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
739 /* Action has been canceled or something wrong
740 * is happening. Stop copying data. */
741 CT_ERROR(rc, "progress ioctl for copy '%s'->'%s' failed",
748 buf = malloc(opt.o_chunk_size);
754 CT_TRACE("start copy of %ju bytes from '%s' to '%s'",
755 (uintmax_t)length, src, dst);
757 while (write_total < length) {
760 int chunk = (length - write_total > opt.o_chunk_size) ?
761 opt.o_chunk_size : length - write_total;
763 rsize = pread(src_fd, buf, chunk, offset);
770 CT_ERROR(rc, "cannot read from '%s'", src);
774 wsize = pwrite(dst_fd, buf, rsize, offset);
777 CT_ERROR(rc, "cannot write to '%s'", dst);
781 write_total += wsize;
785 /* sleep if needed, to honor bandwidth limits */
786 if (opt.o_bandwidth != 0) {
787 unsigned long long write_theory;
789 write_theory = (now - start_time) * opt.o_bandwidth;
791 if (write_theory < write_total) {
792 unsigned long long excess;
793 struct timespec delay;
795 excess = write_total - write_theory;
797 delay.tv_sec = excess / opt.o_bandwidth;
798 delay.tv_nsec = (excess % opt.o_bandwidth) *
799 NSEC_PER_SEC / opt.o_bandwidth;
801 if (now >= last_bw_print + opt.o_report_int) {
802 CT_TRACE("bandwith control: %lluB/s "
803 "excess=%llu sleep for "
805 (unsigned long long)opt.o_bandwidth,
806 (unsigned long long)excess,
807 (long long)delay.tv_sec,
813 rc = nanosleep(&delay, &delay);
814 } while (rc < 0 && errno == EINTR);
816 CT_ERROR(errno, "delay for bandwidth "
817 "control failed to sleep: "
818 "residual=%lld.%09lds",
819 (long long)delay.tv_sec,
827 if (now >= last_report_time + opt.o_report_int) {
828 last_report_time = now;
829 CT_TRACE("%%%ju ", (uintmax_t)(100 * write_total / length));
830 /* only give the length of the write since the last
832 he.length = offset - he.offset;
833 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
835 /* Action has been canceled or something wrong
836 * is happening. Stop copying data. */
837 CT_ERROR(rc, "progress ioctl for copy"
838 " '%s'->'%s' failed", src, dst);
848 * truncate restored file
849 * size is taken from the archive this is done to support
850 * restore after a force release which leaves the file with the
851 * wrong size (can big bigger than the new size)
853 if ((hai->hai_action == HSMA_RESTORE) &&
854 (src_st.st_size < dst_st.st_size)) {
856 * make sure the file is on disk before reporting success.
858 rc = ftruncate(dst_fd, src_st.st_size);
861 CT_ERROR(rc, "cannot truncate '%s' to size %jd",
862 dst, (intmax_t)src_st.st_size);
870 CT_TRACE("copied %ju bytes in %f seconds",
871 (uintmax_t)length, ct_now() - start_ct_now);
876 /* Copy file attributes from file src to file dest */
877 static int ct_copy_attr(const char *src, const char *dst, int src_fd,
881 struct timeval times[2];
884 if (fstat(src_fd, &st) < 0) {
886 CT_ERROR(rc, "cannot stat '%s'", src);
890 times[0].tv_sec = st.st_atime;
891 times[0].tv_usec = 0;
892 times[1].tv_sec = st.st_mtime;
893 times[1].tv_usec = 0;
894 if (fchmod(dst_fd, st.st_mode) < 0 ||
895 fchown(dst_fd, st.st_uid, st.st_gid) < 0 ||
896 futimes(dst_fd, times) < 0) {
898 CT_ERROR(rc, "cannot set attributes of '%s'", src);
905 static int ct_copy_xattr(const char *src, const char *dst, int src_fd,
906 int dst_fd, bool is_restore)
908 char list[XATTR_LIST_MAX];
909 char value[XATTR_SIZE_MAX];
914 list_len = flistxattr(src_fd, list, sizeof(list));
919 while (name < list + list_len) {
920 rc = fgetxattr(src_fd, name, value, sizeof(value));
924 /* when we restore, we do not restore lustre xattr */
926 (strncmp(XATTR_TRUSTED_PREFIX, name,
927 sizeof(XATTR_TRUSTED_PREFIX) - 1) != 0)) {
928 rc = fsetxattr(dst_fd, name, value, rc, 0);
929 CT_TRACE("fsetxattr of '%s' on '%s' rc=%d (%s)",
930 name, dst, rc, strerror(errno));
931 /* lustre.* attrs aren't supported on other FS's */
932 if (rc < 0 && errno != EOPNOTSUPP) {
934 CT_ERROR(rc, "cannot set extended attribute"
940 name += strlen(name) + 1;
946 static int ct_path_lustre(char *buf, int sz, const char *mnt,
947 const struct lu_fid *fid)
949 return scnprintf(buf, sz, "%s/%s/fid/"DFID_NOBRACE, mnt,
950 dot_lustre_name, PFID(fid));
953 static int ct_path_archive_v(char *buf, size_t buf_size,
954 enum ct_archive_format ctaf,
955 const char *archive_path,
956 const struct lu_fid *fid,
960 case CT_ARCHIVE_FORMAT_V1:
961 return scnprintf(buf, buf_size,
962 "%s/%04x/%04x/%04x/%04x/%04x/%04x/"DFID_NOBRACE"%s",
964 (fid)->f_oid & 0xFFFF,
965 (fid)->f_oid >> 16 & 0xFFFF,
966 (unsigned int)((fid)->f_seq & 0xFFFF),
967 (unsigned int)((fid)->f_seq >> 16 & 0xFFFF),
968 (unsigned int)((fid)->f_seq >> 32 & 0xFFFF),
969 (unsigned int)((fid)->f_seq >> 48 & 0xFFFF),
972 case CT_ARCHIVE_FORMAT_V2:
973 return scnprintf(buf, buf_size, "%s/%04x/"DFID_NOBRACE"%s",
975 (unsigned int)((fid)->f_oid ^ (fid)->f_seq) & 0xFFFF,
983 static int ct_path_archive(char *buf, size_t buf_size,
984 const char *archive_path, const struct lu_fid *fid)
986 return ct_path_archive_v(buf, buf_size, opt.o_archive_format,
987 archive_path, fid, "");
990 static bool ct_is_retryable(int err)
992 return err == -ETIMEDOUT;
995 static int ct_begin_restore(struct hsm_copyaction_private **phcp,
996 const struct hsm_action_item *hai,
997 int mdt_index, int open_flags)
1002 rc = llapi_hsm_action_begin(phcp, ctdata, hai, mdt_index, open_flags,
1005 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_fid);
1006 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed", src);
1012 static int ct_begin(struct hsm_copyaction_private **phcp,
1013 const struct hsm_action_item *hai)
1015 /* Restore takes specific parameters. Call the same function w/ default
1016 * values for all other operations. */
1017 return ct_begin_restore(phcp, hai, -1, 0);
1020 static int ct_fini(struct hsm_copyaction_private **phcp,
1021 const struct hsm_action_item *hai, int hp_flags, int ct_rc)
1023 struct hsm_copyaction_private *hcp;
1024 char lstr[PATH_MAX];
1027 CT_TRACE("Action completed, notifying coordinator "
1028 "cookie=%#jx, FID="DFID", hp_flags=%d err=%d",
1029 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid),
1032 ct_path_lustre(lstr, sizeof(lstr), opt.o_mnt, &hai->hai_fid);
1034 if (phcp == NULL || *phcp == NULL) {
1035 rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, true);
1037 CT_ERROR(rc, "llapi_hsm_action_begin() on '%s' failed",
1044 rc = llapi_hsm_action_end(phcp, &hai->hai_extent, hp_flags, abs(ct_rc));
1045 if (rc == -ECANCELED)
1046 CT_ERROR(rc, "completed action on '%s' has been canceled: "
1047 "cookie=%#jx, FID="DFID, lstr,
1048 (uintmax_t)hai->hai_cookie, PFID(&hai->hai_fid));
1050 CT_ERROR(rc, "llapi_hsm_action_end() on '%s' failed", lstr);
1052 CT_TRACE("llapi_hsm_action_end() on '%s' ok (rc=%d)",
1058 static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
1060 struct hsm_copyaction_private *hcp = NULL;
1062 char dst[PATH_MAX + 4] = "";
1063 char root[PATH_MAX] = "";
1066 bool rename_needed = false;
1072 rc = ct_begin(&hcp, hai);
1076 /* we fill archive so:
1078 * destination = lustre FID
1080 ct_path_lustre(src, sizeof(src), opt.o_mnt, &hai->hai_dfid);
1081 ct_path_archive(root, sizeof(root), opt.o_hsm_root, &hai->hai_fid);
1082 if (hai->hai_extent.length == -1) {
1083 /* whole file, write it to tmp location and atomically
1084 * replace old archived file */
1085 snprintf(dst, sizeof(dst), "%s_tmp", root);
1086 /* we cannot rely on the same test because ct_copy_data()
1087 * updates hai_extent.length */
1088 rename_needed = true;
1090 snprintf(dst, sizeof(dst), "%s", root);
1093 CT_TRACE("archiving '%s' to '%s'", src, dst);
1095 if (opt.o_dry_run) {
1100 rc = ct_mkdir_p(dst);
1102 CT_ERROR(rc, "mkdir_p '%s' failed", dst);
1106 src_fd = llapi_hsm_action_get_fd(hcp);
1109 CT_ERROR(rc, "cannot open '%s' for read", src);
1113 open_flags = O_WRONLY | O_NOFOLLOW;
1114 /* If extent is specified, don't truncate an old archived copy */
1115 open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
1117 dst_fd = open(dst, open_flags, FILE_PERM);
1120 CT_ERROR(rc, "cannot open '%s' for write", dst);
1124 /* saving stripe is not critical */
1125 rc = ct_save_stripe(src_fd, src, dst);
1127 CT_ERROR(rc, "cannot save file striping info of '%s' in '%s'",
1130 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1132 CT_ERROR(rc, "data copy failed from '%s' to '%s'", src, dst);
1139 CT_ERROR(rc, "cannot flush '%s' archive file '%s'", src, dst);
1143 CT_TRACE("data archiving for '%s' to '%s' done", src, dst);
1145 /* attrs will remain on the MDS; no need to copy them, except possibly
1146 for disaster recovery */
1147 if (opt.o_copy_attrs) {
1148 rc = ct_copy_attr(src, dst, src_fd, dst_fd);
1150 CT_ERROR(rc, "cannot copy attr of '%s' to '%s'",
1154 CT_TRACE("attr file for '%s' saved to archive '%s'",
1158 /* xattrs will remain on the MDS; no need to copy them, except possibly
1159 for disaster recovery */
1160 if (opt.o_copy_xattrs) {
1161 rc = ct_copy_xattr(src, dst, src_fd, dst_fd, false);
1163 CT_ERROR(rc, "cannot copy xattr of '%s' to '%s'",
1165 rcf = rcf ? rcf : rc;
1167 CT_TRACE("xattr file for '%s' saved to archive '%s'",
1171 if (rename_needed == true) {
1172 char tmp_src[PATH_MAX + 8];
1173 char tmp_dst[PATH_MAX + 8];
1175 /* atomically replace old archived file */
1176 ct_path_archive(src, sizeof(src), opt.o_hsm_root,
1178 rc = rename(dst, src);
1181 CT_ERROR(rc, "cannot rename '%s' to '%s'", dst, src);
1184 /* rename lov file */
1185 snprintf(tmp_src, sizeof(tmp_src), "%s.lov", src);
1186 snprintf(tmp_dst, sizeof(tmp_dst), "%s.lov", dst);
1187 rc = rename(tmp_dst, tmp_src);
1189 CT_ERROR(errno, "cannot rename '%s' to '%s'",
1193 if (opt.o_shadow_tree) {
1194 /* Create a namespace of softlinks that shadows the original
1195 * Lustre namespace. This will only be current at
1196 * time-of-archive (won't follow renames).
1197 * WARNING: release won't kill these links; a manual
1198 * cleanup of dead links would be required.
1201 long long recno = -1;
1207 sprintf(src, "%s/shadow/", opt.o_hsm_root);
1209 ptr = opt.o_hsm_root;
1211 (*ptr++ == '/') ? depth-- : 0;
1213 rc = llapi_fid2path_at(opt.o_mnt_fd, &hai->hai_fid,
1215 sizeof(src) - strlen(src),
1218 CT_ERROR(rc, "cannot get FID of "DFID,
1219 PFID(&hai->hai_fid));
1220 rcf = rcf ? rcf : rc;
1224 /* Figure out how many parent dirs to symlink back */
1227 (*ptr++ == '/') ? depth++ : 0;
1232 ct_path_archive(dst, sizeof(dst), buf, &hai->hai_fid);
1234 if (ct_mkdir_p(src)) {
1235 CT_ERROR(errno, "mkdir_p '%s' failed", src);
1236 rcf = rcf ? rcf : -errno;
1239 /* symlink already exists ? */
1240 sz = readlink(src, buf, sizeof(buf));
1241 /* detect truncation */
1242 if (sz == sizeof(buf)) {
1243 rcf = rcf ? rcf : -E2BIG;
1244 CT_ERROR(rcf, "readlink '%s' truncated", src);
1249 if (sz == 0 || strncmp(buf, dst, sz) != 0) {
1250 if (unlink(src) && errno != ENOENT) {
1252 "cannot unlink symlink '%s'",
1254 rcf = rcf ? rcf : -errno;
1257 /* unlink old symlink done */
1258 CT_TRACE("remove old symlink '%s' pointing"
1259 " to '%s'", src, buf);
1261 /* symlink already ok */
1262 CT_TRACE("symlink '%s' already pointing"
1263 " to '%s'", src, dst);
1268 if (symlink(dst, src)) {
1269 CT_ERROR(errno, "cannot symlink '%s' to '%s'",
1271 rcf = rcf ? rcf : -errno;
1274 CT_TRACE("symlink '%s' to '%s' done", src, dst);
1286 if (ct_is_retryable(rc))
1287 hp_flags |= HP_FLAG_RETRY;
1298 rc = ct_fini(&hcp, hai, hp_flags, rcf);
1303 static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
1305 struct hsm_copyaction_private *hcp = NULL;
1308 char lov_buf[XATTR_SIZE_MAX];
1309 size_t lov_size = sizeof(lov_buf);
1318 /* we fill lustre so:
1319 * source = lustre FID in the backend
1320 * destination = data FID = volatile file
1323 /* build backend file name from released file FID */
1324 ct_path_archive(src, sizeof(src), opt.o_hsm_root, &hai->hai_fid);
1326 rc = llapi_get_mdt_index_by_fid(opt.o_mnt_fd, &hai->hai_fid,
1329 CT_ERROR(rc, "cannot get mdt index "DFID"",
1330 PFID(&hai->hai_fid));
1333 /* restore loads and sets the LOVEA w/o interpreting it to avoid
1334 * dependency on the structure format. */
1335 rc = ct_load_stripe(src, lov_buf, &lov_size);
1337 CT_WARN("cannot get stripe rules for '%s' (%s), use default",
1338 src, strerror(-rc));
1341 open_flags |= O_LOV_DELAY_CREATE;
1345 rc = ct_begin_restore(&hcp, hai, mdt_index, open_flags);
1349 /* get the FID of the volatile file */
1350 rc = llapi_hsm_action_get_dfid(hcp, &dfid);
1352 CT_ERROR(rc, "restoring "DFID
1353 ", cannot get FID of created volatile file",
1354 PFID(&hai->hai_fid));
1358 /* build volatile "file name", for messages */
1359 snprintf(dst, sizeof(dst), "{VOLATILE}="DFID, PFID(&dfid));
1361 CT_TRACE("restoring data from '%s' to '%s'", src, dst);
1363 if (opt.o_dry_run) {
1368 src_fd = open(src, O_RDONLY | O_NOATIME | O_NOFOLLOW);
1371 CT_ERROR(rc, "cannot open '%s' for read", src);
1375 dst_fd = llapi_hsm_action_get_fd(hcp);
1378 CT_ERROR(rc, "cannot open '%s' for write", dst);
1383 /* the layout cannot be allocated through .fid so we have to
1384 * restore a layout */
1385 rc = ct_restore_stripe(src, dst, dst_fd, lov_buf, lov_size);
1387 CT_ERROR(rc, "cannot restore file striping info"
1388 " for '%s' from '%s'", dst, src);
1394 rc = ct_copy_data(hcp, src, dst, src_fd, dst_fd, hai, hal_flags);
1396 CT_ERROR(rc, "cannot copy data from '%s' to '%s'",
1399 if (ct_is_retryable(rc))
1400 hp_flags |= HP_FLAG_RETRY;
1404 CT_TRACE("data restore from '%s' to '%s' done", src, dst);
1407 rc = ct_fini(&hcp, hai, hp_flags, rc);
1409 /* object swaping is done by cdt at copy end, so close of volatile file
1410 * cannot be done before */
1420 static int ct_remove(const struct hsm_action_item *hai, const long hal_flags)
1422 struct hsm_copyaction_private *hcp = NULL;
1423 char dst[PATH_MAX], attr[PATH_MAX + 4];
1426 rc = ct_begin(&hcp, hai);
1430 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, &hai->hai_fid);
1432 CT_TRACE("removing file '%s'", dst);
1434 if (opt.o_dry_run) {
1442 CT_ERROR(rc, "cannot unlink '%s'", dst);
1447 snprintf(attr, sizeof(attr), "%s.lov", dst);
1451 CT_ERROR(rc, "cannot unlink '%s'", attr);
1454 /* ignore the error when lov file does not exist. */
1462 rc = ct_fini(&hcp, hai, 0, rc);
1467 static int ct_process_item(struct hsm_action_item *hai, const long hal_flags)
1471 if (opt.o_verbose >= LLAPI_MSG_INFO || opt.o_dry_run) {
1472 /* Print the original path */
1473 char path[PATH_MAX];
1474 long long recno = -1;
1477 CT_TRACE(DFID" action %s reclen %d, cookie=%#jx",
1478 PFID(&hai->hai_fid),
1479 hsm_copytool_action2name(hai->hai_action),
1480 hai->hai_len, (uintmax_t)hai->hai_cookie);
1481 rc = llapi_fid2path_at(opt.o_mnt_fd, &hai->hai_fid, path,
1482 sizeof(path), &recno, &linkno);
1484 CT_ERROR(rc, "cannot get path of "DFID,
1485 PFID(&hai->hai_fid));
1487 CT_TRACE("processing file '%s'", path);
1490 switch (hai->hai_action) {
1491 /* set err_major, minor inside these functions */
1493 rc = ct_archive(hai, hal_flags);
1496 rc = ct_restore(hai, hal_flags);
1499 rc = ct_remove(hai, hal_flags);
1502 CT_TRACE("cancel not implemented for file system '%s'",
1504 /* Don't report progress to coordinator for this cookie:
1505 * the copy function will get ECANCELED when reporting
1512 CT_ERROR(rc, "unknown action %d, on '%s'", hai->hai_action,
1515 ct_fini(NULL, hai, 0, rc);
1523 struct hsm_action_item *hai;
1526 static void *ct_thread(void *data)
1528 struct ct_th_data *cttd = data;
1531 rc = ct_process_item(cttd->hai, cttd->hal_flags);
1535 pthread_exit((void *)(intptr_t)rc);
1538 static int ct_process_item_async(const struct hsm_action_item *hai,
1541 pthread_attr_t attr;
1543 struct ct_th_data *data;
1546 data = malloc(sizeof(*data));
1550 data->hai = malloc(hai->hai_len);
1551 if (data->hai == NULL) {
1556 memcpy(data->hai, hai, hai->hai_len);
1557 data->hal_flags = hal_flags;
1559 rc = pthread_attr_init(&attr);
1561 CT_ERROR(rc, "pthread_attr_init failed for '%s' service",
1568 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1570 rc = pthread_create(&thread, &attr, ct_thread, data);
1572 CT_ERROR(rc, "cannot create thread for '%s' service",
1575 pthread_attr_destroy(&attr);
1579 static int ct_import_one(const char *src, const char *dst)
1581 char newarc[PATH_MAX];
1586 CT_TRACE("importing '%s' from '%s'", dst, src);
1588 if (stat(src, &st) < 0) {
1590 CT_ERROR(rc, "cannot stat '%s'", src);
1597 rc = llapi_hsm_import(dst,
1598 opt.o_archive_id_used ? opt.o_archive_id[0] : 0,
1600 0 /* default stripe_size */,
1601 -1 /* default stripe offset */,
1602 0 /* default stripe count */,
1603 0 /* stripe pattern (will be RAID0+RELEASED) */,
1604 NULL /* pool_name */,
1607 CT_ERROR(rc, "cannot import '%s' from '%s'", dst, src);
1611 ct_path_archive(newarc, sizeof(newarc), opt.o_hsm_root, &fid);
1613 rc = ct_mkdir_p(newarc);
1615 CT_ERROR(rc, "mkdir_p '%s' failed", newarc);
1621 /* Lots of choices now: mv, ln, ln -s ? */
1622 rc = link(src, newarc); /* hardlink */
1625 CT_ERROR(rc, "cannot link '%s' to '%s'", newarc, src);
1629 CT_TRACE("imported '%s' from '%s'=='%s'", dst, newarc, src);
1634 static char *path_concat(const char *dirname, const char *basename)
1639 rc = asprintf(&result, "%s/%s", dirname, basename);
1646 static int ct_import_fid(const struct lu_fid *import_fid)
1648 char fid_path[PATH_MAX];
1651 ct_path_lustre(fid_path, sizeof(fid_path), opt.o_mnt, import_fid);
1652 rc = access(fid_path, F_OK);
1653 if (rc == 0 || errno != ENOENT) {
1654 rc = (errno == 0) ? -EEXIST : -errno;
1655 CT_ERROR(rc, "cannot import '"DFID"'", PFID(import_fid));
1659 ct_path_archive(fid_path, sizeof(fid_path), opt.o_hsm_root,
1662 CT_TRACE("Resolving "DFID" to %s", PFID(import_fid), fid_path);
1664 return ct_import_one(fid_path, opt.o_dst);
1667 static int ct_import_recurse(const char *relpath)
1671 char *srcpath, *newpath;
1672 struct lu_fid import_fid;
1675 if (relpath == NULL)
1678 /* Is relpath a FID? */
1679 rc = llapi_fid_parse(relpath, &import_fid, NULL);
1681 return ct_import_fid(&import_fid);
1683 srcpath = path_concat(opt.o_hsm_root, relpath);
1684 if (srcpath == NULL) {
1689 dir = opendir(srcpath);
1691 /* Not a dir, or error */
1692 if (errno == ENOTDIR) {
1693 /* Single regular file case, treat o_dst as absolute
1695 rc = ct_import_one(srcpath, opt.o_dst);
1698 CT_ERROR(rc, "cannot opendir '%s'", srcpath);
1706 while ((ent = readdir(dir)) != NULL) {
1707 if (!strcmp(ent->d_name, ".") ||
1708 !strcmp(ent->d_name, ".."))
1711 /* New relative path */
1712 newpath = path_concat(relpath, ent->d_name);
1713 if (newpath == NULL) {
1719 if (ent->d_type == DT_DIR) {
1720 rc = ct_import_recurse(newpath);
1725 sprintf(src, "%s/%s", opt.o_hsm_root, newpath);
1726 sprintf(dst, "%s/%s", opt.o_dst, newpath);
1727 /* Make the target dir in the Lustre fs */
1728 rc = ct_mkdir_p(dst);
1730 /* Import the file */
1731 rc = ct_import_one(src, dst);
1733 CT_ERROR(rc, "ct_mkdir_p '%s' failed", dst);
1739 CT_ERROR(rc, "cannot import '%s'", newpath);
1740 if (err_major && opt.o_abort_on_error) {
1754 static int ct_rebind_one(const struct lu_fid *old_fid,
1755 const struct lu_fid *new_fid)
1761 CT_TRACE("rebind "DFID" to "DFID, PFID(old_fid), PFID(new_fid));
1763 ct_path_archive(src, sizeof(src), opt.o_hsm_root, old_fid);
1764 ct_path_archive(dst, sizeof(dst), opt.o_hsm_root, new_fid);
1766 if (!opt.o_dry_run) {
1767 char src_attr[PATH_MAX + 4];
1768 char dst_attr[PATH_MAX + 4];
1771 if (rename(src, dst)) {
1773 CT_ERROR(rc, "cannot rename '%s' to '%s'", src, dst);
1776 /* rename lov file */
1777 snprintf(src_attr, sizeof(src_attr), "%s.lov", src);
1778 snprintf(dst_attr, sizeof(dst_attr), "%s.lov", dst);
1779 if (rename(src_attr, dst_attr))
1780 CT_ERROR(errno, "cannot rename '%s' to '%s'",
1781 src_attr, dst_attr);
1787 static bool fid_is_file(struct lu_fid *fid)
1789 return fid_is_norm(fid) || fid_is_igif(fid);
1792 static bool should_ignore_line(const char *line)
1796 for (i = 0; line[i] != '\0'; i++) {
1797 if (isspace(line[i]))
1799 else if (line[i] == '#')
1808 static int ct_rebind_list(const char *list)
1814 size_t line_size = 0;
1815 unsigned int nl = 0;
1816 unsigned int ok = 0;
1818 filp = fopen(list, "r");
1821 CT_ERROR(rc, "cannot open '%s'", list);
1825 /* each line consists of 2 FID */
1826 while ((r = getline(&line, &line_size, filp)) != -1) {
1827 struct lu_fid old_fid;
1828 struct lu_fid new_fid;
1831 /* Ignore empty and commented out ('#...') lines. */
1832 if (should_ignore_line(line))
1837 rc = llapi_fid_parse(line, &old_fid, &next_fid);
1840 rc = llapi_fid_parse(next_fid, &new_fid, NULL);
1843 if (!fid_is_file(&old_fid) || !fid_is_file(&new_fid))
1846 error: CT_ERROR(rc, "%s:%u: two FIDs expected in '%s'",
1852 if (ct_rebind_one(&old_fid, &new_fid))
1863 /* return 0 if all rebinds were successful */
1864 CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
1866 return ok == nl ? 0 : -1;
1869 static int ct_rebind(void)
1874 struct lu_fid old_fid;
1875 struct lu_fid new_fid;
1877 rc = llapi_fid_parse(opt.o_src, &old_fid, NULL);
1878 if (!rc && !fid_is_file(&old_fid))
1881 CT_ERROR(rc, "invalid source FID '%s'", opt.o_src);
1885 rc = llapi_fid_parse(opt.o_dst, &new_fid, NULL);
1886 if (!rc && !fid_is_file(&new_fid))
1889 CT_ERROR(rc, "invalid destination FID '%s'", opt.o_dst);
1893 rc = ct_rebind_one(&old_fid, &new_fid);
1898 /* o_src is a list file */
1899 rc = ct_rebind_list(opt.o_src);
1904 static int ct_opendirat(int parent_fd, const char *name, DIR **pdir)
1910 fd = openat(parent_fd, name, O_RDONLY|O_DIRECTORY);
1914 dir = fdopendir(fd);
1930 static int ct_archive_upgrade_reg(int arc_fd, enum ct_archive_format ctaf,
1931 int old_fd, const char *name)
1933 char new_path[PATH_MAX];
1937 int suffix_offset = -1;
1940 /* Formatted fix with optional suffix. We do not inspect
1942 scan_count = sscanf(name, SFID"%n", RFID(&fid), &suffix_offset);
1943 if (scan_count != 3 || suffix_offset < 0) {
1945 CT_TRACE("ignoring unexpected file '%s' in archive", name);
1949 ct_path_archive_v(new_path, sizeof(new_path),
1950 ctaf, ".", &fid, name + suffix_offset);
1952 rc = renameat(old_fd, name, arc_fd, new_path);
1956 if (errno != ENOENT) {
1961 /* Create parent directory and try again. */
1962 split = strrchr(new_path, '/');
1965 rc = ct_mkdirat_p(arc_fd, new_path, DIR_PERM);
1970 rc = renameat(old_fd, name, arc_fd, new_path);
1975 CT_ERROR(rc, "cannot rename '%s' to '%s'", name, new_path);
1977 CT_TRACE("renamed '%s' to '%s'", name, new_path);
1982 static const char *d_type_name(unsigned int type)
1984 static const char *name[] = {
1985 [DT_UNKNOWN] = "unknown",
1996 if (type < ARRAY_SIZE(name) && name[type] != NULL)
1999 return name[DT_UNKNOWN];
2002 static int ct_archive_upgrade_dir(int arc_fd, enum ct_archive_format ctaf,
2003 int parent_fd, const char *dir_name)
2010 rc = ct_opendirat(parent_fd, dir_name, &dir);
2012 CT_ERROR(rc, "cannot open archive dir '%s'", dir_name);
2016 while ((d = readdir(dir)) != NULL) {
2017 CT_TRACE("archive upgrade found %s '%s' (%ld)\n",
2018 d_type_name(d->d_type), d->d_name, d->d_ino);
2020 switch (d->d_type) {
2022 if (strcmp(d->d_name, ".") == 0 ||
2023 strcmp(d->d_name, "..") == 0)
2026 if (strlen(d->d_name) != 4 ||
2027 strspn(d->d_name, "0123456789abcdef") != 4)
2030 rc2 = ct_archive_upgrade_dir(arc_fd, ctaf,
2031 dirfd(dir), d->d_name);
2034 CT_ERROR(rc, "cannot upgrade dir '%s' (%ld)",
2035 d->d_name, d->d_ino);
2038 rc2 = unlinkat(dirfd(dir), d->d_name, AT_REMOVEDIR);
2039 CT_TRACE("unlink dir '%s' (%ld): %s",
2040 d->d_name, d->d_ino, strerror(rc2 < 0 ? errno: 0));
2041 if (rc2 < 0 && errno != ENOTEMPTY)
2046 rc2 = ct_archive_upgrade_reg(arc_fd, ctaf,
2047 dirfd(dir), d->d_name);
2054 CT_TRACE("ignoring unexpected %s '%s' (%ld) in archive",
2055 d_type_name(d->d_type), d->d_name, d->d_ino);
2066 /* Recursive inplace upgrade (or downgrade) of archive to format
2067 * ctaf. Prunes empty archive subdirectories. Idempotent. */
2068 static int ct_archive_upgrade(int arc_fd, enum ct_archive_format ctaf)
2070 /* FIXME Handle shadow tree. */
2071 CT_TRACE("upgrade archive to format %s", ct_archive_format_to_str(ctaf));
2073 return ct_archive_upgrade_dir(arc_fd, ctaf, arc_fd, ".");
2076 static int ct_dir_level_max(const char *dirpath, __u16 *sub_seqmax)
2085 dir = opendir(dirpath);
2088 CT_ERROR(rc, "cannot open directory '%s'", opt.o_hsm_root);
2096 /* end of directory.
2097 * rc is 0 and seqmax contains the max value. */
2100 CT_ERROR(rc, "cannot readdir '%s'", dirpath);
2104 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
2107 if (sscanf(ent->d_name, "%hx", &sub_seq) != 1) {
2108 CT_TRACE("'%s' has an unexpected dirname format, "
2109 "skip entry", ent->d_name);
2112 if (sub_seq > *sub_seqmax)
2113 *sub_seqmax = sub_seq;
2120 static int ct_max_sequence(void)
2123 char path[PATH_MAX];
2127 snprintf(path, sizeof(path), "%s", opt.o_hsm_root);
2128 /* FID sequence is stored in top-level directory names:
2129 * hsm_root/16bits (high weight)/16 bits/16 bits/16 bits (low weight).
2131 for (i = 0; i < 4; i++) {
2134 rc = ct_dir_level_max(path, &subseq);
2137 seq |= ((__u64)subseq << ((3 - i) * 16));
2138 path_len = strlen(path);
2139 rc = snprintf(path + path_len, sizeof(path) - path_len,
2141 if (rc >= (sizeof(path) - path_len))
2143 path[sizeof(path) - 1] = '\0';
2146 printf("max_sequence: %#jx\n", (uintmax_t)seq);
2151 static void handler(int signal)
2153 psignal(signal, "exiting");
2154 /* If we don't clean up upon interrupt, umount thinks there's a ref
2155 * and doesn't remove us from mtab (EINPROGRESS). The lustre client
2156 * does successfully unmount and the mount is actually gone, but the
2157 * mtab entry remains. So this just makes mtab happier. */
2158 llapi_hsm_copytool_unregister(&ctdata);
2160 /* Also remove fifo upon signal as during normal/error exit */
2161 if (opt.o_event_fifo != NULL)
2162 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
2166 /* Daemon waits for messages from the kernel; run it in the background. */
2167 static int ct_run(void)
2169 struct sigaction cleanup_sigaction;
2172 if (opt.o_daemonize) {
2176 CT_ERROR(rc, "cannot daemonize");
2181 if (opt.o_pid_file != NULL) {
2182 pid_file_fd = create_pid_file(opt.o_pid_file);
2183 if (pid_file_fd < 0) {
2185 CT_ERROR(rc, "cannot create PID file");
2190 setbuf(stdout, NULL);
2192 if (opt.o_event_fifo != NULL) {
2193 rc = llapi_hsm_register_event_fifo(opt.o_event_fifo);
2195 CT_ERROR(rc, "failed to register event fifo");
2198 llapi_error_callback_set(llapi_hsm_log_error);
2201 rc = llapi_hsm_copytool_register(&ctdata, opt.o_mnt,
2202 opt.o_archive_id_used,
2203 opt.o_archive_id, 0);
2205 CT_ERROR(rc, "cannot start copytool interface");
2209 memset(&cleanup_sigaction, 0, sizeof(cleanup_sigaction));
2210 cleanup_sigaction.sa_handler = handler;
2211 sigemptyset(&cleanup_sigaction.sa_mask);
2212 sigaction(SIGINT, &cleanup_sigaction, NULL);
2213 sigaction(SIGTERM, &cleanup_sigaction, NULL);
2216 struct hsm_action_list *hal;
2217 struct hsm_action_item *hai;
2221 CT_TRACE("waiting for message from kernel");
2223 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
2224 if (rc == -ESHUTDOWN) {
2225 CT_TRACE("shutting down");
2227 } else if (rc < 0) {
2228 CT_WARN("cannot receive action list: %s",
2231 if (opt.o_abort_on_error)
2237 CT_TRACE("copytool fs=%s archive#=%d item_count=%d",
2238 hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
2240 if (strcmp(hal->hal_fsname, fs_name) != 0) {
2242 CT_ERROR(rc, "'%s' invalid fs name, expecting: %s",
2243 hal->hal_fsname, fs_name);
2245 if (opt.o_abort_on_error)
2251 hai = hai_first(hal);
2252 while (++i <= hal->hal_count) {
2253 if ((char *)hai - (char *)hal > msgsize) {
2256 "'%s' item %d past end of message!",
2261 rc = ct_process_item_async(hai, hal->hal_flags);
2263 CT_ERROR(rc, "'%s' item %d process",
2265 if (opt.o_abort_on_error && err_major)
2267 hai = hai_next(hai);
2270 if (opt.o_abort_on_error && err_major)
2274 llapi_hsm_copytool_unregister(&ctdata);
2275 if (opt.o_event_fifo != NULL)
2276 llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
2281 static int ct_config_get_str(struct cYAML *obj, const char *key, char **pvalue)
2286 if (obj->cy_type != CYAML_TYPE_OBJECT)
2289 for (cy = obj->cy_child; cy != NULL; cy = cy->cy_next) {
2290 if (cy->cy_string != NULL && strcmp(cy->cy_string, key) == 0) {
2291 if (cy->cy_type != CYAML_TYPE_STRING)
2294 if (cy->cy_valuestring == NULL)
2297 value = strdup(cy->cy_valuestring);
2310 static int ct_config_archive_format(struct cYAML *config)
2315 rc = ct_config_get_str(config, "archive_format", &value);
2317 return (rc == -ENOENT) ? 0 : rc;
2319 rc = ct_str_to_archive_format(value, &opt.o_archive_format);
2323 CT_TRACE("setting archive format to %s",
2324 ct_archive_format_to_str(opt.o_archive_format));
2332 static int ct_config_archive_path(struct cYAML *config)
2336 rc = ct_config_get_str(config, "archive_path", &opt.o_hsm_root);
2338 return (rc == -ENOENT) ? 0 : rc;
2340 CT_TRACE("setting archive path to '%s'", opt.o_hsm_root);
2345 static int ct_config(const char *path)
2348 struct cYAML *config = NULL;
2354 file = fopen(path, "r");
2357 CT_ERROR(rc, "cannot open '%s'", path);
2361 config = cYAML_load(file, NULL, false);
2362 if (config == NULL) {
2364 CT_ERROR(rc, "cannot load archive config from '%s'", path);
2368 rc = ct_config_archive_format(config);
2370 CT_ERROR(rc, "cannot load archive format from '%s'", path);
2374 rc = ct_config_archive_path(config);
2376 CT_ERROR(rc, "cannot load archive path from '%s'", path);
2381 cYAML_free_tree(config);
2389 static int ct_setup(void)
2393 if (opt.o_action == CA_ARCHIVE_UPGRADE)
2396 rc = llapi_search_fsname(opt.o_mnt, fs_name);
2398 CT_ERROR(rc, "cannot find a Lustre filesystem mounted at '%s'",
2403 opt.o_mnt_fd = open(opt.o_mnt, O_RDONLY);
2404 if (opt.o_mnt_fd < 0) {
2406 CT_ERROR(rc, "cannot open mount point at '%s'",
2414 static int ct_cleanup(void)
2418 if (opt.o_mnt_fd >= 0) {
2419 rc = close(opt.o_mnt_fd);
2422 CT_ERROR(rc, "cannot close mount point");
2431 CT_ERROR(rc, "cannot close archive root directory");
2436 if (opt.o_archive_id_cnt > 0) {
2437 free(opt.o_archive_id);
2438 opt.o_archive_id = NULL;
2439 opt.o_archive_id_cnt = 0;
2445 int main(int argc, char **argv)
2449 snprintf(cmd_name, sizeof(cmd_name), "%s", basename(argv[0]));
2450 rc = ct_parseopts(argc, argv);
2452 CT_WARN("try '%s --help' for more information", cmd_name);
2456 llapi_msg_set_level(opt.o_verbose);
2458 rc = ct_config(opt.o_config_path);
2462 if (opt.o_hsm_root == NULL) {
2464 CT_ERROR(rc, "must specify a root directory for the backend");
2468 arc_fd = open(opt.o_hsm_root, O_RDONLY);
2471 CT_ERROR(rc, "cannot open archive at '%s'", opt.o_hsm_root);
2479 switch (opt.o_action) {
2481 rc = ct_import_recurse(opt.o_src);
2487 rc = ct_max_sequence();
2489 case CA_ARCHIVE_UPGRADE:
2490 rc = ct_archive_upgrade(arc_fd, opt.o_archive_format);
2497 if (opt.o_action != CA_MAXSEQ)
2498 CT_TRACE("process finished, errs: %d major, %d minor,"
2499 " rc=%d (%s)", err_major, err_minor, rc,