Whamcloud - gitweb
Revert "LU-5261 osc: use wait_for_completion_killable() instead"
[fs/lustre-release.git] / lustre / utils / lustre_rsync.c
index c05a19e..b167afa 100644 (file)
@@ -26,6 +26,8 @@
 /*
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #include <libcfs/libcfsutil.h>
 #include <lustre/lustreapi.h>
+#include <lustre/lustre_idl.h>
 #include "lustre_rsync.h"
 
 #define REPLICATE_STATUS_VER 1
@@ -566,10 +569,9 @@ int lr_get_symlink(struct lr_info *info)
         } else {
                 link = info->linktmp;
         }
-        strncpy(info->link, link, PATH_MAX);
-        info->link[PATH_MAX] = '\0';
+       strlcpy(info->link, link, sizeof(info->link));
 
-        return rc;
+       return rc;
 }
 
 /* Create file/directory/device file/symlink. */
@@ -625,18 +627,29 @@ int lr_mkfile(struct lr_info *info)
 
 int lr_add_pc(const char *pfid, const char *tfid, const char *name)
 {
-        struct lr_parent_child_list *p;
-
-        p = calloc(1, sizeof(*p));
-        if (!p)
-                return -ENOMEM;
-        strcpy(p->pc_log.pcl_pfid, pfid);
-        strcpy(p->pc_log.pcl_tfid, tfid);
-        strcpy(p->pc_log.pcl_name, name);
-
-        p->pc_next = parents;
-        parents = p;
-        return 0;
+       struct lr_parent_child_list *p;
+       size_t len;
+
+       p = calloc(1, sizeof(*p));
+       if (p == NULL)
+               return -ENOMEM;
+       len = strlcpy(p->pc_log.pcl_pfid, pfid, sizeof(p->pc_log.pcl_pfid));
+       if (len >= sizeof(p->pc_log.pcl_pfid))
+               goto out_err;
+       len = strlcpy(p->pc_log.pcl_tfid, tfid, sizeof(p->pc_log.pcl_tfid));
+       if (len >= sizeof(p->pc_log.pcl_tfid))
+               goto out_err;
+       len = strlcpy(p->pc_log.pcl_name, name, sizeof(p->pc_log.pcl_name));
+       if (len >= sizeof(p->pc_log.pcl_name))
+               goto out_err;
+
+       p->pc_next = parents;
+       parents = p;
+       return 0;
+
+out_err:
+       free(p);
+       return -E2BIG;
 }
 
 void lr_cascade_move(const char *fid, const char *dest, struct lr_info *info)
@@ -1108,7 +1121,8 @@ int lr_parse_line(void *priv, struct lr_info *info)
         info->type = rec->cr_type;
         sprintf(info->tfid, DFID, PFID(&rec->cr_tfid));
         sprintf(info->pfid, DFID, PFID(&rec->cr_pfid));
