Whamcloud - gitweb
LU-1270 utils: properly initialize traverse param
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index ffe0e80..4404fae 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2011, 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -71,9 +71,8 @@
 #include <liblustre.h>
 #include <lnet/lnetctl.h>
 #include <obd.h>
-#include <lustre_lib.h>
 #include <obd_lov.h>
-#include <lustre/liblustreapi.h>
+#include <lustre/lustreapi.h>
 
 static unsigned llapi_dir_filetype_table[] = {
         [DT_UNKNOWN]= 0,
@@ -226,47 +225,47 @@ int parse_size(char *optarg, unsigned long long *size,
 /* XXX: llapi_xxx() functions return negative values upon failure */
 
 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
-                             int stripe_count, int stripe_pattern)
-{
-        int page_size, rc;
-
-        /* 64 KB is the largest common page size I'm aware of (on ia64), but
-         * check the local page size just in case. */
-        page_size = LOV_MIN_STRIPE_SIZE;
-        if (getpagesize() > page_size) {
-                page_size = getpagesize();
-                llapi_err_noerrno(LLAPI_MSG_WARN,
-                                  "warning: your page size (%u) is "
-                                  "larger than expected (%u)", page_size,
-                                  LOV_MIN_STRIPE_SIZE);
-        }
-        if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
-                rc = -EINVAL;
-                llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
-                            "must be an even multiple of %d bytes",
-                            stripe_size, page_size);
-                return rc;
-        }
-        if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
-                rc = -EINVAL;
-                llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
-                            stripe_offset);
-                return rc;
-        }
-        if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
-                rc = -EINVAL;
-                llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
-                            stripe_count);
-                return rc;
-        }
-        if (stripe_size >= (1ULL << 32)){
-                rc = -EINVAL;
-                llapi_error(LLAPI_MSG_ERROR, rc,
-                            "warning: stripe size larger than 4G "
-                            "is not currently supported and would wrap");
-                return rc;
-        }
-        return 0;
+                               int stripe_count, int stripe_pattern)
+{
+       int page_size, rc;
+
+       /* 64 KB is the largest common page size I'm aware of (on ia64), but
+        * check the local page size just in case. */
+       page_size = LOV_MIN_STRIPE_SIZE;
+       if (getpagesize() > page_size) {
+               page_size = getpagesize();
+               llapi_err_noerrno(LLAPI_MSG_WARN,
+                               "warning: your page size (%u) is "
+                               "larger than expected (%u)", page_size,
+                               LOV_MIN_STRIPE_SIZE);
+       }
+       if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
+               rc = -EINVAL;
+               llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
+                               "must be an even multiple of %d bytes",
+                               stripe_size, page_size);
+               return rc;
+       }
+       if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
+               rc = -EINVAL;
+               llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
+                               stripe_offset);
+               return rc;
+       }
+       if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
+               rc = -EINVAL;
+               llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
+                               stripe_count);
+               return rc;
+       }
+       if (stripe_size >= (1ULL << 32)) {
+               rc = -EINVAL;
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                               "warning: stripe size 4G or larger "
+                               "is not currently supported and would wrap");
+               return rc;
+       }
+       return 0;
 }
 
 /* return the first file matching this pattern */
@@ -410,6 +409,8 @@ static int get_param_obdvar(const char *fsname, const char *file_path,
                         llapi_error(LLAPI_MSG_ERROR, rc,
                                     "'%s' is not on a Lustre filesystem",
                                     file_path);
+                       if (fp != NULL)
+                               fclose(fp);
                         return rc;
                 }
         } else if (fsname) {
@@ -1016,7 +1017,7 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                         rc = -errno;
                         llapi_error(LLAPI_MSG_ERROR, rc,
                                     "Error reading pool list for '%s'", name);
-                        return rc;
+                       goto out;
                 } else if ((rc == 0) && (cookie == NULL)) {
                         /* end of directory */
                         break;
@@ -1027,13 +1028,17 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                         continue;
 
                 /* check output bounds */
-                if (nb_entries >= list_size)
-                        return -EOVERFLOW;
+               if (nb_entries >= list_size) {
+                       rc = -EOVERFLOW;
+                       goto out;
+               }
 
                 /* +2 for '.' and final '\0' */
-                if (used + strlen(pool.d_name) + strlen(fsname) + 2
-                    > buffer_size)
-                        return -EOVERFLOW;
+               if (used + strlen(pool.d_name) + strlen(fsname) + 2
+                   > buffer_size) {
+                       rc = -EOVERFLOW;
+                       goto out;
+               }
 
                 sprintf(buffer + used, "%s.%s", fsname, pool.d_name);
                 poollist[nb_entries] = buffer + used;
@@ -1041,8 +1046,9 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                 nb_entries++;
         }
 
+out:
         closedir(dir);
-        return nb_entries;
+       return ((rc != 0) ? rc : nb_entries);
 }
 
 /* wrapper for lfs.c and obd.c */
