Whamcloud - gitweb
LU-5710 all: second batch of corrected typos and grammar errors
[fs/lustre-release.git] / lustre / utils / lhsmtool_posix.c
index dda8551..8eef8e6 100644 (file)
@@ -23,6 +23,7 @@
  * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
  *     alternatives
  *
+ * Copyright (c) 2013, 2014, Intel Corporation.
  */
 /* HSM copytool program for POSIX filesystem-based HSM's.
  *
@@ -42,6 +43,7 @@
 #include <dirent.h>
 #include <errno.h>
 #include <getopt.h>
+#include <pthread.h>
 #include <time.h>
 #include <utime.h>
 #include <sys/time.h>
 # define NSEC_PER_SEC 1000000000UL
 #endif
 
-/* copytool uses a 32b bitmask field to register with kuc
- * archive num = 0 => all
- * archive num from 1 to 32
- */
-#define MAX_ARCHIVE_CNT (sizeof(__u32) * 8)
-
 enum ct_action {
        CA_IMPORT = 1,
        CA_REBIND,
@@ -85,7 +81,7 @@ struct options {
        int                      o_verbose;
        int                      o_copy_xattrs;
        int                      o_archive_cnt;
-       int                      o_archive_id[MAX_ARCHIVE_CNT];
+       int                      o_archive_id[LL_HSM_MAX_ARCHIVE];
        int                      o_report_int;
        unsigned long long       o_bandwidth;
        size_t                   o_chunk_size;
@@ -245,11 +241,11 @@ static int ct_parseopts(int argc, char * const *argv)
                                long_opts, NULL)) != -1) {
                switch (c) {
                case 'A':
-                       if ((opt.o_archive_cnt >= MAX_ARCHIVE_CNT) ||
-                           (atoi(optarg) >= MAX_ARCHIVE_CNT)) {
+                       if ((opt.o_archive_cnt >= LL_HSM_MAX_ARCHIVE) ||
+                           (atoi(optarg) >= LL_HSM_MAX_ARCHIVE)) {
                                rc = -E2BIG;
                                CT_ERROR(rc, "archive number must be less"
-                                        "than %zu", MAX_ARCHIVE_CNT);
+                                        "than %zu", LL_HSM_MAX_ARCHIVE);
                                return rc;
                        }
                        opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
@@ -351,7 +347,7 @@ static int ct_parseopts(int argc, char * const *argv)
        CT_TRACE("action=%d src=%s dst=%s mount_point=%s",
                 opt.o_action, opt.o_src, opt.o_dst, opt.o_mnt);
 
-       if (!opt.o_dry_run && opt.o_hsm_root == NULL) {
+       if (opt.o_hsm_root == NULL) {
                rc = -EINVAL;
                CT_ERROR(rc, "must specify a root directory for the backend");
                return rc;
@@ -542,6 +538,14 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
                return rc;
        }
 
+       if (hai->hai_extent.offset > (__u64)src_st.st_size) {
+               rc = -EINVAL;
+               CT_ERROR(rc, "Trying to start reading past end ("LPU64" > "
+                        "%jd) of '%s' source file", hai->hai_extent.offset,
+                        (intmax_t)src_st.st_size, src);
+               return rc;
+       }
+
        if (fstat(dst_fd, &dst_st) < 0) {
                rc = -errno;
                CT_ERROR(rc, "cannot stat '%s'", dst);
@@ -554,17 +558,9 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
                return rc;
        }
 
-       rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
-       if (rc < 0) {
-               rc = -errno;
-               CT_ERROR(rc,
-                        "cannot seek for read to "LPU64" (len %jd) in '%s'",
-                        hai->hai_extent.offset, (intmax_t)src_st.st_size, src);
-               return rc;
-       }
-
        /* Don't read beyond a given extent */
-       length = min(hai->hai_extent.length, src_st.st_size);
+       length = min(hai->hai_extent.length,
+                    src_st.st_size - hai->hai_extent.offset);
 
        start_time = last_bw_print = last_report_time = time(NULL);
 
@@ -916,7 +912,7 @@ static int ct_archive(const struct hsm_action_item *hai, const long hal_flags)
                goto fini_major;
        }
 
-       open_flags = O_WRONLY | O_NOFOLLOW | O_NONBLOCK;
+       open_flags = O_WRONLY | O_NOFOLLOW;
        /* If extent is specified, don't truncate an old archived copy */
        open_flags |= ((hai->hai_extent.length == -1) ? O_TRUNC : 0) | O_CREAT;
 
@@ -1169,7 +1165,7 @@ static int ct_restore(const struct hsm_action_item *hai, const long hal_flags)
                goto fini;
        }
 
-       src_fd = open(src, O_RDONLY | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
+       src_fd = open(src, O_RDONLY | O_NOATIME | O_NOFOLLOW);
        if (src_fd < 0) {
                rc = -errno;
                CT_ERROR(rc, "cannot open '%s' for read", src);
@@ -1654,7 +1650,7 @@ static int ct_rebind_list(const char *list)
        if (line)
                free(line);
 
-       /* return 0 if all rebinds were sucessful */
+       /* return 0 if all rebinds were successful */
        CT_TRACE("%u lines read from '%s', %u rebind successful", nl, list, ok);
 
        return ok == nl ? 0 : -1;
@@ -1773,6 +1769,10 @@ static void handler(int signal)
         * does successfully unmount and the mount is actually gone, but the
         * mtab entry remains. So this just makes mtab happier. */
        llapi_hsm_copytool_unregister(&ctdata);
+
+       /* Also remove fifo upon signal as during normal/error exit */
+       if (opt.o_event_fifo != NULL)
+               llapi_hsm_unregister_event_fifo(opt.o_event_fifo);
        _exit(1);
 }