Whamcloud - gitweb
LU-1889 build: fix false 'uninitialized scalar variable' errs
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index e9937a9..8d8143e 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -73,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,
@@ -185,36 +182,37 @@ int parse_size(char *optarg, unsigned long long *size,
         *size = strtoull(optarg, &end, 0);
 
         if (*end != '\0') {
-                if ((*end == 'b') && *(end+1) == '\0' &&
+                if ((*end == 'b') && *(end + 1) == '\0' &&
                     (*size & (~0ULL << (64 - 9))) == 0 &&
                     !bytes_spec) {
                         *size_units = 1 << 9;
-                } else if ((*end == 'b') && *(end+1) == '\0' &&
+                } else if ((*end == 'b') &&
+                           *(end + 1) == '\0' &&
                            bytes_spec) {
                         *size_units = 1;
                 } else if ((*end == 'k' || *end == 'K') &&
-                           *(end+1) == '\0' && (*size &
-                           (~0ULL << (64 - 10))) == 0) {
+                           *(end + 1) == '\0' &&
+                           (*size & (~0ULL << (64 - 10))) == 0) {
                         *size_units = 1 << 10;
                 } else if ((*end == 'm' || *end == 'M') &&
-                           *(end+1) == '\0' && (*size &
-                           (~0ULL << (64 - 20))) == 0) {
+                           *(end + 1) == '\0' &&
+                           (*size & (~0ULL << (64 - 20))) == 0) {
                         *size_units = 1 << 20;
                 } else if ((*end == 'g' || *end == 'G') &&
-                           *(end+1) == '\0' && (*size &
-                           (~0ULL << (64 - 30))) == 0) {
+                           *(end + 1) == '\0' &&
+                           (*size & (~0ULL << (64 - 30))) == 0) {
                         *size_units = 1 << 30;
                 } else if ((*end == 't' || *end == 'T') &&
-                           *(end+1) == '\0' && (*size &
-                           (~0ULL << (64 - 40))) == 0) {
+                           *(end + 1) == '\0' &&
+                           (*size & (~0ULL << (64 - 40))) == 0) {
                         *size_units = 1ULL << 40;
                 } else if ((*end == 'p' || *end == 'P') &&
-                           *(end+1) == '\0' && (*size &
-                           (~0ULL << (64 - 50))) == 0) {
+                           *(end + 1) == '\0' &&
+                           (*size & (~0ULL << (64 - 50))) == 0) {
                         *size_units = 1ULL << 50;
                 } else if ((*end == 'e' || *end == 'E') &&
-                           *(end+1) == '\0' && (*size &
-                           (~0ULL << (64 - 60))) == 0) {
+                           *(end + 1) == '\0' &&
+                           (*size & (~0ULL << (64 - 60))) == 0) {
                         *size_units = 1ULL << 60;
                 } else {
                         return -1;
@@ -227,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 */
@@ -411,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) {
@@ -1017,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;
@@ -1028,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;
@@ -1042,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 */
@@ -1607,8 +1612,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;
                 }
@@ -1624,12 +1631,21 @@ retry_get_uuids:
         }
 
         for (obdnum = 0; obdnum < num_obds; obdnum++) {
-                for (i = 0; i < obdcount; i++) {
-                        if (llapi_uuid_match(uuids[i].uuid,
-                                             obduuids[obdnum].uuid)) {
-                                indexes[obdnum] = i;
-                                obd_valid++;
-                                break;
+                char *end = NULL;
+
+                /* The user may have specified a simple index */
+                i = strtol(obduuids[obdnum].uuid, &end, 0);
+                if (end && *end == '\0' && i < obdcount) {
+                        indexes[obdnum] = i;
+                        obd_valid++;
+                } else {
+                        for (i = 0; i < obdcount; i++) {
+                                if (llapi_uuid_match(uuids[i].uuid,
+                                                     obduuids[obdnum].uuid)) {
+                                        indexes[obdnum] = i;
+                                        obd_valid++;
+                                        break;
+                                }
                         }
                 }
                 if (i >= obdcount) {
@@ -1972,8 +1988,8 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
 
 void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
                             struct lov_user_ost_data_v1 *objects,
-                            char *path, int is_dir,
-                            int obdindex, int depth, int header, int raw)
+                            char *path, int is_dir, int obdindex,
+                            int depth, int header, int raw)
 {
         int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1;
 
@@ -2351,7 +2367,8 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir,
          * to be compared. */
         if (param->obduuid    || param->mdtuuid || param->check_uid ||
             param->check_gid || param->check_pool || param->atime   ||
-            param->ctime     || param->mtime || param->check_size)
+            param->ctime     || param->mtime || param->check_size ||
+            param->check_stripecount || param->check_stripesize)
                 decision = 0;
 
         if (param->type && checked_type == 0)
@@ -2438,6 +2455,26 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir,
                 }
         }
 
+        if (param->check_stripesize) {
+                decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_size,
+                                          param->stripesize,
+                                          param->stripesize_sign,
+                                          param->exclude_stripesize,
+                                          param->stripesize_units, 0);
+                if (decision == -1)
+                        goto decided;
+        }
+
+        if (param->check_stripecount) {
+                decision = find_value_cmp(param->lmd->lmd_lmm.lmm_stripe_count,
+                                          param->stripecount,
+                                          param->stripecount_sign,
+                                          param->exclude_stripecount, 1, 0);
+                if (decision == -1)
+                        goto decided;
+        }
+
+        /* If an OBD UUID is specified but none matches, skip this file. */
         if ((param->obduuid && param->obdindex == OBD_NOT_FOUND) ||
             (param->mdtuuid && param->mdtindex == OBD_NOT_FOUND))
                 goto decided;
@@ -2653,10 +2690,16 @@ 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
-                llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n",
+               /* coverity[uninit_use_in_call] */
+                llapi_printf(LLAPI_MSG_NORMAL, "%s\nmdt_index:\t%d\n",
                              path, mdtidx);
 
 out:
@@ -2740,7 +2783,7 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
         }
 
 dump:
-        if (!param->get_mdt_index)
+        if (!(param->verbose & VERBOSE_MDTINDEX))
                 llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
 
 out:
@@ -2754,7 +2797,7 @@ out:
 
 int llapi_getstripe(char *path, struct find_param *param)
 {
-        return param_callback(path, param->get_mdt_index ?
+        return param_callback(path, (param->verbose & VERBOSE_MDTINDEX) ?
                               cb_get_mdt_index : cb_getstripe,
                               cb_common_fini, param);
 }
@@ -2821,6 +2864,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;
@@ -2895,51 +2940,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)
 {
@@ -3084,7 +3084,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) {
@@ -3099,7 +3099,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);
@@ -3107,20 +3107,24 @@ 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);
                 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;
+       if (fd >= 0)
+               close(fd);
+       return ((rc != 0) ? rc : found);
 }
 
 static char *next_token(char *p, int div)
@@ -3540,6 +3544,24 @@ int llapi_changelog_fini(void **priv)
         return 0;
 }
 
+/** Convert a changelog_rec to changelog_ext_rec, in this way client can treat
+ *  all records in the format of changelog_ext_rec, this can make record
+ *  analysis simpler.
+ */
+static inline int changelog_extend_rec(struct changelog_ext_rec *ext)
+{
+       if (!CHANGELOG_REC_EXTENDED(ext)) {
+               struct changelog_rec *rec = (struct changelog_rec *)ext;
+
+               memmove(ext->cr_name, rec->cr_name, rec->cr_namelen);
+               fid_zero(&ext->cr_sfid);
+               fid_zero(&ext->cr_spfid);
+               return 1;
+       }
+
+       return 0;
+}
+
 /** Read the next changelog entry
  * @param priv Opaque private control structure
  * @param rech Changelog record handle; record will be allocated here
@@ -3547,7 +3569,7 @@ int llapi_changelog_fini(void **priv)
  *         <0 error code
  *         1 EOF
  */
-int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
+int llapi_changelog_recv(void *priv, struct changelog_ext_rec **rech)
 {
         struct changelog_private *cp = (struct changelog_private *)priv;
         struct kuc_hdr *kuch;
@@ -3588,10 +3610,11 @@ repeat:
                 }
         }
 
-        /* Our message is a changelog_rec.  Use pointer math to skip
-         * kuch_hdr and point directly to the message payload.
-         */
-        *rech = (struct changelog_rec *)(kuch + 1);
+       /* Our message is a changelog_ext_rec.  Use pointer math to skip
+        * kuch_hdr and point directly to the message payload.
+        */
+       *rech = (struct changelog_ext_rec *)(kuch + 1);
+       changelog_extend_rec(*rech);
 
         return 0;
 
@@ -3602,7 +3625,7 @@ out_free:
 }
 
 /** Release the changelog record when done with it. */
-int llapi_changelog_free(struct changelog_rec **rech)
+int llapi_changelog_free(struct changelog_ext_rec **rech)
 {
         if (*rech) {
                 /* We allocated memory starting at the kuc_hdr, but passed
@@ -3702,13 +3725,13 @@ int llapi_path2fid(const char *path, lustre_fid *fid)
         memset(fid, 0, sizeof(*fid));
         fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
         if (fd < 0) {
-                if (errno == ELOOP) /* symbolic link */
+                if (errno == ELOOP || errno == ENXIO)
                         return path2fid_from_lma(path, fid);
                 return -errno;
         }
 
         rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0;
-        if (rc == -EINVAL) /* char special device */
+        if (rc == -EINVAL || rc == -ENOTTY)
                 rc = path2fid_from_lma(path, fid);
 
         close(fd);
@@ -3888,8 +3911,8 @@ out_free:
 /** Release the action list when done with it. */
 int llapi_copytool_free(struct hsm_action_list **hal)
 {
-        /* Reuse the llapi_changelog_free function */
-        return llapi_changelog_free((struct changelog_rec **)hal);
+       /* Reuse the llapi_changelog_free function */
+       return llapi_changelog_free((struct changelog_ext_rec **)hal);
 }
 
 int llapi_get_connect_flags(const char *mnt, __u64 *flags)
@@ -3941,3 +3964,33 @@ int llapi_get_version(char *buffer, int buffer_size,
         *version = data->ioc_bulk;
         return 0;
 }
+
+/**
+ * Get a 64-bit value representing the version of file data pointed by fd.
+ *
+ * Each write or truncate, flushed on OST, will change this value. You can use
+ * this value to verify if file data was modified. This only checks the file
+ * data, not metadata.
+ *
+ * \param  flags  If set to LL_DV_NOFLUSH, the data version will be read
+ *                directly from OST without regard to possible dirty cache on
+ *                client nodes.
+ *
+ * \retval 0 on success.
+ * \retval -errno on error.
+ */
+int llapi_get_data_version(int fd, __u64 *data_version, __u64 flags)
+{
+        int rc;
+        struct ioc_data_version idv;
+
+        idv.idv_flags = flags;
+
+        rc = ioctl(fd, LL_IOC_DATA_VERSION, &idv);
+        if (rc)
+                rc = -errno;
+        else
+                *data_version = idv.idv_version;
+
+        return rc;
+}