@@ -1124,10 +1130,11 @@ static int common_param_init(struct find_param *param, char *path)
                 return -ENOMEM;
         }
 
-        param->got_uuids = 0;
-        param->obdindexes = NULL;
-        param->obdindex = OBD_NOT_FOUND;
-        return 0;
+       param->got_uuids = 0;
+       param->obdindexes = NULL;
+       param->obdindex = OBD_NOT_FOUND;
+       param->mdtindex = OBD_NOT_FOUND;
+       return 0;
 }
 
 static void find_param_fini(struct find_param *param)
@@ -1606,8 +1613,10 @@ retry_get_uuids:
                 if (ret == -EOVERFLOW) {
                         uuids_temp = realloc(uuids, obdcount *
                                              sizeof(struct obd_uuid));
-                        if (uuids_temp != NULL)
+                       if (uuids_temp != NULL) {
+                               uuids = uuids_temp;
                                 goto retry_get_uuids;
+                       }
                         else
                                 ret = -ENOMEM;
                 }
@@ -2682,9 +2691,15 @@ static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data,
                 return ret;
         }
 
+       /* The 'LASSERT(parent != NULL || d != NULL);' guarantees
+        * that either 'd' or 'parent' is not null.
+        * So in all cases llapi_file_fget_mdtidx() is called,
+        * thus initializing 'mdtidx'. */
         if (param->quiet || !(param->verbose & VERBOSE_DETAIL))
+               /* coverity[uninit_use_in_call] */
                 llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx);
         else
+               /* coverity[uninit_use_in_call] */
                 llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
                              path, mdtidx);
 
@@ -2850,6 +2865,8 @@ int llapi_ping(char *obd_type, char *obd_name)
                 return rc;
         }
 
+       /* The purpose is to send a byte as a ping, whatever this byte is. */
+       /* coverity[uninit_use_in_call] */
         rc = write(fd, buf, 1);
         if (rc < 0)
                 rc = -errno;
@@ -2924,51 +2941,6 @@ int llapi_target_check(int type_num, char **obd_type, char *dir)
 
 #undef MAX_STRING_SIZE
 
-int llapi_catinfo(char *dir, char *keyword, char *node_name)
-{
-        char raw[OBD_MAX_IOCTL_BUFFER];
-        char out[LLOG_CHUNK_SIZE];
-        char *buf = raw;
-        struct obd_ioctl_data data = { 0 };
-        char key[30];
-        DIR *root;
-        int rc;
-
-        sprintf(key, "%s", keyword);
-        memset(raw, 0, sizeof(raw));
-        memset(out, 0, sizeof(out));
-        data.ioc_inlbuf1 = key;
-        data.ioc_inllen1 = strlen(key) + 1;
-        if (node_name) {
-                data.ioc_inlbuf2 = node_name;
-                data.ioc_inllen2 = strlen(node_name) + 1;
-        }
-        data.ioc_pbuf1 = out;
-        data.ioc_plen1 = sizeof(out);
-        rc = obd_ioctl_pack(&data, &buf, sizeof(raw));
-        if (rc)
-                return rc;
-
-        root = opendir(dir);
-        if (root == NULL) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", dir);
-                return rc;
-        }
-
-        rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
-        if (rc) {
-                rc = -errno;
-                llapi_error(LLAPI_MSG_ERROR, rc,
-                            "ioctl OBD_IOC_CATINFO failed");
-        } else {
-                llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
-        }
-
-        closedir(root);
-        return rc;
-}
-
 /* Is this a lustre fs? */
 int llapi_is_lustre_mnttype(const char *type)
 {
@@ -3113,7 +3085,7 @@ static int rmtacl_notify(int ops)
 {
         FILE *fp;
         struct mntent *mnt;
-        int found = 0, fd, rc;
+       int found = 0, fd = 0, rc = 0;
 
         fp = setmntent(MOUNTED, "r");
         if (fp == NULL) {
@@ -3128,7 +3100,7 @@ static int rmtacl_notify(int ops)
                 if (!mnt)
                         break;
 
-                if (!llapi_is_lustre_mnt(mnt))
+               if (!llapi_is_lustre_mnt(mnt))
                         continue;
 
                 fd = open(mnt->mnt_dir, O_RDONLY | O_DIRECTORY);
@@ -3136,20 +3108,23 @@ static int rmtacl_notify(int ops)
                         rc = -errno;
                         llapi_error(LLAPI_MSG_ERROR, rc,
                                     "Can't open '%s'\n", mnt->mnt_dir);
-                        return rc;
+                       goto out;
                 }
 
                 rc = ioctl(fd, LL_IOC_RMTACL, ops);
+               close(fd);
                 if (rc < 0) {
                         rc = -errno;
                         llapi_error(LLAPI_MSG_ERROR, rc, "ioctl %d\n", fd);
-                        return rc;
+                       goto out;
                 }
 
                 found++;
         }
+
+out:
         endmntent(fp);
-        return found;
+       return ((rc != 0) ? rc : found);
 }
 
 static char *next_token(char *p, int div)