Whamcloud - gitweb
LU-11776 utils: add support lfs find with mdt hash flag
[fs/lustre-release.git] / lustre / utils / lsnapshot.c
index 3d79674..0f1539e 100644 (file)
@@ -44,6 +44,7 @@
 
 #include <libcfs/util/list.h>
 #include <libcfs/util/ioctl.h>
+#include <libcfs/util/string.h>
 #include <linux/lustre/lustre_ioctl.h>
 #include <linux/lustre/lustre_barrier_user.h>
 
@@ -244,28 +245,20 @@ static int snapshot_load_conf_ldev(struct snapshot_instance *si, char *buf,
        if (*ptr == '/') {
                ptr1 = strrchr(ptr, '/');
                *ptr1 = '\0';
-               len = strlen(ptr);
-               st->st_dir = malloc(len + 1);
+               st->st_dir = strdup(ptr);
                if (!st->st_dir) {
                        rc = -ENOMEM;
                        goto out;
                }
-
-               strncpy(st->st_dir, ptr, len);
-               st->st_dir[len] = '\0';
                ptr = ptr1 + 1;
        }
 
-       len = strlen(ptr);
-       st->st_pool = malloc(len + 1);
+       st->st_pool = strdup(ptr);
        if (!st->st_pool) {
                rc = -ENOMEM;
                goto out;
        }
 
-       strncpy(st->st_pool, ptr, len);
-       st->st_pool[len] = '\0';
-
        /* Format of label:
         * fsname-<role><index> or <role><index> */
        ptr = strrchr(label, '-');
@@ -658,21 +651,15 @@ static void snapshot_unload_conf(struct snapshot_instance *si)
 static int snapshot_handle_string_option(char **dst, const char *option,
                                         const char *opt_name)
 {
-       int len;
-
        if (*dst && *dst != snapshot_rsh_default) {
                fprintf(stderr,
                        "%s option has been specified repeatedly.\n", opt_name);
                return -EINVAL;
        }
 
-       len = strlen(option);
-       *dst = malloc(len + 1);
+       *dst = strdup(option);
        if (!*dst)
                return -ENOMEM;
-
-       strncpy(*dst, option, len);
-       (*dst)[len] = '\0';
        return 0;
 }
 
@@ -922,7 +909,7 @@ static char *snapshot_first_skip_blank(char *buf)
 static int mdt0_is_lustre_snapshot(struct snapshot_instance *si)
 {
        struct snapshot_target *st = si->si_mdt0;
-       char buf[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE * 3];
        FILE *fp;
        int rc;
 
@@ -1007,7 +994,7 @@ static int snapshot_get_fsname(struct snapshot_instance *si,
                               char *fsname, int fslen)
 {
        struct snapshot_target *st = si->si_mdt0;
-       char buf[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE * 3];
        FILE *fp;
        int rc = 0;
 
@@ -1033,7 +1020,7 @@ static int snapshot_get_fsname(struct snapshot_instance *si,
 static int snapshot_get_mgsnode(struct snapshot_instance *si,
                                char *node, int size)
 {
-       char buf[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE * 2];
        struct snapshot_target *st;
        FILE *fp;
        int rc = 0;
@@ -1061,7 +1048,7 @@ static int snapshot_get_mgsnode(struct snapshot_instance *si,
 static int snapshot_exists_check(struct snapshot_instance *si,
                                 struct snapshot_target *st)
 {
-       char buf[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE * 2];
        FILE *fp;
        int rc = 0;
 
@@ -1128,7 +1115,7 @@ static int snapshot_inherit_prop(struct snapshot_instance *si,
                                 struct snapshot_target *st,
                                 char *cmd, int size)
 {
-       char buf[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE * 3];
        FILE *fp;
        int len = 0;
        int rc = 0;
@@ -1200,8 +1187,8 @@ static int snapshot_inherit_prop(struct snapshot_instance *si,
                if (end)
                        *end = '\0';
 
-               rc = snprintf(cmd + len, size - len - 1,
-                             "-o %s=\"%s\" ", buf, ptr);
+               rc = scnprintf(cmd + len, size - len - 1,
+                              "-o %s=\"%s\" ", buf, ptr);
                if (rc <= 0)
                        return -EOVERFLOW;
 
@@ -1243,21 +1230,23 @@ static int __snapshot_create(struct snapshot_instance *si,
                        int len;
 
                        memset(cmd, 0, sizeof(cmd));
-                       len = snprintf(cmd, sizeof(cmd) - 1,
-                                      DRSH" '"DZFS" snapshot "
-                                      "-o lustre:fsname=%s "
-                                      "-o lustre:magic=%s "
-                                      "-o lustre:ctime=%llu "
-                                      "-o lustre:mtime=%llu ",
-                                      PRSH(si, st), PZFS(st), fsname,
-                                      SNAPSHOT_MAGIC, xtime, xtime);
+                       len = scnprintf(cmd, sizeof(cmd) - 1,
+                                       DRSH" '"DZFS" snapshot "
+                                       "-o lustre:fsname=%s "
+                                       "-o lustre:magic=%s "
+                                       "-o lustre:ctime=%llu "
+                                       "-o lustre:mtime=%llu ",
+                                       PRSH(si, st), PZFS(st), fsname,
+                                       SNAPSHOT_MAGIC,
+                                       (unsigned long long)xtime,
+                                       (unsigned long long)xtime);
                        if (len <= 0)
                                exit(-EOVERFLOW);
 
                        if (si->si_comment) {
-                               rc = snprintf(cmd + len, sizeof(cmd) - len - 1,
-                                             "-o lustre:comment=\"%s\" ",
-                                             si->si_comment);
+                               rc = scnprintf(cmd + len, sizeof(cmd) - len - 1,
+                                              "-o lustre:comment=\"%s\" ",
+                                              si->si_comment);
                                if (rc <= 0)
                                        exit(-EOVERFLOW);
 
@@ -1597,7 +1586,7 @@ static int __snapshot_destroy(struct snapshot_instance *si,
 
                /* child */
                if (pid == 0) {
-                       char cmd[MAX_BUF_SIZE];
+                       char cmd[MAX_BUF_SIZE * 2];
 
                        memset(cmd, 0, sizeof(cmd));
                        if (si->si_force)
@@ -1801,7 +1790,7 @@ static int __snapshot_modify(struct snapshot_instance *si,
 
                /* child */
                if (pid == 0) {
-                       char cmd[MAX_BUF_SIZE];
+                       char cmd[MAX_BUF_SIZE * 5];
 
                        memset(cmd, 0, sizeof(cmd));
                        if (si->si_new_ssname && si->si_comment)
@@ -1814,7 +1803,8 @@ static int __snapshot_modify(struct snapshot_instance *si,
                                         PRSH(si, st), PIMPORT(st), PZFS(st),
                                         PSSNAME(si, st), PSS_NEW(si, st),
                                         PZFS(st), si->si_comment,
-                                        PSS_NEW(si, st), PZFS(st), xtime,
+                                        PSS_NEW(si, st), PZFS(st),
+                                        (unsigned long long)xtime,
                                         PSS_NEW(si, st));
                        else if (si->si_new_ssname)
                                snprintf(cmd, sizeof(cmd) - 1,
@@ -1823,7 +1813,8 @@ static int __snapshot_modify(struct snapshot_instance *si,
                                         " set lustre:mtime=%llu "DSSNAME"'",
                                         PRSH(si, st), PIMPORT(st), PZFS(st),
                                         PSSNAME(si, st), PSS_NEW(si, st),
-                                        PZFS(st), xtime, PSS_NEW(si, st));
+                                        PZFS(st), (unsigned long long)xtime,
+                                        PSS_NEW(si, st));
                        else if (si->si_comment)
                                snprintf(cmd, sizeof(cmd) - 1,
                                         DRSH" '"DIMPORT"; "DZFS
@@ -1832,7 +1823,8 @@ static int __snapshot_modify(struct snapshot_instance *si,
                                         " set lustre:mtime=%llu "DSSNAME"'",
                                         PRSH(si, st), PIMPORT(st), PZFS(st),
                                         si->si_comment, PSSNAME(si, st),
-                                        PZFS(st), xtime, PSSNAME(si, st));
+                                        PZFS(st), (unsigned long long)xtime,
+                                        PSSNAME(si, st));
                        else
                                exit(-EINVAL);
 
@@ -1942,7 +1934,7 @@ static void snapshot_list_usage(void)
 static int snapshot_list_one(struct snapshot_instance *si,
                             struct snapshot_target *st)
 {
-       char buf[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE * 3];
        FILE *fp;
        int rc;
 
@@ -1991,7 +1983,7 @@ static int snapshot_list_one(struct snapshot_instance *si,
                            strlen("lustre:ctime")) == 0) {
                        ptr = snapshot_first_skip_blank(buf);
                        if (ptr) {
-                               sscanf(ptr, "%llu", &xtime);
+                               xtime = (__u64)strtoull(ptr, NULL, 10);
                                printf("create_time: %s",
                                       ctime((time_t *)&xtime));
                        }
@@ -2002,7 +1994,7 @@ static int snapshot_list_one(struct snapshot_instance *si,
                            strlen("lustre:mtime")) == 0) {
                        ptr = snapshot_first_skip_blank(buf);
                        if (ptr) {
-                               sscanf(ptr, "%llu", &xtime);
+                               xtime = (__u64)strtoull(ptr, NULL, 10);
                                printf("modify_time: %s",
                                       ctime((time_t *)&xtime));
                        }
@@ -2074,7 +2066,7 @@ static int snapshot_list_all(struct snapshot_instance *si)
 
        struct list_head list_sub_items;
        struct list_sub_item *lsi;
-       char buf[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE * 2];
        FILE *fp;
        int rc = 0;
 
@@ -2101,8 +2093,7 @@ static int snapshot_list_all(struct snapshot_instance *si)
                        break;
                }
 
-               strncpy(lsi->lsi_ssname, buf, len);
-               lsi->lsi_ssname[len] = '\0';
+               memcpy(lsi->lsi_ssname, buf, len + 1);
                list_add(&lsi->lsi_list, &list_sub_items);
        }
 
@@ -2198,7 +2189,7 @@ static int snapshot_mount_check(struct snapshot_instance *si, char *fsname,
 static int snapshot_mount_target(struct snapshot_instance *si,
                                 struct snapshot_target *st, const char *optstr)
 {
-       char cmd[MAX_BUF_SIZE];
+       char cmd[MAX_BUF_SIZE * 2];
        char name[8];
        int rc;