Whamcloud - gitweb
LU-11881 utils: silence error message
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index 5f95d44..bb1bf98 100644 (file)
@@ -290,8 +290,9 @@ int llapi_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max_len)
        data->ioc_version = OBD_IOCTL_VERSION;
 
        if (*pbuf != NULL && data->ioc_len > max_len) {
-               fprintf(stderr, "pbuf = %p, ioc_len = %u, max_len = %d\n",
-                       *pbuf, data->ioc_len, max_len);
+               llapi_error(LLAPI_MSG_ERROR, -EINVAL,
+                           "pbuf = %p, ioc_len = %u, max_len = %d\n",
+                           *pbuf, data->ioc_len, max_len);
                return -EINVAL;
        }
 
@@ -1344,26 +1345,36 @@ int llapi_search_rootpath(char *pathname, const char *fsname)
 
 int llapi_getname(const char *path, char *buf, size_t size)
 {
-        struct obd_uuid uuid_buf;
-        char *uuid = uuid_buf.uuid;
-        int rc, nr;
+       struct obd_uuid uuid_buf;
+       char *uuid = uuid_buf.uuid;
+       char *cfg_instance;
+       int rc, len, fsname_len;
 
-        memset(&uuid_buf, 0, sizeof(uuid_buf));
-        rc = llapi_file_get_lov_uuid(path, &uuid_buf);
-        if (rc)
-                return rc;
+       memset(&uuid_buf, 0, sizeof(uuid_buf));
+       rc = llapi_file_get_lov_uuid(path, &uuid_buf);
+       if (rc)
+               return rc;
 
-        /* We want to turn lustre-clilov-ffff88002738bc00 into
-         * lustre-ffff88002738bc00. */
+       /*
+        * We want to turn testfs-clilov-ffff88002738bc00 into
+        * testfs-ffff88002738bc00 in a portable way that doesn't depend
+        * on what is after "-clilov-" as it may change in the future.
+        * Unfortunately, the "fsname" part may contain a dash, so we
+        * can't just skip to the first dash, and the "instance" may be a
+        * UUID in the future, so we can't necessarily go to the last dash.
+        */
+       cfg_instance = strstr(uuid, "-clilov-");
+       if (!cfg_instance)
+               return -EINVAL;
 
-        nr = snprintf(buf, size, "%.*s-%s",
-                      (int) (strlen(uuid) - 24), uuid,
-                      uuid + strlen(uuid) - 16);
+       fsname_len = cfg_instance - uuid;
+       cfg_instance += strlen("-clilov-");
+       len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
 
-        if (nr >= size)
-                rc = -ENAMETOOLONG;
+       if (len >= size)
+               rc = -ENAMETOOLONG;
 
-        return rc;
+       return rc;
 }
 
 /**
@@ -1509,7 +1520,6 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                goto free_path;
        }
 
-       llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
        dir = opendir(pathname.gl_pathv[0]);
        if (dir == NULL) {
                rc = -errno;
@@ -1534,14 +1544,14 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                 /* check output bounds */
                if (nb_entries >= list_size) {
                        rc = -EOVERFLOW;
-                       goto free_dir;
+                       goto free_dir_no_msg;
                }
 
                 /* +2 for '.' and final '\0' */
                if (used + strlen(pool->d_name) + strlen(fsname) + 2
                    > buffer_size) {
                        rc = -EOVERFLOW;
-                       goto free_dir;
+                       goto free_dir_no_msg;
                }
 
                sprintf(buffer + used, "%s.%s", fsname, pool->d_name);
@@ -1554,6 +1564,10 @@ free_dir:
        if (rc)
                llapi_error(LLAPI_MSG_ERROR, rc,
                            "Error reading pool list for '%s'", name);
+       else
+               llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
+
+free_dir_no_msg:
        closedir(dir);
 free_path:
        cfs_free_param_data(&pathname);
@@ -2828,7 +2842,8 @@ void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
        }
 
        if (verbose & VERBOSE_HASH_TYPE) {
-               unsigned int type = lum->lum_hash_type;
+               unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
+               unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
 
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
                if (verbose & ~VERBOSE_HASH_TYPE)
@@ -2837,9 +2852,18 @@ void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
                        llapi_printf(LLAPI_MSG_NORMAL, "%s",
                                     mdt_hash_name[type]);
                else
-                       llapi_printf(LLAPI_MSG_NORMAL, "%d",
-                                    (int)type);
+                       llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
+
+               if (flags & LMV_HASH_FLAG_MIGRATION)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
+               if (flags & LMV_HASH_FLAG_DEAD)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",dead");
+               if (flags & LMV_HASH_FLAG_BAD_TYPE)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
+               if (flags & LMV_HASH_FLAG_LOST_LMV)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
                separator = "\n";
+
        }
 
        if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
@@ -4462,19 +4486,22 @@ migrate:
                        goto migrate;
                } else if (errno == EALREADY) {
                        if (param->fp_verbose & VERBOSE_DETAIL)
-                               fprintf(stdout,
-                                       "%s was migrated to MDT%d already\n",
-                                       path, lmu->lum_stripe_offset);
+                               llapi_printf(LLAPI_MSG_NORMAL,
+                                            "%s was migrated to MDT%d already\n",
+                                            path, lmu->lum_stripe_offset);
                        ret = 0;
                } else {
                        ret = -errno;
-                       fprintf(stderr, "%s migrate failed: %s (%d)\n",
-                               path, strerror(-ret), ret);
+                       llapi_error(LLAPI_MSG_ERROR, ret,
+                                   "%s migrate failed: %s (%d)\n",
+                                   path, strerror(-ret), ret);
                        goto out;
                }
        } else if (param->fp_verbose & VERBOSE_DETAIL) {
-               fprintf(stdout, "migrate %s to MDT%d stripe count %d\n",
-                       path, lmu->lum_stripe_offset, lmu->lum_stripe_count);
+               llapi_printf(LLAPI_MSG_NORMAL,
+                            "migrate %s to MDT%d stripe count %d\n",
+                            path, lmu->lum_stripe_offset,
+                            lmu->lum_stripe_count);
        }
 
 out:
@@ -4801,26 +4828,32 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index,
 
 int llapi_ping(char *obd_type, char *obd_name)
 {
+       int flags = O_RDONLY;
+       char buf[1] = { 0 };
        glob_t path;
-       char buf[1];
        int rc, fd;
 
        rc = cfs_get_param_paths(&path, "%s/%s/ping",
                                obd_type, obd_name);
        if (rc != 0)
                return -errno;
-
-       fd = open(path.gl_pathv[0], O_WRONLY);
+retry_open:
+       fd = open(path.gl_pathv[0], flags);
        if (fd < 0) {
+               if (errno == EACCES && flags == O_RDONLY) {
+                       flags = O_WRONLY;
+                       goto retry_open;
+               }
                rc = -errno;
                llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
                            path.gl_pathv[0]);
                goto failed;
        }
 
-       /* 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 (flags == O_RDONLY)
+               rc = read(fd, buf, sizeof(buf));
+       else
+               rc = write(fd, buf, sizeof(buf));
        if (rc < 0)
                rc = -errno;
        close(fd);