-        strncpy(info->name, rec->cr_name, rec->cr_namelen);
+       strncpy(info->name, rec->cr_name, rec->cr_namelen);
+       info->name[rec->cr_namelen] = '\0';
 
        if (fid_is_sane(&rec->cr_sfid)) {
                sprintf(info->sfid, DFID, PFID(&rec->cr_sfid));
@@ -1121,8 +1135,6 @@ int lr_parse_line(void *priv, struct lr_info *info)
                        printf("Rec %lld: %d %s %s\n", info->recno, info->type,
                                info->name, info->sname);
        } else {
-               info->name[rec->cr_namelen] = '\0';
-
                if (verbose > 1)
                        printf("Rec %lld: %d %s\n", info->recno, info->type,
                                info->name);
@@ -1137,7 +1149,7 @@ int lr_parse_line(void *priv, struct lr_info *info)
 /* Initialize the replication parameters */
 int lr_init_status()
 {
-        size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX;
+        size_t size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
 
         if (status != NULL)
                 return 0;
@@ -1216,38 +1228,57 @@ int lr_read_log()
         struct lustre_rsync_status *s;
         int fd = -1;
         size_t size;
-        size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX;
+        size_t read_size = sizeof(struct lustre_rsync_status) + PATH_MAX + 1;
         int rc = 0;
 
         if (statuslog == NULL)
                 return 0;
 
         s = calloc(1, read_size);
-        if (s == NULL)
-                GOTO(out, rc = -ENOMEM);
-
-        fd = open(statuslog, O_RDONLY);
-        if (fd == -1)
-                GOTO(out, rc = -errno);
-        size = read(fd, s, read_size);
-        if (size != read_size)
-                GOTO(out, rc = -EINVAL);
-        if (read_size < s->ls_size) {
-                read_size = s->ls_size;
-                s = lr_grow_buf(s, read_size);
-                if (s == NULL)
-                        GOTO(out, rc = -ENOMEM);
-                if (lseek(fd, 0, SEEK_SET) == -1)
-                        GOTO(out, rc = -errno);
-                size = read(fd, s, read_size);
-                if (size != read_size)
-                        GOTO(out, rc = -EINVAL);
-        }
+       if (s == NULL) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       fd = open(statuslog, O_RDONLY);
+       if (fd == -1) {
+               rc = -errno;
+               goto out;
+       }
+
+       size = read(fd, s, read_size);
+       if (size != read_size) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (read_size < s->ls_size) {
+               read_size = s->ls_size;
+               s = lr_grow_buf(s, read_size);
+               if (s == NULL) {
+                       rc = -ENOMEM;
+                       goto out;
+               }
+
+               if (lseek(fd, 0, SEEK_SET) == -1) {
+                       rc = -ENOMEM;
+                       goto out;
+               }
+
+               size = read(fd, s, read_size);
+               if (size != read_size) {
+                       rc = -EINVAL;
+                       goto out;
+               }
+       }
+
+       while (read(fd, &rec, sizeof(rec)) != 0) {
+               tmp = calloc(1, sizeof(*tmp));
+               if (!tmp) {
+                       rc = -ENOMEM;
+                       goto out;
+               }
 
-        while (read(fd, &rec, sizeof(rec)) != 0) {
-                tmp = calloc(1, sizeof(*tmp));
-                if (!tmp)
-                        GOTO(out, rc = -ENOMEM);
                 tmp->pc_log = rec;
                 tmp->pc_next = parents;
                 parents = tmp;
@@ -1257,31 +1288,35 @@ int lr_read_log()
         if (status->ls_num_targets == 0) {
                 if (status->ls_size != s->ls_size) {
                         status = lr_grow_buf(status, s->ls_size);
-                        if (status == NULL)
-                                GOTO(out, rc = -ENOMEM);
+                       if (status == NULL) {
+                               rc = -ENOMEM;
+                               goto out;
+                       }
+
                         status->ls_size = s->ls_size;
                 }
                 status->ls_num_targets = s->ls_num_targets;
                 memcpy(status->ls_targets, s->ls_targets,
-                       PATH_MAX * s->ls_num_targets);
+                       (PATH_MAX + 1) * s->ls_num_targets);
         }
         if (status->ls_last_recno == -1)
                 status->ls_last_recno = s->ls_last_recno;
 
-        if (status->ls_registration[0] == '\0')
-                strncpy(status->ls_registration, s->ls_registration,
-                        LR_NAME_MAXLEN);
+       if (status->ls_registration[0] == '\0')
+               strlcpy(status->ls_registration, s->ls_registration,
+                       sizeof(status->ls_registration));
 
-        if (status->ls_mdt_device[0] == '\0')
-                strncpy(status->ls_mdt_device, s->ls_mdt_device,
-                        LR_NAME_MAXLEN);
+       if (status->ls_mdt_device[0] == '\0')
+               strlcpy(status->ls_mdt_device, s->ls_mdt_device,
+                       sizeof(status->ls_mdt_device));
 
-        if (status->ls_source_fs[0] == '\0')
-                strncpy(status->ls_source_fs, s->ls_source_fs,
-                        LR_NAME_MAXLEN);
+       if (status->ls_source_fs[0] == '\0')
+               strlcpy(status->ls_source_fs, s->ls_source_fs,
+                       sizeof(status->ls_source_fs));
 
-        if (status->ls_source[0] == '\0')
-                strncpy(status->ls_source, s->ls_source, PATH_MAX);
+       if (status->ls_source[0] == '\0')
+               strlcpy(status->ls_source, s->ls_source,
+                       sizeof(status->ls_source));
 
  out:
         if (fd != -1)
@@ -1295,9 +1330,9 @@ int lr_read_log()
    processing. */
 int lr_clear_cl(struct lr_info *info, int force)
 {
-        char    mdt_device[LR_NAME_MAXLEN + 1];
-        long long rec;
-        int rc = 0;
+       char            mdt_device[LR_NAME_MAXLEN + 1];
+       long long       rec;
+       int             rc = 0;
 
         if (force || info->recno > status->ls_last_recno + CLEAR_INTERVAL) {
                 if (info->type == CL_RENAME)
@@ -1309,8 +1344,8 @@ int lr_clear_cl(struct lr_info *info, int force)
                          * device name so make a copy of it until this
                          * is fixed.
                         */
-                        strncpy(mdt_device, status->ls_mdt_device,
-                                LR_NAME_MAXLEN);
+                       strlcpy(mdt_device, status->ls_mdt_device,
+                               sizeof(mdt_device));
                         rc = llapi_changelog_clear(mdt_device,
                                                    status->ls_registration,
                                                    rec);
@@ -1482,8 +1517,8 @@ int lr_replicate()
                        memcpy(info->spfid, info->pfid, sizeof(info->spfid));
                        memcpy(info->tfid, ext->tfid, sizeof(info->tfid));
                        memcpy(info->pfid, ext->pfid, sizeof(info->pfid));
-                       strncpy(info->sname, info->name, sizeof(info->sname));
-                       strncpy(info->name, ext->name, sizeof(info->name));
+                       strlcpy(info->sname, info->name, sizeof(info->sname));
+                       strlcpy(info->name, ext->name, sizeof(info->name));
                        info->is_extended = 1;
                }
 
@@ -1516,15 +1551,16 @@ int lr_replicate()
                 case CL_XATTR:
                         rc = lr_setxattr(info);
                         break;
-                case CL_CLOSE:
-                case CL_EXT:
-                case CL_OPEN:
-                case CL_IOCTL:
-                case CL_MARK:
-                        /* Nothing needs to be done for these entries */
-                default:
-                        break;
-                }
+               case CL_CLOSE:
+               case CL_EXT:
+               case CL_OPEN:
+               case CL_LAYOUT:
+               case CL_MARK:
+                       /* Nothing needs to be done for these entries */
+                       /* fallthrough */
+               default:
+                       break;
+               }
                DEBUG_EXIT(info, rc);
                 if (rc && rc != -ENOENT) {
                         lr_print_failure(info, rc);
@@ -1589,7 +1625,8 @@ int main(int argc, char *argv[])
                         break;
                 case 's':
                         /* Assume absolute paths */
-                        strncpy(status->ls_source, optarg, PATH_MAX);
+                       strlcpy(status->ls_source, optarg,
+                               sizeof(status->ls_source));
                         break;
                 case 't':
                         status->ls_num_targets++;
@@ -1603,23 +1640,23 @@ int main(int argc, char *argv[])
                                 status->ls_num_targets = numtargets;
                         }
                         newsize = sizeof (struct lustre_rsync_status) +
-                                (status->ls_num_targets * PATH_MAX);
+                                (status->ls_num_targets * (PATH_MAX + 1));
                         if (status->ls_size != newsize) {
                                 status->ls_size = newsize;
                                 status = lr_grow_buf(status, newsize);
                                 if (status == NULL)
                                         return -ENOMEM;
                         }
-                        strncpy(status->ls_targets[status->ls_num_targets - 1],
-                                optarg,
-                                PATH_MAX);
-                        break;
-                case 'm':
-                        strncpy(status->ls_mdt_device, optarg, LR_NAME_MAXLEN);
-                        break;
-                case 'u':
-                        strncpy(status->ls_registration, optarg,
-                                LR_NAME_MAXLEN);
+                       strlcpy(status->ls_targets[status->ls_num_targets - 1],
+                               optarg, sizeof(status->ls_targets[0]));
+                       break;
+               case 'm':
+                       strlcpy(status->ls_mdt_device, optarg,
+                               sizeof(status->ls_mdt_device));
+                       break;
+               case 'u':
+                       strlcpy(status->ls_registration, optarg,
+                               sizeof(status->ls_registration));
                         break;
                 case 'l':
                         statuslog = optarg;
@@ -1673,6 +1710,8 @@ int main(int argc, char *argv[])
                         break;
                case 'D':
                        /* Undocumented option debug log file */
+                       if (debug_log != NULL)
+                               fclose(debug_log);
                        debug_log = fopen(optarg, "a");
                        if (debug_log == NULL) {
                                printf("Cannot open %s for debug log\n",