Whamcloud - gitweb
LU-10822 utils: stop bogus buffer overflow errors
[fs/lustre-release.git] / lustre / utils / liblustreapi_hsm.c
index ed3511f..41eac0f 100644 (file)
@@ -6,7 +6,7 @@
  * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
  *     alternatives
  *
- * Copyright (c) 2013, 2016, Intel Corporation.
+ * Copyright (c) 2013, 2017, Intel Corporation.
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the GNU Lesser General Public License
@@ -78,6 +78,7 @@ struct hsm_copytool_private {
 #define CP_PRIV_MAGIC 0x19880429
 struct hsm_copyaction_private {
        __u32                                    magic;
+       __u32                                    source_fd;
        __s32                                    data_fd;
        const struct hsm_copytool_private       *ct_priv;
        struct hsm_copy                          copy;
@@ -933,7 +934,7 @@ out_err:
  * \param parent_len Destination buffer size.
  * \return 0 on success.
  */
-static int fid_parent(const char *mnt, const lustre_fid *fid, char *parent,
+static int fid_parent(const char *mnt, const struct lu_fid *fid, char *parent,
                      size_t parent_len)
 {
        int              rc;
@@ -993,32 +994,30 @@ static int ct_md_getattr(const struct hsm_copytool_private *ct,
                         lstat_t *st)
 {
        struct lov_user_mds_data *lmd;
+       char fname[FID_NOBRACE_LEN + 1] = "";
        size_t lmd_size;
        int rc;
 
+       rc = snprintf(fname, sizeof(fname), DFID_NOBRACE, PFID(fid));
+       if (rc < 0)
+               return rc;
+       if (rc >= sizeof(fname) || rc == 0)
+               return -EINVAL;
+
        lmd_size = sizeof(lmd->lmd_st) +
                lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
 
        if (lmd_size < sizeof(lmd->lmd_st) + XATTR_SIZE_MAX)
                lmd_size = sizeof(lmd->lmd_st) + XATTR_SIZE_MAX;
 
-       if (lmd_size < FID_NOBRACE_LEN + 1)
-               lmd_size = FID_NOBRACE_LEN + 1;
-
        lmd = malloc(lmd_size);
        if (lmd == NULL)
                return -ENOMEM;
 
-       snprintf((char *)lmd, lmd_size, DFID_NOBRACE, PFID(fid));
-
-       rc = ioctl(ct->open_by_fid_fd, IOC_MDC_GETFILEINFO, lmd);
-       if (rc != 0) {
-               rc = -errno;
-               llapi_error(LLAPI_MSG_ERROR, rc,
-                           "cannot get metadata attributes of "DFID" in '%s'",
-                           PFID(fid), ct->mnt);
+       rc = get_lmd_info_fd(fname, ct->open_by_fid_fd, -1,
+                            lmd, lmd_size, GET_LMD_INFO);
+       if (rc)
                goto out;
-       }
 
        *st = lmd->lmd_st;
 out:
@@ -1099,13 +1098,15 @@ int llapi_hsm_action_begin(struct hsm_copyaction_private **phcp,
                           int restore_mdt_index, int restore_open_flags,
                           bool is_error)
 {
-       struct hsm_copyaction_private   *hcp;
-       int                              rc;
+       struct hsm_copyaction_private *hcp;
+       int fd;
+       int rc;
 
        hcp = calloc(1, sizeof(*hcp));
        if (hcp == NULL)
                return -ENOMEM;
 
+       hcp->source_fd = -1;
        hcp->data_fd = -1;
        hcp->ct_priv = ct;
        hcp->copy.hc_hai = *hai;
@@ -1114,7 +1115,16 @@ int llapi_hsm_action_begin(struct hsm_copyaction_private **phcp,
        if (is_error)
                goto ok_out;
 
-       if (hai->hai_action == HSMA_RESTORE) {
+       if (hai->hai_action == HSMA_ARCHIVE) {
+               fd = ct_open_by_fid(hcp->ct_priv, &hai->hai_dfid,
+                               O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NONBLOCK);
+               if (fd < 0) {
+                       rc = fd;
+                       goto err_out;
+               }
+
+               hcp->source_fd = fd;
+       } else if (hai->hai_action == HSMA_RESTORE) {
                rc = ct_md_getattr(hcp->ct_priv, &hai->hai_fid, &hcp->stat);
                if (rc < 0)
                        goto err_out;
@@ -1123,12 +1133,11 @@ int llapi_hsm_action_begin(struct hsm_copyaction_private **phcp,
                                             restore_open_flags);
                if (rc < 0)
                        goto err_out;
-       }
-
-       /* Since remove is atomic there is no need to send an initial
-        * MDS_HSM_PROGRESS RPC. */
-       if (hai->hai_action == HSMA_REMOVE)
+       } else if (hai->hai_action == HSMA_REMOVE) {
+               /* Since remove is atomic there is no need to send an
+                * initial MDS_HSM_PROGRESS RPC. */
                goto out_log;
+       }
 
        rc = ioctl(ct->mnt_fd, LL_IOC_HSM_COPY_START, &hcp->copy);
        if (rc < 0) {
@@ -1145,6 +1154,9 @@ ok_out:
        return 0;
 
 err_out:
+       if (!(hcp->source_fd < 0))
+               close(hcp->source_fd);
+
        if (!(hcp->data_fd < 0))
                close(hcp->data_fd);
 
@@ -1226,6 +1238,9 @@ end:
        llapi_hsm_log_ct_progress(&hcp, hai, CT_FINISH, 0, 0);
 
 err_cleanup:
+       if (!(hcp->source_fd < 0))
+               close(hcp->source_fd);
+
        if (!(hcp->data_fd < 0))
                close(hcp->data_fd);
 
@@ -1280,7 +1295,7 @@ int llapi_hsm_action_progress(struct hsm_copyaction_private *hcp,
  * @return error code if the action is not a copy operation.
  */
 int llapi_hsm_action_get_dfid(const struct hsm_copyaction_private *hcp,
-                             lustre_fid *fid)
+                             struct lu_fid *fid)
 {
        const struct hsm_action_item    *hai = &hcp->copy.hc_hai;
 
@@ -1311,8 +1326,8 @@ int llapi_hsm_action_get_fd(const struct hsm_copyaction_private *hcp)
                return -EINVAL;
 
        if (hai->hai_action == HSMA_ARCHIVE) {
-               return ct_open_by_fid(hcp->ct_priv, &hai->hai_dfid,
-                               O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NONBLOCK);
+               fd = dup(hcp->source_fd);
+               return fd < 0 ? -errno : fd;
        } else if (hai->hai_action == HSMA_RESTORE) {
                fd = dup(hcp->data_fd);
                return fd < 0 ? -errno : fd;
@@ -1337,7 +1352,7 @@ int llapi_hsm_action_get_fd(const struct hsm_copyaction_private *hcp)
 int llapi_hsm_import(const char *dst, int archive, const struct stat *st,
                     unsigned long long stripe_size, int stripe_offset,
                     int stripe_count, int stripe_pattern, char *pool_name,
-                    lustre_fid *newfid)
+                    struct lu_fid *newfid)
 {
        struct hsm_user_import   hui;
        int                      fd;