Whamcloud - gitweb
LU-11213 uapi: Remove unused CONNECT flag
[fs/lustre-release.git] / lustre / utils / liblustreapi_util.c
index f4788a8..59f7e98 100644 (file)
@@ -5,7 +5,7 @@
  *
  * (C) Copyright (c) 2015, Cray Inc, all rights reserved.
  *
- * Copyright (c) 2016 Intel Corporation.
+ * Copyright (c) 2016, 2017, Intel Corporation.
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the GNU Lesser General Public License
@@ -34,6 +34,7 @@
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/ioctl.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/time.h>
@@ -41,7 +42,6 @@
 #include <sys/stat.h>
 #include <sys/syscall.h>
 #include <lustre/lustreapi.h>
-#include <libcfs/util/string.h>        /* only needed for compat strlcpy() */
 #include <linux/lustre/lustre_ver.h>   /* only until LUSTRE_VERSION_CODE is gone */
 #include "lustreapi_internal.h"
 
@@ -137,7 +137,7 @@ int llapi_get_version_string(char *version, unsigned int version_size)
                return -1;
        }
 
-       if (strlcpy(version, ptr, version_size) >= version_size) {
+       if (snprintf(version, version_size, "%s", ptr) >= version_size) {
                errno = EOVERFLOW;
                return -1;
        }
@@ -181,3 +181,133 @@ int llapi_get_version(char *buffer, int buffer_size, char **version)
        return rc;
 }
 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 4, 53, 0) */
+
+/*
+ * fsname must be specified
+ * if poolname is NULL, search tgtname in fsname
+ * if poolname is not NULL:
+ *  if poolname not found returns errno < 0
+ *  if tgtname is NULL, returns 1 if pool is not empty and 0 if pool empty
+ *  if tgtname is not NULL, returns 1 if target is in pool and 0 if not
+ */
+int llapi_search_tgt(const char *fsname, const char *poolname,
+                    const char *tgtname, bool is_mdt)
+{
+       char buffer[PATH_MAX];
+       size_t len = 0;
+       glob_t param;
+       FILE *fd;
+       int rc;
+
+       if (fsname && fsname[0] == '\0')
+               fsname = NULL;
+       if (!fsname) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (poolname && poolname[0] == '\0')
+               poolname = NULL;
+       if (tgtname) {
+               if (tgtname[0] == '\0')
+                       tgtname = NULL;
+               else
+                       len = strlen(tgtname);
+       }
+
+       /* You need one or the other to have something in it */
+       if (!poolname && !tgtname) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (poolname) {
+               rc = poolpath(&param, fsname, NULL);
+               if (!rc) {
+                       snprintf(buffer, sizeof(buffer) - 1, "%s/%s",
+                                param.gl_pathv[0], poolname);
+                       buffer[sizeof(buffer) - 1] = '\0';
+               }
+       } else {
+               rc = get_lustre_param_path(is_mdt ? "lmv" : "lov", fsname,
+                                          FILTER_BY_FS_NAME,
+                                          "target_obd", &param);
+               if (!rc) {
+                       strncpy(buffer, param.gl_pathv[0],
+                               sizeof(buffer) - 1);
+                       buffer[sizeof(buffer) - 1] = '\0';
+               }
+       }
+       cfs_free_param_data(&param);
+       if (rc)
+               goto out;
+
+       fd = fopen(buffer, "r");
+       if (!fd) {
+               rc = -errno;
+               goto out;
+       }
+
+       while (fgets(buffer, sizeof(buffer), fd)) {
+               if (!poolname) {
+                       char *ptr;
+                       /* Search for an tgtname in the list of all targets
+                        * Line format is IDX: fsname-OST/MDTxxxx_UUID STATUS */
+                       ptr = strchr(buffer, ' ');
+                       if (ptr && strncmp(ptr + 1, tgtname, len) == 0) {
+                               rc = 1;
+                               goto out_close;
+                       }
+               } else {
+                       /* Search for an tgtname in a pool,
+                        * (or an existing non-empty pool if no tgtname) */
+                       if (!tgtname || strncmp(buffer, tgtname, len) == 0) {
+                               rc = 1;
+                               goto out_close;
+                       }
+               }
+       }
+out_close:
+       fclose(fd);
+out:
+       if (rc < 0)
+               errno = -rc;
+       return rc;
+}
+
+int llapi_search_mdt(const char *fsname, const char *poolname,
+                    const char *mdtname)
+{
+       return llapi_search_tgt(fsname, poolname, mdtname, true);
+}
+
+int llapi_search_ost(const char *fsname, const char *poolname,
+                    const char *ostname)
+{
+       return llapi_search_tgt(fsname, poolname, ostname, false);
+}
+
+int llapi_rmfid(const char *path, struct fid_array *fa)
+{
+       char rootpath[PATH_MAX];
+       int fd, rc;
+
+retry_open:
+       fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
+       if (fd < 0) {
+               if (errno == ENOENT && path != rootpath) {
+                       rc = llapi_search_rootpath(rootpath, path);
+                       if (!rc) {
+                               path = rootpath;
+                               goto retry_open;
+                       }
+               } else {
+                       return -errno;
+               }
+       }
+
+       rc = ioctl(fd, LL_IOC_RMFID, fa);
+       close(fd);
+
+       return rc ? -errno : 0;
+}